Skip to content

Commit b3b3fe1

Browse files
committed
Add gamepad functionality to BLEClientHidAdafruit
Has a couple issues that still need to be resolved in BLEClientHidAdafruit.cpp line 123 and line 138/139
1 parent 10ed826 commit b3b3fe1

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

libraries/Bluefruit52Lib/src/clients/BLEClientHidAdafruit.cpp

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ BLEClientHidAdafruit::BLEClientHidAdafruit(void)
4141
_protcol_mode(UUID16_CHR_PROTOCOL_MODE),
4242
_hid_info(UUID16_CHR_HID_INFORMATION), _hid_control(UUID16_CHR_HID_CONTROL_POINT),
4343
_kbd_boot_input(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT), _kbd_boot_output(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT),
44-
_mse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT)
44+
_mse_boot_input(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT),
45+
_gpd_report(UUID16_CHR_REPORT)
4546
{
4647
_kbd_cb = NULL;
4748
_mse_cb = NULL;
49+
_gpd_cb = NULL;
4850
varclr(&_last_kbd_report);
4951
varclr(&_last_mse_report);
52+
varclr(&_last_gpd_report);
5053
}
5154

5255

@@ -62,6 +65,12 @@ void mse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t
6265
svc._handle_mse_input(data, len);
6366
}
6467

68+
void gpd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len)
69+
{
70+
BLEClientHidAdafruit& svc = (BLEClientHidAdafruit&) chr->parentService();
71+
svc._handle_gpd_input(data, len);
72+
}
73+
6574

6675
bool BLEClientHidAdafruit::begin(void)
6776
{
@@ -77,10 +86,13 @@ bool BLEClientHidAdafruit::begin(void)
7786

7887
_mse_boot_input.begin(this);
7988

89+
_gpd_report.begin(this);
90+
8091

8192
// set notify callback
8293
_kbd_boot_input.setNotifyCallback(kbd_client_notify_cb);
8394
_mse_boot_input.setNotifyCallback(mse_client_notify_cb);
95+
_gpd_report.setNotifyCallback(gpd_client_notify_cb);
8496

8597
return true;
8698
}
@@ -95,17 +107,36 @@ void BLEClientHidAdafruit::setMouseReportCallback(mse_callback_t fp)
95107
_mse_cb = fp;
96108
}
97109

110+
void BLEClientHidAdafruit::setGamepadReportCallback(gpd_callback_t fp)
111+
{
112+
_gpd_cb = fp;
113+
}
114+
115+
98116
bool BLEClientHidAdafruit::discover(uint16_t conn_handle)
99117
{
100118
// Call Base class discover
101119
VERIFY( BLEClientService::discover(conn_handle) );
102120
_conn_hdl = BLE_CONN_HANDLE_INVALID; // make as invalid until we found all chars
103121

104122
// Discover all characteristics
105-
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _mse_boot_input, _hid_info, _hid_control);
123+
Bluefruit.Discovery.discoverCharacteristic(conn_handle, _protcol_mode, _kbd_boot_input, _kbd_boot_output, _hid_info, _hid_control, _gpd_report);
124+
125+
Serial.print("_protcol_mode.discovered(): ");
126+
Serial.println(_protcol_mode.discovered());
127+
128+
Serial.print("_hid_info.discovered(): ");
129+
Serial.println(_hid_info.discovered());
130+
131+
Serial.print("_hid_control.discovered(): ");
132+
Serial.println(_hid_control.discovered());
106133

107-
VERIFY( _protcol_mode.discovered() && _hid_info.discovered() && _hid_control.discovered() );
108-
VERIFY ( keyboardPresent() || mousePresent() );
134+
Serial.print("_gpd_report.discovered(): ");
135+
Serial.println(_gpd_report.discovered());
136+
137+
138+
VERIFY( /*_protcol_mode.discovered() &&*/ _hid_info.discovered() && _hid_control.discovered() );
139+
// VERIFY ( keyboardPresent() || mousePresent() || gamepadPresent() );
109140

110141
_conn_hdl = conn_handle;
111142
return true;
@@ -195,3 +226,34 @@ void BLEClientHidAdafruit::getMouseReport(hid_mouse_report_t* report)
195226
memcpy(report, &_last_mse_report, sizeof(hid_mouse_report_t));
196227
}
197228

229+
/*------------------------------------------------------------------*/
230+
/* Gamepad
231+
*------------------------------------------------------------------*/
232+
bool BLEClientHidAdafruit::gamepadPresent(void)
233+
{
234+
return _gpd_report.discovered();
235+
}
236+
237+
bool BLEClientHidAdafruit::enableGamepad(void)
238+
{
239+
return _gpd_report.enableNotify();
240+
}
241+
242+
bool BLEClientHidAdafruit::disableGamepad(void)
243+
{
244+
return _gpd_report.disableNotify();
245+
}
246+
247+
void BLEClientHidAdafruit::_handle_gpd_input(uint8_t* data, uint16_t len)
248+
{
249+
varclr(&_last_gpd_report);
250+
memcpy(&_last_gpd_report, data, len);
251+
252+
if ( _gpd_cb ) _gpd_cb(&_last_gpd_report);
253+
}
254+
255+
void BLEClientHidAdafruit::getGamepadReport(hid_gamepad_report_t* report)
256+
{
257+
memcpy(report, &_last_gpd_report, sizeof(hid_gamepad_report_t));
258+
}
259+

libraries/Bluefruit52Lib/src/clients/BLEClientHidAdafruit.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class BLEClientHidAdafruit : public BLEClientService
4949
// Callback Signatures
5050
typedef void (*kbd_callback_t ) (hid_keyboard_report_t* report);
5151
typedef void (*mse_callback_t ) (hid_mouse_report_t* report);
52+
typedef void (*gpd_callback_t ) (hid_gamepad_report_t* report);
5253

5354
BLEClientHidAdafruit(void);
5455

@@ -74,16 +75,26 @@ class BLEClientHidAdafruit : public BLEClientService
7475

7576
void getMouseReport(hid_mouse_report_t* report);
7677

78+
// Gamepad API
79+
bool gamepadPresent(void);
80+
bool enableGamepad(void);
81+
bool disableGamepad(void);
82+
83+
void getGamepadReport(hid_gamepad_report_t* report);
84+
7785
// Report callback
7886
void setKeyboardReportCallback(kbd_callback_t fp);
7987
void setMouseReportCallback(mse_callback_t fp);
88+
void setGamepadReportCallback(gpd_callback_t fp);
8089

8190
protected:
8291
kbd_callback_t _kbd_cb;
8392
mse_callback_t _mse_cb;
93+
gpd_callback_t _gpd_cb;
8494

8595
hid_keyboard_report_t _last_kbd_report;
8696
hid_mouse_report_t _last_mse_report;
97+
hid_gamepad_report_t _last_gpd_report;
8798

8899
// Only support Boot protocol for keyboard and Mouse
89100
BLEClientCharacteristic _protcol_mode;
@@ -95,11 +106,15 @@ class BLEClientHidAdafruit : public BLEClientService
95106

96107
BLEClientCharacteristic _mse_boot_input;
97108

109+
BLEClientCharacteristic _gpd_report;
110+
98111
void _handle_kbd_input(uint8_t* data, uint16_t len);
99112
void _handle_mse_input(uint8_t* data, uint16_t len);
113+
void _handle_gpd_input(uint8_t* data, uint16_t len);
100114

101115
friend void kbd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
102116
friend void mse_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
117+
friend void gpd_client_notify_cb(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len);
103118
};
104119

105120

0 commit comments

Comments
 (0)