2121// Single Report (no ID) descriptor
2222uint8_t const desc_hid_report[] =
2323{
24- TUD_HID_REPORT_DESC_KEYBOARD (),
24+ TUD_HID_REPORT_DESC_KEYBOARD ()
2525};
2626
2727Adafruit_USBD_HID usb_hid;
2828
2929// Array of pins and its keycode
3030// For keycode definition see BLEHidGeneric.h
3131#ifdef ARDUINO_ARCH_RP2040
32- uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
32+ uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
3333#else
34- uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
34+ uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
3535#endif
36- uint8_t hidcode[] = { HID_KEY_0, HID_KEY_1, HID_KEY_2, HID_KEY_3 , HID_KEY_4, HID_KEY_5 };
36+
37+ uint8_t hidcode[] = { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_UP , HID_KEY_4, HID_KEY_5 };
3738
3839uint8_t pincount = sizeof (pins)/sizeof (pins[0 ]);
3940
@@ -45,6 +46,7 @@ void setup()
4546 TinyUSB_Device_Init (0 );
4647#endif
4748
49+ usb_hid.setBootProtocol (HID_ITF_PROTOCOL_KEYBOARD);
4850 usb_hid.setPollInterval (2 );
4951 usb_hid.setReportDescriptor (desc_hid_report, sizeof (desc_hid_report));
5052 usb_hid.setReportCallback (NULL , hid_report_callback);
@@ -72,18 +74,8 @@ void loop()
7274 // poll gpio once each 2 ms
7375 delay (2 );
7476
75- // // Remote wakeup
76- // if ( TinyUSBDevice.suspended() && btn )
77- // {
78- // // Wake up host if we are in suspend mode
79- // // and REMOTE_WAKEUP feature is enabled by host
80- // TinyUSBDevice.remoteWakeup();
81- // }
82-
83- if ( !usb_hid.ready () ) return ;
84-
77+ // used to avoid send multiple consecutive zero report for keyboard
8578 static bool keyPressedPreviously = false ;
86- bool anyKeyPressed = false ;
8779
8880 uint8_t count=0 ;
8981 uint8_t keycode[6 ] = { 0 };
@@ -97,35 +89,38 @@ void loop()
9789 keycode[count++] = hidcode[i];
9890
9991 // 6 is max keycode per report
100- if (count == 6 )
101- {
102- usb_hid.keyboardReport (0 , 0 , keycode);
103- delay (2 ); // delay for report to send out
104-
105- // reset report
106- count = 0 ;
107- memset (keycode, 0 , 6 );
108- }
109-
110- // used later
111- anyKeyPressed = true ;
112- keyPressedPreviously = true ;
92+ if (count == 6 ) break ;
11393 }
11494 }
11595
116- // Send any remaining keys (not accumulated up to 6)
117- if ( count )
96+ if ( TinyUSBDevice.suspended () && count )
11897 {
119- usb_hid.keyboardReport (0 , 0 , keycode);
98+ // Wake up host if we are in suspend mode
99+ // and REMOTE_WAKEUP feature is enabled by host
100+ TinyUSBDevice.remoteWakeup ();
120101 }
121102
122- // Send All-zero report to indicate there is no keys pressed
123- // Most of the time, it is, though we don't need to send zero report
124- // every loop(), only a key is pressed in previous loop()
125- if ( !anyKeyPressed && keyPressedPreviously )
103+ // skip if hid is not ready e.g still transferring previous report
104+ if ( !usb_hid.ready () ) return ;
105+
106+ if ( count )
107+ {
108+ // Send report if there is key pressed
109+ uint8_t const report_id = 0 ;
110+ uint8_t const modifier = 0 ;
111+
112+ keyPressedPreviously = true ;
113+ usb_hid.keyboardReport (report_id, modifier, keycode);
114+ }else
126115 {
127- keyPressedPreviously = false ;
128- usb_hid.keyboardRelease (0 );
116+ // Send All-zero report to indicate there is no keys pressed
117+ // Most of the time, it is, though we don't need to send zero report
118+ // every loop(), only a key is pressed in previous loop()
119+ if ( keyPressedPreviously )
120+ {
121+ keyPressedPreviously = false ;
122+ usb_hid.keyboardRelease (0 );
123+ }
129124 }
130125}
131126
0 commit comments