Add Gamepad implementation to Central HID Example
This commit is contained in:
parent
459f7cef16
commit
0f30241ed5
1 changed files with 60 additions and 12 deletions
|
|
@ -18,21 +18,33 @@
|
|||
*/
|
||||
#include <bluefruit.h>
|
||||
|
||||
// Polling or callback implementation
|
||||
#define POLLING 1
|
||||
/*
|
||||
* Polling or callback implementation
|
||||
* 0 = Request data using a poll
|
||||
* 1 = Use callbacks
|
||||
*/
|
||||
#define POLLING 0
|
||||
/*
|
||||
* Device Type flag for use in the example
|
||||
* Change this if you want to demo the Gamepad!
|
||||
* 0 = Keyboard + Mouse
|
||||
* 1 = Gamepad
|
||||
*/
|
||||
#define DEVICE_TYPE 0
|
||||
|
||||
BLEClientHidAdafruit hid;
|
||||
|
||||
// Last checked report, to detect if there is changes between reports
|
||||
hid_keyboard_report_t last_kbd_report = { 0 };
|
||||
hid_mouse_report_t last_mse_report = { 0 };
|
||||
hid_gamepad_report_t last_gpd_report = { 0 };
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
// while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central HID (Keyboard + Mouse) Example");
|
||||
Serial.println("Bluefruit52 Central HID (Keyboard + Mouse + Gamepad) Example");
|
||||
Serial.println("--------------------------------------------------\n");
|
||||
|
||||
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
|
||||
|
|
@ -46,6 +58,7 @@ void setup()
|
|||
|
||||
#if POLLING == 0
|
||||
hid.setKeyboardReportCallback(keyboard_report_callback);
|
||||
hid.setGamepadReportCallback(gamepad_report_callback);
|
||||
#endif
|
||||
|
||||
// Increase Blink rate to different from PrPh advertising mode
|
||||
|
|
@ -69,7 +82,7 @@ void setup()
|
|||
Bluefruit.Scanner.restartOnDisconnect(true);
|
||||
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
|
||||
Bluefruit.Scanner.filterService(hid); // only report HID service
|
||||
Bluefruit.Scanner.useActiveScan(false);
|
||||
Bluefruit.Scanner.useActiveScan(true);
|
||||
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
|
||||
}
|
||||
|
||||
|
|
@ -137,8 +150,11 @@ void connection_secured_callback(uint16_t conn_handle)
|
|||
Serial.printf("HID Flags : 0x%02X\n", hidInfo[3]);
|
||||
|
||||
// BLEClientHidAdafruit currently only supports Boot Protocol Mode
|
||||
// for Keyboard and Mouse. Let's set the protocol mode on prph to Boot Mode
|
||||
// for Keyboard and Mouse. If we are using a Keyboard + Mouse,
|
||||
// let's set the protocol mode on prph to Boot Mode.
|
||||
#if DEVICE_TYPE == 0
|
||||
hid.setBootMode(true);
|
||||
#endif
|
||||
|
||||
// Enable Keyboard report notification if present on prph
|
||||
if ( hid.keyboardPresent() ) hid.enableKeyboard();
|
||||
|
|
@ -146,6 +162,9 @@ void connection_secured_callback(uint16_t conn_handle)
|
|||
// Enable Mouse report notification if present on prph
|
||||
if ( hid.mousePresent() ) hid.enableMouse();
|
||||
|
||||
// Enable Gamepad report notification if present on prph
|
||||
if ( hid.gamepadPresent() ) hid.enableGamepad();
|
||||
|
||||
Serial.println("Ready to receive from peripheral");
|
||||
}
|
||||
}
|
||||
|
|
@ -169,15 +188,26 @@ void loop()
|
|||
#if POLLING == 1
|
||||
// nothing to do if hid not discovered
|
||||
if ( !hid.discovered() ) return;
|
||||
|
||||
if ( hid.keyboardPresent() ) {
|
||||
/*------------- Polling Keyboard -------------*/
|
||||
hid_keyboard_report_t kbd_report;
|
||||
|
||||
/*------------- Polling Keyboard -------------*/
|
||||
hid_keyboard_report_t kbd_report;
|
||||
|
||||
// Get latest report
|
||||
hid.getKeyboardReport(&kbd_report);
|
||||
|
||||
processKeyboardReport(&kbd_report);
|
||||
// Get latest report
|
||||
Serial.println("Get report from Keyboard");
|
||||
hid.getKeyboardReport(&kbd_report);
|
||||
processKeyboardReport(&kbd_report);
|
||||
}
|
||||
|
||||
if ( hid.gamepadPresent() ) {
|
||||
/*------------- Polling Gamepad -------------*/
|
||||
hid_gamepad_report_t gpd_report;
|
||||
|
||||
// Get latest report
|
||||
Serial.println("Get report from Gamepad");
|
||||
hid.getGamepadReport(&gpd_report);
|
||||
processGamepadReport(&gpd_report);
|
||||
}
|
||||
|
||||
// polling interval is 5 ms
|
||||
delay(5);
|
||||
|
|
@ -186,6 +216,24 @@ void loop()
|
|||
}
|
||||
|
||||
|
||||
void gamepad_report_callback(hid_gamepad_report_t* report)
|
||||
{
|
||||
Serial.println("Recieved report from Gamepad");
|
||||
processGamepadReport(report);
|
||||
}
|
||||
|
||||
void processGamepadReport(hid_gamepad_report_t* report)
|
||||
{
|
||||
memcpy(&last_gpd_report, report, sizeof(hid_gamepad_report_t));
|
||||
|
||||
Serial.print("Current Gamepad State: Buttons: ");
|
||||
Serial.print(report->buttons, BIN);
|
||||
Serial.print(" X: ");
|
||||
Serial.print(report->x);
|
||||
Serial.print(" Y: ");
|
||||
Serial.println(report->y);
|
||||
}
|
||||
|
||||
void keyboard_report_callback(hid_keyboard_report_t* report)
|
||||
{
|
||||
processKeyboardReport(report);
|
||||
|
|
|
|||
Loading…
Reference in a new issue