Merge pull request #730 from OstlerDev/feat_BLEClientHid_Gamepad
HID Gamepad Client & Central HID Gamepad example
This commit is contained in:
5 files changed
+146
-18
No files matched your search
@@ -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);
|
||||
|
||||
@@ -240,6 +240,10 @@ void BLEDiscovery::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
LOG_LV2("DISC", "[CHR] Characteristic Count: %d", chr_rsp->count);
|
||||
|
||||
for (uint8_t i; i < chr_rsp->count; i++) {
|
||||
LOG_LV2("DISC", "[CHR] Characteristic #%d: 0x%04X", i, chr_rsp->chars[i].uuid.uuid);
|
||||
}
|
||||
|
||||
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS)
|
||||
{
|
||||
if ( chr_rsp->count )
|
||||
|
||||
@@ -101,6 +101,18 @@ class BLEDiscovery
|
||||
return discoverCharacteristic(conn_handle, chr_arr, arrcount(chr_arr));
|
||||
}
|
||||
|
||||
uint8_t discoverCharacteristic(uint16_t conn_handle, BLEClientCharacteristic& chr1, BLEClientCharacteristic& chr2, BLEClientCharacteristic& chr3, BLEClientCharacteristic& chr4, BLEClientCharacteristic& chr5, BLEClientCharacteristic& chr6, BLEClientCharacteristic& chr7)
|
||||
{
|
||||
BLEClientCharacteristic* chr_arr[] = {&chr1, &chr2, &chr3, &chr4, &chr5, &chr6, &chr7};
|
||||
return discoverCharacteristic(conn_handle, chr_arr, arrcount(chr_arr));
|
||||
}
|
||||
|
||||
uint8_t discoverCharacteristic(uint16_t conn_handle, BLEClientCharacteristic& chr1, BLEClientCharacteristic& chr2, BLEClientCharacteristic& chr3, BLEClientCharacteristic& chr4, BLEClientCharacteristic& chr5, BLEClientCharacteristic& chr6, BLEClientCharacteristic& chr7, BLEClientCharacteristic& chr8)
|
||||
{
|
||||
BLEClientCharacteristic* chr_arr[] = {&chr1, &chr2, &chr3, &chr4, &chr5, &chr6, &chr7, &chr8};
|
||||
return discoverCharacteristic(conn_handle, chr_arr, arrcount(chr_arr));
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
|
||||
@@ -41,12 +41,15 @@ BLEClientHidAdafruit::BLEClientHidAdafruit(void)
|
||||
_protcol_mode(UUID16_CHR_PROTOCOL_MODE),
|
||||
_hid_info(UUID16_CHR_HID_INFORMATION), _hid_control(UUID16_CHR_HID_CONTROL_POINT),
|
||||
_kbd_boot_input(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT), _kbd_boot_output(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT),
|
||||
_mse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT)
|
||||
_mse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
|
||||
_gpd_report(UUID16_CHR_REPORT)
|
||||
{
|
||||
_kbd_cb = NULL;
|
||||
_mse_cb = NULL;
|
||||
_gpd_cb = NULL;
|
||||
varclr(&_last_kbd_report);
|
||||
varclr(&_last_mse_report);
|
||||
varclr(&_last_gpd_report);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,6 +65,12 @@ void mse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t
|
||||
svc._handle_mse_input(data, len);
|
||||
}
|
||||
|
||||
void gpd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEClientHidAdafruit& svc = (BLEClientHidAdafruit&) chr->parentService();
|
||||
svc._handle_gpd_input(data, len);
|
||||
}
|
||||
|
||||
|
||||
bool BLEClientHidAdafruit::begin(void)
|
||||
{
|
||||
@@ -77,10 +86,13 @@ bool BLEClientHidAdafruit::begin(void)
|
||||
|
||||
_mse_boot_input.begin(this);
|
||||
|
||||
_gpd_report.begin(this);
|
||||
|
||||
|
||||
// set notify callback
|
||||
_kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
|
||||
_mse_boot_input.setNotifyCallback(mse_client_notify_cb);
|
||||
_gpd_report.setNotifyCallback(gpd_client_notify_cb);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -95,6 +107,12 @@ void BLEClientHidAdafruit::setMouseReportCallback(mse_callback_t fp)
|
||||
_mse_cb = fp;
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::setGamepadReportCallback(gpd_callback_t fp)
|
||||
{
|
||||
_gpd_cb = fp;
|
||||
}
|
||||
|
||||
|
||||
bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
|
||||
{
|
||||
// Call Base class discover
|
||||
@@ -102,10 +120,10 @@ bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID; // make as invalid until we found all chars
|
||||
|
||||
// Discover all characteristics
|
||||
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mse_boot_input, _hid_info, _hid_control);
|
||||
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mse_boot_input, _hid_info, _hid_control, _gpd_report);
|
||||
|
||||
VERIFY( _protcol_mode.discovered() && _hid_info.discovered() && _hid_control.discovered() );
|
||||
VERIFY ( keyboardPresent() || mousePresent() );
|
||||
VERIFY( _hid_info.discovered() && _hid_control.discovered() );
|
||||
VERIFY (keyboardPresent() || mousePresent() || gamepadPresent());
|
||||
|
||||
_conn_hdl = conn_handle;
|
||||
return true;
|
||||
@@ -138,7 +156,7 @@ bool BLEClientHidAdafruit::setBootMode(bool boot)
|
||||
*------------------------------------------------------------------*/
|
||||
bool BLEClientHidAdafruit::keyboardPresent(void)
|
||||
{
|
||||
return _kbd_boot_input.discovered() && _kbd_boot_output.discovered();
|
||||
return _kbd_boot_input.discovered() && _kbd_boot_output.discovered() && _protcol_mode.discovered();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::enableKeyboard(void)
|
||||
@@ -169,7 +187,7 @@ void BLEClientHidAdafruit::getKeyboardReport(hid_keyboard_report_t* report)
|
||||
*------------------------------------------------------------------*/
|
||||
bool BLEClientHidAdafruit::mousePresent(void)
|
||||
{
|
||||
return _mse_boot_input.discovered();
|
||||
return _mse_boot_input.discovered() && _protcol_mode.discovered();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::enableMouse(void)
|
||||
@@ -195,3 +213,34 @@ void BLEClientHidAdafruit::getMouseReport(hid_mouse_report_t* report)
|
||||
memcpy(report, &_last_mse_report, sizeof(hid_mouse_report_t));
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Gamepad
|
||||
*------------------------------------------------------------------*/
|
||||
bool BLEClientHidAdafruit::gamepadPresent(void)
|
||||
{
|
||||
return _gpd_report.discovered();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::enableGamepad(void)
|
||||
{
|
||||
return _gpd_report.enableNotify();
|
||||
}
|
||||
|
||||
bool BLEClientHidAdafruit::disableGamepad(void)
|
||||
{
|
||||
return _gpd_report.disableNotify();
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::_handle_gpd_input(uint8_t* data, uint16_t len)
|
||||
{
|
||||
varclr(&_last_gpd_report);
|
||||
memcpy(&_last_gpd_report, data, len);
|
||||
|
||||
if ( _gpd_cb ) _gpd_cb(&_last_gpd_report);
|
||||
}
|
||||
|
||||
void BLEClientHidAdafruit::getGamepadReport(hid_gamepad_report_t* report)
|
||||
{
|
||||
memcpy(report, &_last_gpd_report, sizeof(hid_gamepad_report_t));
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ class BLEClientHidAdafruit : public BLEClientService
|
||||
// Callback Signatures
|
||||
typedef void (*kbd_callback_t ) (hid_keyboard_report_t* report);
|
||||
typedef void (*mse_callback_t ) (hid_mouse_report_t* report);
|
||||
typedef void (*gpd_callback_t ) (hid_gamepad_report_t* report);
|
||||
|
||||
BLEClientHidAdafruit(void);
|
||||
|
||||
@@ -74,16 +75,26 @@ class BLEClientHidAdafruit : public BLEClientService
|
||||
|
||||
void getMouseReport(hid_mouse_report_t* report);
|
||||
|
||||
// Gamepad API
|
||||
bool gamepadPresent(void);
|
||||
bool enableGamepad(void);
|
||||
bool disableGamepad(void);
|
||||
|
||||
void getGamepadReport(hid_gamepad_report_t* report);
|
||||
|
||||
// Report callback
|
||||
void setKeyboardReportCallback(kbd_callback_t fp);
|
||||
void setMouseReportCallback(mse_callback_t fp);
|
||||
void setGamepadReportCallback(gpd_callback_t fp);
|
||||
|
||||
protected:
|
||||
kbd_callback_t _kbd_cb;
|
||||
mse_callback_t _mse_cb;
|
||||
gpd_callback_t _gpd_cb;
|
||||
|
||||
hid_keyboard_report_t _last_kbd_report;
|
||||
hid_mouse_report_t _last_mse_report;
|
||||
hid_gamepad_report_t _last_gpd_report;
|
||||
|
||||
// Only support Boot protocol for keyboard and Mouse
|
||||
BLEClientCharacteristic _protcol_mode;
|
||||
@@ -95,11 +106,15 @@ class BLEClientHidAdafruit : public BLEClientService
|
||||
|
||||
BLEClientCharacteristic _mse_boot_input;
|
||||
|
||||
BLEClientCharacteristic _gpd_report;
|
||||
|
||||
void _handle_kbd_input(uint8_t* data, uint16_t len);
|
||||
void _handle_mse_input(uint8_t* data, uint16_t len);
|
||||
void _handle_gpd_input(uint8_t* data, uint16_t len);
|
||||
|
||||
friend void kbd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
|
||||
friend void mse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
|
||||
friend void gpd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user