21
21
// Single Report (no ID) descriptor
22
22
uint8_t const desc_hid_report[] =
23
23
{
24
- TUD_HID_REPORT_DESC_KEYBOARD (),
24
+ TUD_HID_REPORT_DESC_KEYBOARD ()
25
25
};
26
26
27
27
Adafruit_USBD_HID usb_hid;
28
28
29
29
// Array of pins and its keycode
30
30
// For keycode definition see BLEHidGeneric.h
31
31
#ifdef ARDUINO_ARCH_RP2040
32
- uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
32
+ uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
33
33
#else
34
- uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
34
+ uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
35
35
#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 };
37
38
38
39
uint8_t pincount = sizeof (pins)/sizeof (pins[0 ]);
39
40
@@ -45,6 +46,7 @@ void setup()
45
46
TinyUSB_Device_Init (0 );
46
47
#endif
47
48
49
+ usb_hid.setBootProtocol (HID_ITF_PROTOCOL_KEYBOARD);
48
50
usb_hid.setPollInterval (2 );
49
51
usb_hid.setReportDescriptor (desc_hid_report, sizeof (desc_hid_report));
50
52
usb_hid.setReportCallback (NULL , hid_report_callback);
@@ -72,18 +74,8 @@ void loop()
72
74
// poll gpio once each 2 ms
73
75
delay (2 );
74
76
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
85
78
static bool keyPressedPreviously = false ;
86
- bool anyKeyPressed = false ;
87
79
88
80
uint8_t count=0 ;
89
81
uint8_t keycode[6 ] = { 0 };
@@ -97,35 +89,38 @@ void loop()
97
89
keycode[count++] = hidcode[i];
98
90
99
91
// 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 ;
113
93
}
114
94
}
115
95
116
- // Send any remaining keys (not accumulated up to 6)
117
- if ( count )
96
+ if ( TinyUSBDevice.suspended () && count )
118
97
{
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 ();
120
101
}
121
102
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
126
115
{
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
+ }
129
124
}
130
125
}
131
126
0 commit comments