Skip to content

Commit eb3beca

Browse files
committed
added BLEHidGamepad and blehid_gamepad example
1 parent d1564be commit eb3beca

File tree

6 files changed

+476
-6
lines changed

6 files changed

+476
-6
lines changed
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
/*********************************************************************
2+
This is an example for our nRF52 based Bluefruit LE modules
3+
4+
Pick one up today in the adafruit shop!
5+
6+
Adafruit invests time and resources providing this open source code,
7+
please support Adafruit and open-source hardware by purchasing
8+
products from Adafruit!
9+
10+
MIT license, check LICENSE for more information
11+
All text above, and the splash screen below must be included in
12+
any redistribution
13+
*********************************************************************/
14+
15+
/* This sketch demonstrate how to use BLEHidGamepad to send
16+
* button, dpad, and joystick report
17+
*/
18+
19+
#include <bluefruit.h>
20+
21+
BLEDis bledis;
22+
BLEHidGamepad blegamepad;
23+
24+
// defined in hid.h from Adafruit_TinyUSB_Arduino
25+
hid_gamepad_report_t gp;
26+
27+
void setup()
28+
{
29+
Serial.begin(115200);
30+
31+
#if CFG_DEBUG
32+
// Blocking wait for connection when debug mode is enabled via IDE
33+
while ( !Serial ) delay(10);
34+
#endif
35+
36+
Serial.println("Bluefruit52 HID Gamepad Example");
37+
Serial.println("-------------------------------\n");
38+
39+
Serial.println();
40+
Serial.println("Go to your phone's Bluetooth settings to pair your device");
41+
Serial.println("then open an application that accepts gamepad input");
42+
43+
Bluefruit.begin();
44+
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
45+
46+
// Configure and Start Device Information Service
47+
bledis.setManufacturer("Adafruit Industries");
48+
bledis.setModel("Bluefruit Feather 52");
49+
bledis.begin();
50+
51+
/* Start BLE HID
52+
* Note: Apple requires BLE device must have min connection interval >= 20m
53+
* ( The smaller the connection interval the faster we could send data).
54+
* However for HID and MIDI device, Apple could accept min connection interval
55+
* up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max
56+
* connection interval to 11.25 ms and 15 ms respectively for best performance.
57+
*/
58+
blegamepad.begin();
59+
60+
/* Set connection interval (min, max) to your perferred value.
61+
* Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms
62+
* min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms
63+
*/
64+
/* Bluefruit.Periph.setConnInterval(9, 12); */
65+
66+
// Set up and start advertising
67+
startAdv();
68+
}
69+
70+
void startAdv(void)
71+
{
72+
// Advertising packet
73+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
74+
Bluefruit.Advertising.addTxPower();
75+
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_GAMEPAD);
76+
77+
// Include BLE HID service
78+
Bluefruit.Advertising.addService(blegamepad);
79+
80+
// There is enough room for the dev name in the advertising packet
81+
Bluefruit.Advertising.addName();
82+
83+
/* Start Advertising
84+
* - Enable auto advertising if disconnected
85+
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
86+
* - Timeout for fast mode is 30 seconds
87+
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
88+
*
89+
* For recommended advertising interval
90+
* https://developer.apple.com/library/content/qa/qa1931/_index.html
91+
*/
92+
Bluefruit.Advertising.restartOnDisconnect(true);
93+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
94+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
95+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
96+
}
97+
98+
void loop()
99+
{
100+
// nothing to do if not connected or
101+
if ( !Bluefruit.connected() ) return;
102+
103+
Serial.println("No pressing buttons");
104+
gp.x = 0;
105+
gp.y = 0;
106+
gp.z = 0;
107+
gp.rz = 0;
108+
gp.rx = 0;
109+
gp.ry = 0;
110+
gp.hat = 0;
111+
gp.buttons = 0;
112+
blegamepad.report(&gp);
113+
delay(1000);
114+
115+
//------------- DPAD / HAT -------------//
116+
Serial.println("Hat/DPAD UP");
117+
gp.hat = GAMEPAD_HAT_UP;
118+
blegamepad.report(&gp);
119+
delay(1000);
120+
121+
Serial.println("Hat/DPAD UP RIGHT");
122+
gp.hat = GAMEPAD_HAT_UP_RIGHT;
123+
blegamepad.report(&gp);
124+
delay(1000);
125+
126+
Serial.println("Hat/DPAD RIGHT");
127+
gp.hat = GAMEPAD_HAT_RIGHT;
128+
blegamepad.report(&gp);
129+
delay(1000);
130+
131+
Serial.println("Hat/DPAD DOWN RIGHT");
132+
gp.hat = GAMEPAD_HAT_DOWN_RIGHT;
133+
blegamepad.report(&gp);
134+
delay(1000);
135+
136+
Serial.println("Hat/DPAD DOWN");
137+
gp.hat = GAMEPAD_HAT_DOWN;
138+
blegamepad.report(&gp);
139+
delay(1000);
140+
141+
Serial.println("Hat/DPAD DOWN LEFT");
142+
gp.hat = GAMEPAD_HAT_DOWN_LEFT;
143+
blegamepad.report(&gp);
144+
delay(1000);
145+
146+
Serial.println("Hat/DPAD LEFT");
147+
gp.hat = GAMEPAD_HAT_LEFT;
148+
blegamepad.report(&gp);
149+
delay(1000);
150+
151+
Serial.println("Hat/DPAD UP LEFT");
152+
gp.hat = GAMEPAD_HAT_UP_LEFT;
153+
blegamepad.report(&gp);
154+
delay(1000);
155+
156+
Serial.println("Hat/DPAD CENTER");
157+
gp.hat = GAMEPAD_HAT_CENTERED;
158+
blegamepad.report(&gp);
159+
delay(1000);
160+
161+
//------------- Joystick 1 -------------//
162+
163+
Serial.println("Joystick 1 UP");
164+
gp.x = 0;
165+
gp.y = -127;
166+
blegamepad.report(&gp);
167+
delay(1000);
168+
169+
Serial.println("Joystick 1 DOWN");
170+
gp.x = 0;
171+
gp.y = 127;
172+
blegamepad.report(&gp);
173+
delay(1000);
174+
175+
Serial.println("Joystick 1 RIGHT");
176+
gp.x = 127;
177+
gp.y = 0;
178+
blegamepad.report(&gp);
179+
delay(1000);
180+
181+
Serial.println("Joystick 1 LEFT");
182+
gp.x = -127;
183+
gp.y = 0;
184+
blegamepad.report(&gp);
185+
delay(1000);
186+
187+
Serial.println("Joystick 1 CENTER");
188+
gp.x = 0;
189+
gp.y = 0;
190+
blegamepad.report(&gp);
191+
delay(1000);
192+
193+
194+
//------------- Joystick 2 -------------//
195+
Serial.println("Joystick 2 UP");
196+
gp.z = 0;
197+
gp.rz = 127;
198+
blegamepad.report(&gp);
199+
delay(1000);
200+
201+
Serial.println("Joystick 2 DOWN");
202+
gp.z = 0;
203+
gp.rz = -127;
204+
blegamepad.report(&gp);
205+
delay(1000);
206+
207+
Serial.println("Joystick 2 RIGHT");
208+
gp.z = 127;
209+
gp.rz = 0;
210+
blegamepad.report(&gp);
211+
delay(1000);
212+
213+
Serial.println("Joystick 2 LEFT");
214+
gp.z = -127;
215+
gp.rz = 0;
216+
blegamepad.report(&gp);
217+
delay(1000);
218+
219+
Serial.println("Joystick 2 CENTER");
220+
gp.z = 0;
221+
gp.rz = 0;
222+
blegamepad.report(&gp);
223+
delay(1000);
224+
225+
//------------- Analog Trigger 1 -------------//
226+
Serial.println("Analog Trigger 1 UP");
227+
gp.rx = 127;
228+
blegamepad.report(&gp);
229+
delay(1000);
230+
231+
Serial.println("Analog Trigger 1 DOWN");
232+
gp.rx = -127;
233+
blegamepad.report(&gp);
234+
delay(1000);
235+
236+
Serial.println("Analog Trigger 1 CENTER");
237+
gp.rx = 0;
238+
blegamepad.report(&gp);
239+
delay(1000);
240+
241+
//------------- Analog Trigger 2 -------------//
242+
Serial.println("Analog Trigger 2 UP");
243+
gp.ry = 127;
244+
blegamepad.report(&gp);
245+
delay(1000);
246+
247+
Serial.println("Analog Trigger 2 DOWN");
248+
gp.ry = -127;
249+
blegamepad.report(&gp);
250+
delay(1000);
251+
252+
Serial.println("Analog Trigger 2 CENTER");
253+
gp.ry = 0;
254+
blegamepad.report(&gp);
255+
delay(1000);
256+
257+
//------------- Buttons -------------//
258+
for (int i=0; i<16; ++i)
259+
{
260+
Serial.print("Pressing button "); Serial.println(i);
261+
gp.buttons = (1ul << i);
262+
blegamepad.report(&gp);
263+
delay(1000);
264+
}
265+
266+
// Random touch
267+
Serial.println("Random touch");
268+
gp.x = random(-127, 128);
269+
gp.y = random(-127, 128);
270+
gp.z = random(-127, 128);
271+
gp.rz = random(-127, 128);
272+
gp.rx = random(-127, 128);
273+
gp.ry = random(-127, 128);
274+
gp.hat = random(0, 9);
275+
gp.buttons = random(0, 0xffff);
276+
blegamepad.report(&gp);
277+
delay(1000);
278+
}

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "services/BLEBeacon.h"
6666
#include "services/BLEHidGeneric.h"
6767
#include "services/BLEHidAdafruit.h"
68+
#include "services/BLEHidGamepad.h"
6869
#include "services/BLEMidi.h"
6970
#include "services/EddyStone.h"
7071

libraries/Bluefruit52Lib/src/services/BLEHidAdafruit.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ enum
4343
{
4444
REPORT_ID_KEYBOARD = 1,
4545
REPORT_ID_CONSUMER_CONTROL,
46-
REPORT_ID_MOUSE,
47-
REPORT_ID_GAMEPAD
46+
REPORT_ID_MOUSE
4847
};
4948

5049
uint8_t const hid_report_descriptor[] =

0 commit comments

Comments
 (0)