2626
2727// ================================================================================
2828// ================================================================================
29- // Keyboard
29+ // Keyboard
3030
3131static const uint8_t _hidReportDescriptor[] PROGMEM = {
32- // Keyboard
33- 0x05 , 0x01 , /* USAGE_PAGE (Generic Desktop) 47 */
34- 0x09 , 0x06 , /* USAGE (Keyboard) */
35- 0xa1 , 0x01 , /* COLLECTION (Application) */
36- 0x85 , HID_REPORTID_KEYBOARD, /* REPORT_ID TODO order important? */
37- 0x05 , 0x07 , /* USAGE_PAGE (Keyboard) */
38-
39- /* Keyboard Modifiers (shift, alt, ...) */
40- 0x19 , 0xe0 , /* USAGE_MINIMUM (Keyboard LeftControl) */
41- 0x29 , 0xe7 , /* USAGE_MAXIMUM (Keyboard Right GUI) */
42- 0x15 , 0x00 , /* LOGICAL_MINIMUM (0) */
43- 0x25 , 0x01 , /* LOGICAL_MAXIMUM (1) */
44- 0x75 , 0x01 , /* REPORT_SIZE (1) */
45- 0x95 , 0x08 , /* REPORT_COUNT (8) */
46- 0x81 , 0x02 , /* INPUT (Data,Var,Abs) */
47-
48- /* Reserved byte, used for consumer reports, only works with linux */
49- 0x05 , 0x0C , /* Usage Page (Consumer) */
50- 0x95 , 0x01 , /* REPORT_COUNT (1) */
51- 0x75 , 0x08 , /* REPORT_SIZE (8) */
52- 0x15 , 0x00 , /* LOGICAL_MINIMUM (0) */
53- 0x26 , 0xFF , 0x00 , /* LOGICAL_MAXIMUM (255) */
54- 0x19 , 0x00 , /* USAGE_MINIMUM (0) */
55- 0x29 , 0xFF , /* USAGE_MAXIMUM (255) */
56- 0x81 , 0x00 , /* INPUT (Data,Ary,Abs) */
57-
58- /* 6 Keyboard keys */
59- 0x05 , 0x07 , /* USAGE_PAGE (Keyboard) */
60- 0x95 , 0x06 , /* REPORT_COUNT (6) */
61- 0x75 , 0x08 , /* REPORT_SIZE (8) */
62- 0x15 , 0x00 , /* LOGICAL_MINIMUM (0) */
63- 0x26 , 0xE7 , 0x00 , /* LOGICAL_MAXIMUM (231) */
64- 0x19 , 0x00 , /* USAGE_MINIMUM (Reserved (no event indicated)) */
65- 0x29 , 0xE7 , /* USAGE_MAXIMUM (Keyboard Right GUI) */
66- 0x81 , 0x00 , /* INPUT (Data,Ary,Abs) */
67-
68- /* End */
69- 0xc0 /* END_COLLECTION */
32+ // Keyboard
33+ 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop) // 47
34+ 0x09 , 0x06 , // USAGE (Keyboard)
35+ 0xa1 , 0x01 , // COLLECTION (Application)
36+ 0x85 , HID_REPORTID_KEYBOARD, // REPORT_ID TODO order important?
37+ 0x05 , 0x07 , // USAGE_PAGE (Keyboard)
38+
39+ // Keyboard Modifiers (shift, alt, ...)
40+ 0x19 , 0xe0 , // USAGE_MINIMUM (Keyboard LeftControl)
41+ 0x29 , 0xe7 , // USAGE_MAXIMUM (Keyboard Right GUI)
42+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
43+ 0x25 , 0x01 , // LOGICAL_MAXIMUM (1)
44+ 0x75 , 0x01 , // REPORT_SIZE (1)
45+ 0x95 , 0x08 , // REPORT_COUNT (8)
46+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
47+
48+ // Reserved byte, used for consumer reports, only works with linux
49+ 0x05 , 0x0C , // Usage Page (Consumer)
50+ 0x95 , 0x01 , // REPORT_COUNT (1)
51+ 0x75 , 0x08 , // REPORT_SIZE (8)
52+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
53+ 0x26 , 0xFF , 0x00 , // LOGICAL_MAXIMUM (255)
54+ 0x19 , 0x00 , // USAGE_MINIMUM (0)
55+ 0x29 , 0xFF , // USAGE_MAXIMUM (255)
56+ 0x81 , 0x00 , // INPUT (Data,Ary,Abs)
57+
58+ // 6 Keyboard keys
59+ 0x05 , 0x07 , // USAGE_PAGE (Keyboard)
60+ 0x95 , 0x06 , // REPORT_COUNT (6)
61+ 0x75 , 0x08 , // REPORT_SIZE (8)
62+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
63+ 0x26 , 0xE7 , 0x00 , // LOGICAL_MAXIMUM (231)
64+ 0x19 , 0x00 , // USAGE_MINIMUM (Reserved (no event indicated))
65+ 0x29 , 0xE7 , // USAGE_MAXIMUM (Keyboard Right GUI)
66+ 0x81 , 0x00 , // INPUT (Data,Ary,Abs)
67+
68+ // End
69+ 0xc0 // END_COLLECTION
7070};
7171
7272Keyboard_::Keyboard_ (void )
7373{
74- static HIDSubDescriptor node (_hidReportDescriptor, sizeof (_hidReportDescriptor));
75- HID ().AppendDescriptor (&node);
74+ static HIDSubDescriptor node (_hidReportDescriptor, sizeof (_hidReportDescriptor));
75+ HID ().AppendDescriptor (&node);
7676}
7777
7878void Keyboard_::begin (void )
@@ -85,70 +85,70 @@ void Keyboard_::end(void)
8585
8686void Keyboard_::sendReport (KeyReport* keys)
8787{
88- HID ().SendReport (2 ,keys,sizeof (KeyReport));
88+ HID ().SendReport (2 ,keys,sizeof (KeyReport));
8989}
9090
9191size_t Keyboard_::set (KeyboardKeycode k, bool s)
9292{
93- // It's a modifier key
94- if (k >= KEY_LEFT_CTRL && k <= KEY_RIGHT_GUI)
95- {
96- // Convert key into bitfield (0 - 7)
97- k = KeyboardKeycode (uint8_t (k) - uint8_t (KEY_LEFT_CTRL));
98- if (s){
99- _keyReport.modifiers |= (1 << k);
100- }
101- else {
102- _keyReport.modifiers &= ~(1 << k);
103- }
104- return 1 ;
105- }
106- // It's a normal key
107- else {
108- // get size of keycodes during compile time
109- const uint8_t keycodesSize = sizeof (_keyReport.keycodes );
110-
111- // if we are adding an element to keycodes
112- if (s){
113- // iterate through the keycodes
114- for (uint8_t i = 0 ; i < keycodesSize; i++)
115- {
116- auto key = _keyReport.keycodes [i];
117- // if target key is found
118- if (key == uint8_t (k)) {
119- // do nothing and exit
120- return 1 ;
121- }
122- }
123- // iterate through the keycodes again, this only happens if no existing
124- // keycodes matches k
125- for (uint8_t i = 0 ; i < keycodesSize; i++)
126- {
127- auto key = _keyReport.keycodes [i];
128- // if first instance of empty slot is found
129- if (key == KEY_RESERVED) {
130- // change empty slot to k and exit
131- _keyReport.keycodes [i] = k;
132- return 1 ;
133- }
134- }
135- } else { // we are removing k from keycodes
136- // iterate through the keycodes
137- for (uint8_t i = 0 ; i < keycodesSize; i++)
138- {
139- auto key = _keyReport.keycodes [i];
140- // if target key is found
141- if (key == k) {
142- // remove target and exit
143- _keyReport.keycodes [i] = KEY_RESERVED;
144- return 1 ;
145- }
146- }
147- }
148- }
149-
150- // No empty/pressed key was found
151- return 0 ;
93+ // It's a modifier key
94+ if (k >= KEY_LEFT_CTRL && k <= KEY_RIGHT_GUI)
95+ {
96+ // Convert key into bitfield (0 - 7)
97+ k = KeyboardKeycode (uint8_t (k) - uint8_t (KEY_LEFT_CTRL));
98+ if (s){
99+ _keyReport.modifiers |= (1 << k);
100+ }
101+ else {
102+ _keyReport.modifiers &= ~(1 << k);
103+ }
104+ return 1 ;
105+ }
106+ // It's a normal key
107+ else {
108+ // get size of keycodes during compile time
109+ const uint8_t keycodesSize = sizeof (_keyReport.keycodes );
110+
111+ // if we are adding an element to keycodes
112+ if (s){
113+ // iterate through the keycodes
114+ for (uint8_t i = 0 ; i < keycodesSize; i++)
115+ {
116+ auto key = _keyReport.keycodes [i];
117+ // if target key is found
118+ if (key == uint8_t (k)) {
119+ // do nothing and exit
120+ return 1 ;
121+ }
122+ }
123+ // iterate through the keycodes again, this only happens if no existing
124+ // keycodes matches k
125+ for (uint8_t i = 0 ; i < keycodesSize; i++)
126+ {
127+ auto key = _keyReport.keycodes [i];
128+ // if first instance of empty slot is found
129+ if (key == KEY_RESERVED) {
130+ // change empty slot to k and exit
131+ _keyReport.keycodes [i] = k;
132+ return 1 ;
133+ }
134+ }
135+ } else { // we are removing k from keycodes
136+ // iterate through the keycodes
137+ for (uint8_t i = 0 ; i < keycodesSize; i++)
138+ {
139+ auto key = _keyReport.keycodes [i];
140+ // if target key is found
141+ if (key == k) {
142+ // remove target and exit
143+ _keyReport.keycodes [i] = KEY_RESERVED;
144+ return 1 ;
145+ }
146+ }
147+ }
148+ }
149+
150+ // No empty/pressed key was found
151+ return 0 ;
152152}
153153
154154uint8_t USBPutChar (uint8_t c);
@@ -159,60 +159,60 @@ uint8_t USBPutChar(uint8_t c);
159159// call release(), releaseAll(), or otherwise clear the report and resend.
160160size_t Keyboard_::press (uint8_t k)
161161{
162- // Press key and send report to host
163- auto ret = set ((KeyboardKeycode)k, true );
164- if (ret){
165- sendReport (&_keyReport);
166- return 1 ;
167- }
168- return 0 ;
162+ // Press key and send report to host
163+ auto ret = set ((KeyboardKeycode)k, true );
164+ if (ret){
165+ sendReport (&_keyReport);
166+ return 1 ;
167+ }
168+ return 0 ;
169169}
170170
171171// release() takes the specified key out of the persistent key report and
172172// sends the report. This tells the OS the key is no longer pressed and that
173173// it shouldn't be repeated any more.
174174size_t Keyboard_::release (uint8_t k)
175175{
176- auto ret = set ((KeyboardKeycode)k, false );
177- if (ret){
178- sendReport (&_keyReport);
179- return 1 ;
180- }
181- return 0 ;
176+ auto ret = set ((KeyboardKeycode)k, false );
177+ if (ret){
178+ sendReport (&_keyReport);
179+ return 1 ;
180+ }
181+ return 0 ;
182182}
183183
184184void Keyboard_::releaseAll (void )
185185{
186- _keyReport.keycodes [0 ] = 0 ;
187- _keyReport.keycodes [1 ] = 0 ;
188- _keyReport.keycodes [2 ] = 0 ;
189- _keyReport.keycodes [3 ] = 0 ;
190- _keyReport.keycodes [4 ] = 0 ;
191- _keyReport.keycodes [5 ] = 0 ;
192- _keyReport.modifiers = 0 ;
193- sendReport (&_keyReport);
186+ _keyReport.keycodes [0 ] = 0 ;
187+ _keyReport.keycodes [1 ] = 0 ;
188+ _keyReport.keycodes [2 ] = 0 ;
189+ _keyReport.keycodes [3 ] = 0 ;
190+ _keyReport.keycodes [4 ] = 0 ;
191+ _keyReport.keycodes [5 ] = 0 ;
192+ _keyReport.modifiers = 0 ;
193+ sendReport (&_keyReport);
194194}
195195
196196size_t Keyboard_::write (uint8_t c)
197197{
198- uint8_t p = press (c); // Keydown
199- release (c); // Keyup
200- return p; // just return the result of press() since release() almost always returns 1
198+ uint8_t p = press (c); // Keydown
199+ release (c); // Keyup
200+ return p; // just return the result of press() since release() almost always returns 1
201201}
202202
203203size_t Keyboard_::write (const uint8_t *buffer, size_t size) {
204- size_t n = 0 ;
205- while (size--) {
206- if (*buffer != ' \r ' ) {
207- if (write (*buffer)) {
208- n++;
209- } else {
210- break ;
211- }
212- }
213- buffer++;
214- }
215- return n;
204+ size_t n = 0 ;
205+ while (size--) {
206+ if (*buffer != ' \r ' ) {
207+ if (write (*buffer)) {
208+ n++;
209+ } else {
210+ break ;
211+ }
212+ }
213+ buffer++;
214+ }
215+ return n;
216216}
217217
218218Keyboard_ Keyboard;
0 commit comments