Skip to content

Commit 028b420

Browse files
committed
add central hid callback implementation
1 parent 231f862 commit 028b420

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

libraries/Bluefruit52Lib/examples/Central/central_hid/central_hid.ino

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
BLEClientHidAdafruit hid;
2525

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
2727
hid_keyboard_report_t last_kbd_report = { 0 };
2828
hid_mouse_report_t last_mse_report = { 0 };
2929

@@ -42,7 +42,10 @@ void setup()
4242

4343
// Init BLE Central Uart Serivce
4444
hid.begin();
45-
//clientUart.setRxCallback(bleuart_rx_callback);
45+
46+
#if POLLING == 0
47+
hid.setKeyboardReportCallback(keyboard_report_callback);
48+
#endif
4649

4750
// Increase Blink rate to different from PrPh advertising mode
4851
Bluefruit.setConnLedInterval(250);
@@ -141,7 +144,7 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
141144
void loop()
142145
{
143146

144-
#if POLLING
147+
#if POLLING == 1
145148
// nothing to do if hid not discovered
146149
if ( !hid.discovered() ) return;
147150

@@ -151,34 +154,51 @@ void loop()
151154
// Get latest report
152155
hid.getKeyboardReport(&kbd_report);
153156

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)) )
156176
{
157177
bool shifted = false;
158178

159-
if ( kbd_report.modifier )
179+
if ( report->modifier )
160180
{
161-
if ( kbd_report.modifier & (KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_RIGHTCTRL) )
181+
if ( report->modifier & (KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_RIGHTCTRL) )
162182
{
163183
Serial.print("Ctrl ");
164184
}
165185

166-
if ( kbd_report.modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT) )
186+
if ( report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT) )
167187
{
168188
Serial.print("Shift ");
169189

170190
shifted = true;
171191
}
172192

173-
if ( kbd_report.modifier & (KEYBOARD_MODIFIER_LEFTALT | KEYBOARD_MODIFIER_RIGHTALT) )
193+
if ( report->modifier & (KEYBOARD_MODIFIER_LEFTALT | KEYBOARD_MODIFIER_RIGHTALT) )
174194
{
175195
Serial.print("Alt ");
176196
}
177197
}
178198

179199
for(uint8_t i=0; i<6; i++)
180200
{
181-
uint8_t kc = kbd_report.keycode[i];
201+
uint8_t kc = report->keycode[i];
182202
char ch = 0;
183203

184204
if ( kc < 128 )
@@ -198,12 +218,6 @@ void loop()
198218
}
199219

200220
// 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));
208222
}
209223

0 commit comments

Comments
 (0)