|
1 |
| -#include <Bluepad32.h> |
| 1 | +#include "Arduino.h" |
2 | 2 |
|
3 |
| -GamepadPtr myGamepads[BP32_MAX_GAMEPADS]; |
4 |
| - |
5 |
| -// This callback gets called any time a new gamepad is connected. |
6 |
| -// Up to 4 gamepads can be connected at the same time. |
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"); |
24 |
| - } |
25 |
| -} |
26 |
| - |
27 |
| -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 |
| - } |
37 |
| - } |
38 |
| - |
39 |
| - if (!foundGamepad) { |
40 |
| - Serial.println("CALLBACK: Gamepad disconnected, but not found in myGamepads"); |
41 |
| - } |
42 |
| -} |
43 |
| - |
44 |
| -// Arduino setup function. Runs in CPU 1 |
45 | 3 | 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(); |
| 4 | + Serial.begin(115200); |
60 | 5 | }
|
61 | 6 |
|
62 |
| -// Arduino loop function. Runs in CPU 1 |
63 | 7 | 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. |
140 |
| - } |
141 |
| - } |
142 |
| - |
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 |
148 |
| - |
149 |
| - // vTaskDelay(1); |
150 |
| - delay(150); |
| 8 | + Serial.println("Hello World!"); |
| 9 | + delay(1000); |
151 | 10 | }
|
0 commit comments