18
18
*/
19
19
#include < bluefruit.h>
20
20
21
- // Polling or callback implementation
22
- #define POLLING 1
21
+ /*
22
+ * Polling or callback implementation
23
+ * 0 = Request data using a poll
24
+ * 1 = Use callbacks
25
+ */
26
+ #define POLLING 0
27
+ /*
28
+ * Device Type flag for use in the example
29
+ * Change this if you want to demo the Gamepad!
30
+ * 0 = Keyboard + Mouse
31
+ * 1 = Gamepad
32
+ */
33
+ #define DEVICE_TYPE 0
23
34
24
35
BLEClientHidAdafruit hid;
25
36
26
37
// Last checked report, to detect if there is changes between reports
27
38
hid_keyboard_report_t last_kbd_report = { 0 };
28
39
hid_mouse_report_t last_mse_report = { 0 };
40
+ hid_gamepad_report_t last_gpd_report = { 0 };
29
41
30
42
void setup ()
31
43
{
32
44
Serial.begin (115200 );
33
45
// while ( !Serial ) delay(10); // for nrf52840 with native usb
34
46
35
- Serial.println (" Bluefruit52 Central HID (Keyboard + Mouse) Example" );
47
+ Serial.println (" Bluefruit52 Central HID (Keyboard + Mouse + Gamepad ) Example" );
36
48
Serial.println (" --------------------------------------------------\n " );
37
49
38
50
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
@@ -46,6 +58,7 @@ void setup()
46
58
47
59
#if POLLING == 0
48
60
hid.setKeyboardReportCallback (keyboard_report_callback);
61
+ hid.setGamepadReportCallback (gamepad_report_callback);
49
62
#endif
50
63
51
64
// Increase Blink rate to different from PrPh advertising mode
@@ -69,7 +82,7 @@ void setup()
69
82
Bluefruit.Scanner .restartOnDisconnect (true );
70
83
Bluefruit.Scanner .setInterval (160 , 80 ); // in unit of 0.625 ms
71
84
Bluefruit.Scanner .filterService (hid); // only report HID service
72
- Bluefruit.Scanner .useActiveScan (false );
85
+ Bluefruit.Scanner .useActiveScan (true );
73
86
Bluefruit.Scanner .start (0 ); // 0 = Don't stop scanning after n seconds
74
87
}
75
88
@@ -137,15 +150,21 @@ void connection_secured_callback(uint16_t conn_handle)
137
150
Serial.printf (" HID Flags : 0x%02X\n " , hidInfo[3 ]);
138
151
139
152
// BLEClientHidAdafruit currently only supports Boot Protocol Mode
140
- // for Keyboard and Mouse. Let's set the protocol mode on prph to Boot Mode
153
+ // for Keyboard and Mouse. If we are using a Keyboard + Mouse,
154
+ // let's set the protocol mode on prph to Boot Mode.
155
+ #if DEVICE_TYPE == 0
141
156
hid.setBootMode (true );
157
+ #endif
142
158
143
159
// Enable Keyboard report notification if present on prph
144
160
if ( hid.keyboardPresent () ) hid.enableKeyboard ();
145
161
146
162
// Enable Mouse report notification if present on prph
147
163
if ( hid.mousePresent () ) hid.enableMouse ();
148
164
165
+ // Enable Gamepad report notification if present on prph
166
+ if ( hid.gamepadPresent () ) hid.enableGamepad ();
167
+
149
168
Serial.println (" Ready to receive from peripheral" );
150
169
}
151
170
}
@@ -169,15 +188,26 @@ void loop()
169
188
#if POLLING == 1
170
189
// nothing to do if hid not discovered
171
190
if ( !hid.discovered () ) return ;
172
-
173
- /* ------------- Polling Keyboard -------------*/
174
- hid_keyboard_report_t kbd_report;
175
-
176
- // Get latest report
177
- hid.getKeyboardReport (&kbd_report);
178
191
179
- processKeyboardReport (&kbd_report);
192
+ if ( hid.keyboardPresent () ) {
193
+ /* ------------- Polling Keyboard -------------*/
194
+ hid_keyboard_report_t kbd_report;
195
+
196
+ // Get latest report
197
+ Serial.println (" Get report from Keyboard" );
198
+ hid.getKeyboardReport (&kbd_report);
199
+ processKeyboardReport (&kbd_report);
200
+ }
180
201
202
+ if ( hid.gamepadPresent () ) {
203
+ /* ------------- Polling Gamepad -------------*/
204
+ hid_gamepad_report_t gpd_report;
205
+
206
+ // Get latest report
207
+ Serial.println (" Get report from Gamepad" );
208
+ hid.getGamepadReport (&gpd_report);
209
+ processGamepadReport (&gpd_report);
210
+ }
181
211
182
212
// polling interval is 5 ms
183
213
delay (5 );
@@ -186,6 +216,24 @@ void loop()
186
216
}
187
217
188
218
219
+ void gamepad_report_callback (hid_gamepad_report_t * report)
220
+ {
221
+ Serial.println (" Recieved report from Gamepad" );
222
+ processGamepadReport (report);
223
+ }
224
+
225
+ void processGamepadReport (hid_gamepad_report_t * report)
226
+ {
227
+ memcpy (&last_gpd_report, report, sizeof (hid_gamepad_report_t ));
228
+
229
+ Serial.print (" Current Gamepad State: Buttons: " );
230
+ Serial.print (report->buttons , BIN);
231
+ Serial.print (" X: " );
232
+ Serial.print (report->x );
233
+ Serial.print (" Y: " );
234
+ Serial.println (report->y );
235
+ }
236
+
189
237
void keyboard_report_callback (hid_keyboard_report_t * report)
190
238
{
191
239
processKeyboardReport (report);
0 commit comments