@@ -45,7 +45,7 @@ void setup1() {
4545 while ( !Serial ) delay (10 ); // wait for native usb
4646 Serial.printf (" Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n " , cpu_hz);
4747 Serial.printf (" Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n " );
48- while (1 ) delay (1 );
48+ while (1 ) delay (1 );
4949 }
5050
5151#ifdef PIN_5V_EN
@@ -71,14 +71,14 @@ uint32_t repeat_timeout;
7171const uint32_t repeat_time = 150 ;
7272
7373void send_ascii (uint8_t code) {
74- old_ascii = code;
75- repeat_timeout = millis () + repeat_time;
76- if (code > 32 && code < 127 ) {
77- Serial.printf (" '%c'\r\n " , code);
78- } else {
79- Serial.printf (" '\\ x%02x'\r\n " , code);
80- }
81- pio_serial.write (code);
74+ old_ascii = code;
75+ repeat_timeout = millis () + repeat_time;
76+ if (code > 32 && code < 127 ) {
77+ Serial.printf (" '%c'\r\n " , code);
78+ } else {
79+ Serial.printf (" '\\ x%02x'\r\n " , code);
80+ }
81+ pio_serial.write (code);
8282}
8383
8484void loop1 ()
@@ -124,124 +124,125 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
124124}
125125
126126#define FLAG_ALPHABETIC (1 )
127- #define FLAG_SHIFT (2 )
128- #define FLAG_NUMLOCK (4 )
127+ #define FLAG_SHIFT (2 )
128+ #define FLAG_NUMLOCK (4 )
129129#define FLAG_CTRL (8 )
130130#define FLAG_LUT (16 )
131131
132132const char * const lut[] = {
133- " !@#$%^&*()" , /* 0 - shifted numeric keys */
134- " \r\x1b\10\t -=[]\\ #;'`,./" , /* 1 - symbol keys */
135- " \n\x1b\177\t _+{}|~:\" ~<>?" , /* 2 - shifted */
136- " \3\4\2\1 " , /* 3 - arrow keys RLDU */
137- " /*-+\n 1234567890." , /* 4 - keypad w/numlock */
138- " /*-+\n\xff\2\xff\4\xff\3\xff\1\xff\xff ." , /* 5 - keypad w/o numlock */
133+ " !@#$%^&*()" , /* 0 - shifted numeric keys */
134+ " \r\x1b\10\t -=[]\\ #;'`,./" , /* 1 - symbol keys */
135+ " \n\x1b\177\t _+{}|~:\" ~<>?" , /* 2 - shifted */
136+ " \3\4\2\1 " , /* 3 - arrow keys RLDU */
137+ " /*-+\n 1234567890." , /* 4 - keypad w/numlock */
138+ " /*-+\n\xff\2\xff\4\xff\3\xff\1\xff\xff ." , /* 5 - keypad w/o numlock */
139139};
140140
141141struct keycode_mapper {
142- uint8_t first, last, code, flags;
143- } keycode_to_ascii[] = {
144- { HID_KEY_A, HID_KEY_Z, ' a' , FLAG_ALPHABETIC, },
145-
146- { HID_KEY_1, HID_KEY_9, 0 , FLAG_SHIFT | FLAG_LUT, },
147- { HID_KEY_1, HID_KEY_9, ' 1' , 0 , },
148- { HID_KEY_0, HID_KEY_0, ' )' , FLAG_SHIFT, },
149- { HID_KEY_0, HID_KEY_0, ' 0' , 0 , },
150-
151- { HID_KEY_ENTER, HID_KEY_ENTER, ' \n ' , FLAG_CTRL },
152- { HID_KEY_ENTER, HID_KEY_SLASH, 2 , FLAG_SHIFT | FLAG_LUT, },
153- { HID_KEY_ENTER, HID_KEY_SLASH, 1 , FLAG_LUT, },
154-
155- { HID_KEY_F1, HID_KEY_F1, 0x1e , 0 , }, // help key on xerox 820 kbd
156-
157- { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_UP, 3 , FLAG_LUT },
158-
159- { HID_KEY_KEYPAD_DIVIDE, HID_KEY_KEYPAD_DECIMAL, 4 , FLAG_NUMLOCK | FLAG_LUT },
160- { HID_KEY_KEYPAD_DIVIDE, HID_KEY_KEYPAD_DECIMAL, 5 , FLAG_LUT },
161- };
142+ uint8_t first, last, code, flags;
143+ } keycode_to_ascii[] = {
144+ { HID_KEY_A, HID_KEY_Z, ' a' , FLAG_ALPHABETIC, },
145+
146+ { HID_KEY_1, HID_KEY_9, 0 , FLAG_SHIFT | FLAG_LUT, },
147+ { HID_KEY_1, HID_KEY_9, ' 1' , 0 , },
148+ { HID_KEY_0, HID_KEY_0, ' )' , FLAG_SHIFT, },
149+ { HID_KEY_0, HID_KEY_0, ' 0' , 0 , },
150+
151+ { HID_KEY_ENTER, HID_KEY_ENTER, ' \n ' , FLAG_CTRL },
152+ { HID_KEY_ENTER, HID_KEY_SLASH, 2 , FLAG_SHIFT | FLAG_LUT, },
153+ { HID_KEY_ENTER, HID_KEY_SLASH, 1 , FLAG_LUT, },
154+
155+ { HID_KEY_F1, HID_KEY_F1, 0x1e , 0 , }, // help key on xerox 820 kbd
156+
157+ { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_UP, 3 , FLAG_LUT },
158+
159+ { HID_KEY_KEYPAD_DIVIDE, HID_KEY_KEYPAD_DECIMAL, 4 , FLAG_NUMLOCK | FLAG_LUT },
160+ { HID_KEY_KEYPAD_DIVIDE, HID_KEY_KEYPAD_DECIMAL, 5 , FLAG_LUT },
161+ };
162162
163163
164164bool report_contains (const hid_keyboard_report_t &report, uint8_t key) {
165- for (int i=0 ; i<6 ; i++) { if (report.keycode [i] == key) return true ; }
166- return false ;
165+ for (int i = 0 ; i < 6 ; i++) {
166+ if (report.keycode [i] == key) return true ;
167+ }
168+ return false ;
167169}
168170
169171hid_keyboard_report_t old_report;
170172
171173void process_event (uint8_t dev_addr, uint8_t instance, const hid_keyboard_report_t &report) {
172- bool alt = report.modifier & 0x44 ;
173- bool shift = report.modifier & 0x22 ;
174- bool ctrl = report.modifier & 0x11 ;
175- bool caps = old_report.reserved & 1 ;
176- bool num = old_report.reserved & 2 ;
177- uint8_t code = 0 ;
178-
179- if (report.keycode [0 ] == 1 && report.keycode [1 ] == 1 ) {
180- // keyboard says it has exceeded max kro
181- return ;
182- }
174+ bool alt = report.modifier & 0x44 ;
175+ bool shift = report.modifier & 0x22 ;
176+ bool ctrl = report.modifier & 0x11 ;
177+ bool caps = old_report.reserved & 1 ;
178+ bool num = old_report.reserved & 2 ;
179+ uint8_t code = 0 ;
180+
181+ if (report.keycode [0 ] == 1 && report.keycode [1 ] == 1 ) {
182+ // keyboard says it has exceeded max kro
183+ return ;
184+ }
183185
184- // something was pressed or release, so cancel any key repeat
185- old_ascii = -1 ;
186+ // something was pressed or release, so cancel any key repeat
187+ old_ascii = -1 ;
186188
187- for (auto keycode : report.keycode ) {
188- if (keycode == 0 ) continue ;
189- if (report_contains (old_report, keycode)) continue ;
189+ for (auto keycode : report.keycode ) {
190+ if (keycode == 0 ) continue ;
191+ if (report_contains (old_report, keycode)) continue ;
190192
191- /* key is newly pressed */
192- if (keycode == HID_KEY_NUM_LOCK) {
193- num = !num;
194- } else if (keycode == HID_KEY_CAPS_LOCK) {
195- caps = !caps;
193+ /* key is newly pressed */
194+ if (keycode == HID_KEY_NUM_LOCK) {
195+ num = !num;
196+ } else if (keycode == HID_KEY_CAPS_LOCK) {
197+ caps = !caps;
198+ } else {
199+ for (const auto &mapper : keycode_to_ascii) {
200+ if (!(keycode >= mapper.first && keycode <= mapper.last ))
201+ continue ;
202+ if (mapper.flags & FLAG_SHIFT && !shift)
203+ continue ;
204+ if (mapper.flags & FLAG_NUMLOCK && !num)
205+ continue ;
206+ if (mapper.flags & FLAG_CTRL && !ctrl)
207+ continue ;
208+ if (mapper.flags & FLAG_LUT) {
209+ code = lut[mapper.code ][keycode - mapper.first ];
196210 } else {
197- for (const auto &mapper : keycode_to_ascii) {
198- if (!(keycode >= mapper.first && keycode <= mapper.last ))
199- continue ;
200- if (mapper.flags & FLAG_SHIFT && !shift)
201- continue ;
202- if (mapper.flags & FLAG_NUMLOCK && !num)
203- continue ;
204- if (mapper.flags & FLAG_CTRL && !ctrl)
205- continue ;
206- if (mapper.flags & FLAG_LUT) {
207- code = lut[mapper.code ][keycode - mapper.first ];
208- } else {
209- code = keycode - mapper.first + mapper.code ;
210- }
211- if (mapper.flags & FLAG_ALPHABETIC) {
212- if (shift ^ caps) {
213- code ^= (' a' ^ ' A' );
214- }
215- }
216- if (ctrl) code &= 0x1f ;
217- if (alt) code ^= 0x80 ;
218- send_ascii (code);
219- break ;
220- }
211+ code = keycode - mapper.first + mapper.code ;
212+ }
213+ if (mapper.flags & FLAG_ALPHABETIC) {
214+ if (shift ^ caps) {
215+ code ^= (' a' ^ ' A' );
216+ }
221217 }
218+ if (ctrl) code &= 0x1f ;
219+ if (alt) code ^= 0x80 ;
220+ send_ascii (code);
221+ break ;
222+ }
222223 }
224+ }
223225
224- uint8_t leds = (caps | (num << 1 ));
225- if (leds != old_report.reserved ) {
226- Serial.printf (" Send LEDs report %d (dev:instance = %d:%d)\r\n " , leds, dev_addr, instance);
227- // no worky
228- auto r = tuh_hid_set_report (dev_addr, instance/* idx*/ , 0 /* report_id*/ , HID_REPORT_TYPE_OUTPUT/* report_type*/ , &leds, sizeof (leds));
229- Serial.printf (" set_report() -> %d\n " , (int )r);
230- }
231- old_report = report;
232- old_report.reserved = leds;
226+ uint8_t leds = (caps | (num << 1 ));
227+ if (leds != old_report.reserved ) {
228+ Serial.printf (" Send LEDs report %d (dev:instance = %d:%d)\r\n " , leds, dev_addr, instance);
229+ // no worky
230+ auto r = tuh_hid_set_report (dev_addr, instance/* idx*/ , 0 /* report_id*/ , HID_REPORT_TYPE_OUTPUT/* report_type*/ , &leds, sizeof (leds));
231+ Serial.printf (" set_report() -> %d\n " , (int )r);
232+ }
233+ old_report = report;
234+ old_report.reserved = leds;
233235}
234236
235237// Invoked when received report from device via interrupt endpoint
236238void tuh_hid_report_received_cb (uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len) {
237239 if ( len != sizeof (hid_keyboard_report_t ) ) {
238240 Serial.printf (" report len = %u NOT 8, probably something wrong !!\r\n " , len);
239- }else {
241+ } else {
240242 process_event (dev_addr, instance, *(hid_keyboard_report_t *)report);
241243 }
242244 // continue to request to receive report
243245 if (!tuh_hid_receive_report (dev_addr, instance)) {
244246 Serial.printf (" Error: cannot request to receive report\r\n " );
245247 }
246248}
247-
0 commit comments