@@ -5,147 +5,158 @@ GamepadPtr myGamepads[BP32_MAX_GAMEPADS];
5
5
// This callback gets called any time a new gamepad is connected.
6
6
// Up to 4 gamepads can be connected at the same time.
7
7
void onConnectedGamepad (GamepadPtr gp) {
8
- bool foundEmptySlot = false ;
9
- for (int i = 0 ; i < BP32_MAX_GAMEPADS; i++) {
10
- if (myGamepads[i] == nullptr ) {
11
- Serial.printf (" CALLBACK: Gamepad is connected, index=%d\n " , i);
12
- // Additionally, you can get certain gamepad properties like:
13
- // Model, VID, PID, BTAddr, flags, etc.
14
- GamepadProperties properties = gp->getProperties ();
15
- Serial.printf (" Gamepad model: %s, VID=0x%04x, PID=0x%04x\n " , gp->getModelName ().c_str (), properties.vendor_id ,
16
- properties.product_id );
17
- myGamepads[i] = gp;
18
- foundEmptySlot = true ;
19
- break ;
20
- }
21
- }
22
- if (!foundEmptySlot) {
23
- Serial.println (" CALLBACK: Gamepad connected, but could not found empty slot" );
8
+ bool foundEmptySlot = false ;
9
+ for (int i = 0 ; i < BP32_MAX_GAMEPADS; i++) {
10
+ if (myGamepads[i] == nullptr ) {
11
+ Serial.printf (" CALLBACK: Gamepad is connected, index=%d\n " , i);
12
+ // Additionally, you can get certain gamepad properties like:
13
+ // Model, VID, PID, BTAddr, flags, etc.
14
+ GamepadProperties properties = gp->getProperties ();
15
+ Serial.printf (" Gamepad model: %s, VID=0x%04x, PID=0x%04x\n " ,
16
+ gp->getModelName ().c_str (), properties.vendor_id ,
17
+ properties.product_id );
18
+ myGamepads[i] = gp;
19
+ foundEmptySlot = true ;
20
+ break ;
24
21
}
22
+ }
23
+ if (!foundEmptySlot) {
24
+ Serial.println (
25
+ " CALLBACK: Gamepad connected, but could not found empty slot" );
26
+ }
25
27
}
26
28
27
29
void onDisconnectedGamepad (GamepadPtr gp) {
28
- bool foundGamepad = false ;
29
-
30
- for (int i = 0 ; i < BP32_MAX_GAMEPADS; i++) {
31
- if (myGamepads[i] == gp) {
32
- Serial.printf (" CALLBACK: Gamepad is disconnected from index=%d\n " , i);
33
- myGamepads[i] = nullptr ;
34
- foundGamepad = true ;
35
- break ;
36
- }
30
+ bool foundGamepad = false ;
31
+
32
+ for (int i = 0 ; i < BP32_MAX_GAMEPADS; i++) {
33
+ if (myGamepads[i] == gp) {
34
+ Serial.printf (" CALLBACK: Gamepad is disconnected from index=%d\n " , i);
35
+ myGamepads[i] = nullptr ;
36
+ foundGamepad = true ;
37
+ break ;
37
38
}
39
+ }
38
40
39
- if (!foundGamepad) {
40
- Serial.println (" CALLBACK: Gamepad disconnected, but not found in myGamepads" );
41
- }
41
+ if (!foundGamepad) {
42
+ Serial.println (
43
+ " CALLBACK: Gamepad disconnected, but not found in myGamepads" );
44
+ }
42
45
}
43
46
44
47
// Arduino setup function. Runs in CPU 1
45
48
void setup () {
46
- Serial.begin (115200 );
47
- Serial.printf (" Firmware: %s\n " , BP32.firmwareVersion ());
48
- const uint8_t * addr = BP32.localBdAddress ();
49
- Serial.printf (" BD Addr: %2X:%2X:%2X:%2X:%2X:%2X\n " , addr[0 ], addr[1 ], addr[2 ], addr[3 ], addr[4 ], addr[5 ]);
50
-
51
- // Setup the Bluepad32 callbacks
52
- BP32.setup (&onConnectedGamepad, &onDisconnectedGamepad);
53
-
54
- // "forgetBluetoothKeys()" should be called when the user performs
55
- // a "device factory reset", or similar.
56
- // Calling "forgetBluetoothKeys" in setup() just as an example.
57
- // Forgetting Bluetooth keys prevents "paired" gamepads to reconnect.
58
- // But might also fix some connection / re-connection issues.
59
- BP32.forgetBluetoothKeys ();
49
+ Serial.begin (115200 );
50
+ Serial.printf (" Firmware: %s\n " , BP32.firmwareVersion ());
51
+ const uint8_t *addr = BP32.localBdAddress ();
52
+ Serial.printf (" BD Addr: %2X:%2X:%2X:%2X:%2X:%2X\n " , addr[0 ], addr[1 ], addr[2 ],
53
+ addr[3 ], addr[4 ], addr[5 ]);
54
+
55
+ // Setup the Bluepad32 callbacks
56
+ BP32.setup (&onConnectedGamepad, &onDisconnectedGamepad);
57
+
58
+ // "forgetBluetoothKeys()" should be called when the user performs
59
+ // a "device factory reset", or similar.
60
+ // Calling "forgetBluetoothKeys" in setup() just as an example.
61
+ // Forgetting Bluetooth keys prevents "paired" gamepads to reconnect.
62
+ // But might also fix some connection / re-connection issues.
63
+ BP32.forgetBluetoothKeys ();
60
64
}
61
65
62
66
// Arduino loop function. Runs in CPU 1
63
67
void loop () {
64
- // This call fetches all the gamepad info from the NINA (ESP32) module.
65
- // Just call this function in your main loop.
66
- // The gamepads pointer (the ones received in the callbacks) gets updated
67
- // automatically.
68
- BP32.update ();
69
-
70
- // It is safe to always do this before using the gamepad API.
71
- // This guarantees that the gamepad is valid and connected.
72
- for (int i = 0 ; i < BP32_MAX_GAMEPADS; i++) {
73
- GamepadPtr myGamepad = myGamepads[i];
74
-
75
- if (myGamepad && myGamepad->isConnected ()) {
76
- // There are different ways to query whether a button is pressed.
77
- // By query each button individually:
78
- // a(), b(), x(), y(), l1(), etc...
79
- if (myGamepad->a ()) {
80
- static int colorIdx = 0 ;
81
- // Some gamepads like DS4 and DualSense support changing the color LED.
82
- // It is possible to change it by calling:
83
- switch (colorIdx % 3 ) {
84
- case 0 :
85
- // Red
86
- myGamepad->setColorLED (255 , 0 , 0 );
87
- break ;
88
- case 1 :
89
- // Green
90
- myGamepad->setColorLED (0 , 255 , 0 );
91
- break ;
92
- case 2 :
93
- // Blue
94
- myGamepad->setColorLED (0 , 0 , 255 );
95
- break ;
96
- }
97
- colorIdx++;
98
- }
99
-
100
- if (myGamepad->b ()) {
101
- // Turn on the 4 LED. Each bit represents one LED.
102
- static int led = 0 ;
103
- led++;
104
- // Some gamepads like the DS3, DualSense, Nintendo Wii, Nintendo Switch
105
- // support changing the "Player LEDs": those 4 LEDs that usually indicate
106
- // the "gamepad seat".
107
- // It is possible to change them by calling:
108
- myGamepad->setPlayerLEDs (led & 0x0f );
109
- }
110
-
111
- if (myGamepad->x ()) {
112
- // Duration: 255 is ~2 seconds
113
- // force: intensity
114
- // Some gamepads like DS3, DS4, DualSense, Switch, Xbox One S support
115
- // rumble.
116
- // It is possible to set it by calling:
117
- myGamepad->setRumble (0xc0 /* force */ , 0xc0 /* duration */ );
118
- }
119
-
120
- // Another way to query the buttons, is by calling buttons(), or
121
- // miscButtons() which return a bitmask.
122
- // Some gamepads also have DPAD, axis and more.
123
- Serial.printf (
124
- " idx=%d, dpad: 0x%02x, buttons: 0x%04x, axis L: %4d, %4d, axis R: %4d, "
125
- " %4d, brake: %4d, throttle: %4d, misc: 0x%02x\n " ,
126
- i, // Gamepad Index
127
- myGamepad->dpad (), // DPAD
128
- myGamepad->buttons (), // bitmask of pressed buttons
129
- myGamepad->axisX (), // (-511 - 512) left X Axis
130
- myGamepad->axisY (), // (-511 - 512) left Y axis
131
- myGamepad->axisRX (), // (-511 - 512) right X axis
132
- myGamepad->axisRY (), // (-511 - 512) right Y axis
133
- myGamepad->brake (), // (0 - 1023): brake button
134
- myGamepad->throttle (), // (0 - 1023): throttle (AKA gas) button
135
- myGamepad->miscButtons () // bitmak of pressed "misc" buttons
136
- );
137
-
138
- // You can query the axis and other properties as well. See Gamepad.h
139
- // For all the available functions.
68
+ // This call fetches all the gamepad info from the NINA (ESP32) module.
69
+ // Just call this function in your main loop.
70
+ // The gamepads pointer (the ones received in the callbacks) gets updated
71
+ // automatically.
72
+ BP32.update ();
73
+
74
+ // It is safe to always do this before using the gamepad API.
75
+ // This guarantees that the gamepad is valid and connected.
76
+ for (int i = 0 ; i < BP32_MAX_GAMEPADS; i++) {
77
+ GamepadPtr myGamepad = myGamepads[i];
78
+
79
+ if (myGamepad && myGamepad->isConnected ()) {
80
+ // There are different ways to query whether a button is pressed.
81
+ // By query each button individually:
82
+ // a(), b(), x(), y(), l1(), etc...
83
+ if (myGamepad->a ()) {
84
+ static int colorIdx = 0 ;
85
+ // Some gamepads like DS4 and DualSense support changing the color LED.
86
+ // It is possible to change it by calling:
87
+ switch (colorIdx % 3 ) {
88
+ case 0 :
89
+ // Red
90
+ myGamepad->setColorLED (255 , 0 , 0 );
91
+ break ;
92
+ case 1 :
93
+ // Green
94
+ myGamepad->setColorLED (0 , 255 , 0 );
95
+ break ;
96
+ case 2 :
97
+ // Blue
98
+ myGamepad->setColorLED (0 , 0 , 255 );
99
+ break ;
140
100
}
101
+ colorIdx++;
102
+ }
103
+
104
+ if (myGamepad->b ()) {
105
+ // Turn on the 4 LED. Each bit represents one LED.
106
+ static int led = 0 ;
107
+ led++;
108
+ // Some gamepads like the DS3, DualSense, Nintendo Wii, Nintendo Switch
109
+ // support changing the "Player LEDs": those 4 LEDs that usually
110
+ // indicate the "gamepad seat". It is possible to change them by
111
+ // calling:
112
+ myGamepad->setPlayerLEDs (led & 0x0f );
113
+ }
114
+
115
+ if (myGamepad->x ()) {
116
+ // Duration: 255 is ~2 seconds
117
+ // force: intensity
118
+ // Some gamepads like DS3, DS4, DualSense, Switch, Xbox One S support
119
+ // rumble.
120
+ // It is possible to set it by calling:
121
+ myGamepad->setRumble (0xc0 /* force */ , 0xc0 /* duration */ );
122
+ }
123
+
124
+ // Another way to query the buttons, is by calling buttons(), or
125
+ // miscButtons() which return a bitmask.
126
+ // Some gamepads also have DPAD, axis and more.
127
+ Serial.printf (
128
+ " idx=%d, dpad: 0x%02x, buttons: 0x%04x, axis L: %4d, %4d, axis R: "
129
+ " %4d, %4d, brake: %4d, throttle: %4d, misc: 0x%02x, gyro x:%6d y:%6d "
130
+ " z:%6d, accel x:%6d y:%6d z:%6d\n " ,
131
+ i, // Gamepad Index
132
+ myGamepad->dpad (), // DPAD
133
+ myGamepad->buttons (), // bitmask of pressed buttons
134
+ myGamepad->axisX (), // (-511 - 512) left X Axis
135
+ myGamepad->axisY (), // (-511 - 512) left Y axis
136
+ myGamepad->axisRX (), // (-511 - 512) right X axis
137
+ myGamepad->axisRY (), // (-511 - 512) right Y axis
138
+ myGamepad->brake (), // (0 - 1023): brake button
139
+ myGamepad->throttle (), // (0 - 1023): throttle (AKA gas) button
140
+ myGamepad->miscButtons (), // bitmak of pressed "misc" buttons
141
+ myGamepad->gyroX (), // Gyro X
142
+ myGamepad->gyroY (), // Gyro Y
143
+ myGamepad->gyroZ (), // Gyro Z
144
+ myGamepad->accelX (), // Accelerometer X
145
+ myGamepad->accelY (), // Accelerometer Y
146
+ myGamepad->accelZ () // Accelerometer Z
147
+ );
148
+
149
+ // You can query the axis and other properties as well. See Gamepad.h
150
+ // For all the available functions.
141
151
}
152
+ }
142
153
143
- // The main loop must have some kind of "yield to lower priority task" event.
144
- // Otherwise the watchdog will get triggered.
145
- // If your main loop doesn't have one, just add a simple `vTaskDelay(1)`.
146
- // Detailed info here:
147
- // https://stackoverflow.com/questions/66278271/task-watchdog-got-triggered-the-tasks-did-not-reset-the-watchdog-in-time
154
+ // The main loop must have some kind of "yield to lower priority task" event.
155
+ // Otherwise the watchdog will get triggered.
156
+ // If your main loop doesn't have one, just add a simple `vTaskDelay(1)`.
157
+ // Detailed info here:
158
+ // https://stackoverflow.com/questions/66278271/task-watchdog-got-triggered-the-tasks-did-not-reset-the-watchdog-in-time
148
159
149
- // vTaskDelay(1);
150
- delay (150 );
160
+ // vTaskDelay(1);
161
+ delay (150 );
151
162
}
0 commit comments