1818 */
1919#include < bluefruit.h>
2020
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
2334
2435BLEClientHidAdafruit hid;
2536
2637// Last checked report, to detect if there is changes between reports
2738hid_keyboard_report_t last_kbd_report = { 0 };
2839hid_mouse_report_t last_mse_report = { 0 };
40+ hid_gamepad_report_t last_gpd_report = { 0 };
2941
3042void setup ()
3143{
3244 Serial.begin (115200 );
3345// while ( !Serial ) delay(10); // for nrf52840 with native usb
3446
35- Serial.println (" Bluefruit52 Central HID (Keyboard + Mouse) Example" );
47+ Serial.println (" Bluefruit52 Central HID (Keyboard + Mouse + Gamepad ) Example" );
3648 Serial.println (" --------------------------------------------------\n " );
3749
3850 // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
@@ -46,6 +58,7 @@ void setup()
4658
4759 #if POLLING == 0
4860 hid.setKeyboardReportCallback (keyboard_report_callback);
61+ hid.setGamepadReportCallback (gamepad_report_callback);
4962 #endif
5063
5164 // Increase Blink rate to different from PrPh advertising mode
@@ -69,7 +82,7 @@ void setup()
6982 Bluefruit.Scanner .restartOnDisconnect (true );
7083 Bluefruit.Scanner .setInterval (160 , 80 ); // in unit of 0.625 ms
7184 Bluefruit.Scanner .filterService (hid); // only report HID service
72- Bluefruit.Scanner .useActiveScan (false );
85+ Bluefruit.Scanner .useActiveScan (true );
7386 Bluefruit.Scanner .start (0 ); // 0 = Don't stop scanning after n seconds
7487}
7588
@@ -137,15 +150,21 @@ void connection_secured_callback(uint16_t conn_handle)
137150 Serial.printf (" HID Flags : 0x%02X\n " , hidInfo[3 ]);
138151
139152 // 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
141156 hid.setBootMode (true );
157+ #endif
142158
143159 // Enable Keyboard report notification if present on prph
144160 if ( hid.keyboardPresent () ) hid.enableKeyboard ();
145161
146162 // Enable Mouse report notification if present on prph
147163 if ( hid.mousePresent () ) hid.enableMouse ();
148164
165+ // Enable Gamepad report notification if present on prph
166+ if ( hid.gamepadPresent () ) hid.enableGamepad ();
167+
149168 Serial.println (" Ready to receive from peripheral" );
150169 }
151170}
@@ -169,15 +188,26 @@ void loop()
169188#if POLLING == 1
170189 // nothing to do if hid not discovered
171190 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);
178191
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+ }
180201
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+ }
181211
182212 // polling interval is 5 ms
183213 delay (5 );
@@ -186,6 +216,24 @@ void loop()
186216}
187217
188218
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+
189237void keyboard_report_callback (hid_keyboard_report_t * report)
190238{
191239 processKeyboardReport (report);
0 commit comments