23
23
24
24
BLEClientHidAdafruit hid;
25
25
26
- // Last checked report, to detect if we receive a new one with polling method
26
+ // Last checked report, to detect if there is changes between reports
27
27
hid_keyboard_report_t last_kbd_report = { 0 };
28
28
hid_mouse_report_t last_mse_report = { 0 };
29
29
@@ -42,7 +42,10 @@ void setup()
42
42
43
43
// Init BLE Central Uart Serivce
44
44
hid.begin ();
45
- // clientUart.setRxCallback(bleuart_rx_callback);
45
+
46
+ #if POLLING == 0
47
+ hid.setKeyboardReportCallback (keyboard_report_callback);
48
+ #endif
46
49
47
50
// Increase Blink rate to different from PrPh advertising mode
48
51
Bluefruit.setConnLedInterval (250 );
@@ -141,7 +144,7 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
141
144
void loop ()
142
145
{
143
146
144
- #if POLLING
147
+ #if POLLING == 1
145
148
// nothing to do if hid not discovered
146
149
if ( !hid.discovered () ) return ;
147
150
@@ -151,34 +154,51 @@ void loop()
151
154
// Get latest report
152
155
hid.getKeyboardReport (&kbd_report);
153
156
154
- // Check with last report to see if there is new arrival report
155
- if ( memcmp (&last_kbd_report, &kbd_report, sizeof (kbd_report)) )
157
+ processKeyboardReport (&kbd_report);
158
+
159
+
160
+ // polling interval is 5 ms
161
+ delay (5 );
162
+ #endif
163
+
164
+ }
165
+
166
+
167
+ void keyboard_report_callback (hid_keyboard_report_t * report)
168
+ {
169
+ processKeyboardReport (report);
170
+ }
171
+
172
+ void processKeyboardReport (hid_keyboard_report_t * report)
173
+ {
174
+ // Check with last report to see if there is any changes
175
+ if ( memcmp (&last_kbd_report, report, sizeof (hid_keyboard_report_t )) )
156
176
{
157
177
bool shifted = false ;
158
178
159
- if ( kbd_report. modifier )
179
+ if ( report-> modifier )
160
180
{
161
- if ( kbd_report. modifier & (KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_RIGHTCTRL) )
181
+ if ( report-> modifier & (KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_RIGHTCTRL) )
162
182
{
163
183
Serial.print (" Ctrl " );
164
184
}
165
185
166
- if ( kbd_report. modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT) )
186
+ if ( report-> modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT) )
167
187
{
168
188
Serial.print (" Shift " );
169
189
170
190
shifted = true ;
171
191
}
172
192
173
- if ( kbd_report. modifier & (KEYBOARD_MODIFIER_LEFTALT | KEYBOARD_MODIFIER_RIGHTALT) )
193
+ if ( report-> modifier & (KEYBOARD_MODIFIER_LEFTALT | KEYBOARD_MODIFIER_RIGHTALT) )
174
194
{
175
195
Serial.print (" Alt " );
176
196
}
177
197
}
178
198
179
199
for (uint8_t i=0 ; i<6 ; i++)
180
200
{
181
- uint8_t kc = kbd_report. keycode [i];
201
+ uint8_t kc = report-> keycode [i];
182
202
char ch = 0 ;
183
203
184
204
if ( kc < 128 )
@@ -198,12 +218,6 @@ void loop()
198
218
}
199
219
200
220
// update last report
201
- memcpy (&last_kbd_report, &kbd_report, sizeof (kbd_report));
202
-
203
-
204
- // polling interval is 5 ms
205
- delay (5 );
206
- #endif
207
-
221
+ memcpy (&last_kbd_report, report, sizeof (hid_keyboard_report_t ));
208
222
}
209
223
0 commit comments