|
| 1 | +/**************************************************************************** |
| 2 | +http://retro.moe/unijoysticle2 |
| 3 | +
|
| 4 | +Copyright 2021 Ricardo Quesada |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +****************************************************************************/ |
| 18 | + |
| 19 | +#include "sdkconfig.h" |
| 20 | +#ifndef CONFIG_BLUEPAD32_PLATFORM_ARDUINO |
| 21 | +#error "Must only be compiled when using Bluepad32 Arduino platform" |
| 22 | +#endif // !CONFIG_BLUEPAD32_PLATFORM_ARDUINO |
| 23 | + |
| 24 | +#include <Arduino.h> |
| 25 | +#include <Bluepad32.h> |
| 26 | +#include "ble_server.h" |
| 27 | + |
| 28 | +// |
| 29 | +// README FIRST, README FIRST, README FIRST |
| 30 | +// |
| 31 | +// Bluepad32 has a built-in interactive console. |
| 32 | +// By default it is enabled (hey, this is a great feature!). |
| 33 | +// But it is incompatible with Arduino "Serial" class. |
| 34 | +// |
| 35 | +// Instead of using "Serial" you can use Bluepad32 "Console" class instead. |
| 36 | +// It is somewhat similar to Serial but not exactly the same. |
| 37 | +// |
| 38 | +// Should you want to still use "Serial", you have to disable the Bluepad32's console |
| 39 | +// from "sdkconfig.defaults" with: |
| 40 | +// CONFIG_BLUEPAD32_USB_CONSOLE_ENABLE=n |
| 41 | + |
| 42 | +GamepadPtr myGamepads[BP32_MAX_GAMEPADS]; |
| 43 | + |
| 44 | +// This callback gets called any time a new gamepad is connected. |
| 45 | +// Up to 4 gamepads can be connected at the same time. |
| 46 | +void onConnectedGamepad(GamepadPtr gp) |
| 47 | +{ |
| 48 | + bool foundEmptySlot = false; |
| 49 | + for (int i = 0; i < BP32_MAX_GAMEPADS; i++) |
| 50 | + { |
| 51 | + if (myGamepads[i] == nullptr) |
| 52 | + { |
| 53 | + Console.printf("CALLBACK: Gamepad is connected, index=%d\n", i); |
| 54 | + // Additionally, you can get certain gamepad properties like: |
| 55 | + // Model, VID, PID, BTAddr, flags, etc. |
| 56 | + GamepadProperties properties = gp->getProperties(); |
| 57 | + Console.printf("Gamepad model: %s, VID=0x%04x, PID=0x%04x\n", gp->getModelName(), properties.vendor_id, |
| 58 | + properties.product_id); |
| 59 | + myGamepads[i] = gp; |
| 60 | + foundEmptySlot = true; |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + if (!foundEmptySlot) |
| 65 | + { |
| 66 | + Console.println("CALLBACK: Gamepad connected, but could not found empty slot"); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void onDisconnectedGamepad(GamepadPtr gp) |
| 71 | +{ |
| 72 | + bool foundGamepad = false; |
| 73 | + |
| 74 | + for (int i = 0; i < BP32_MAX_GAMEPADS; i++) |
| 75 | + { |
| 76 | + if (myGamepads[i] == gp) |
| 77 | + { |
| 78 | + Console.printf("CALLBACK: Gamepad is disconnected from index=%d\n", i); |
| 79 | + myGamepads[i] = nullptr; |
| 80 | + foundGamepad = true; |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (!foundGamepad) |
| 86 | + { |
| 87 | + Console.println("CALLBACK: Gamepad disconnected, but not found in myGamepads"); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Arduino setup function. Runs in CPU 1 |
| 92 | +void setup() |
| 93 | +{ |
| 94 | + Console.printf("Firmware: %s\n", BP32.firmwareVersion()); |
| 95 | + const uint8_t *addr = BP32.localBdAddress(); |
| 96 | + Console.printf("BD Addr: %2X:%2X:%2X:%2X:%2X:%2X\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); |
| 97 | + |
| 98 | + // Setup the Bluepad32 callbacks |
| 99 | + BP32.setup(&onConnectedGamepad, &onDisconnectedGamepad); |
| 100 | + |
| 101 | + // "forgetBluetoothKeys()" should be called when the user performs |
| 102 | + // a "device factory reset", or similar. |
| 103 | + // Calling "forgetBluetoothKeys" in setup() just as an example. |
| 104 | + // Forgetting Bluetooth keys prevents "paired" gamepads to reconnect. |
| 105 | + // But might also fix some connection / re-connection issues. |
| 106 | + BP32.forgetBluetoothKeys(); |
| 107 | + |
| 108 | + BP32.enableNewBluetoothConnections(false); |
| 109 | + |
| 110 | + BLE_SERVER_SETUP(); |
| 111 | +} |
| 112 | + |
| 113 | +// Arduino loop function. Runs in CPU 1 |
| 114 | +void loop() |
| 115 | +{ |
| 116 | + // This call fetches all the gamepad info from the NINA (ESP32) module. |
| 117 | + // Just call this function in your main loop. |
| 118 | + // The gamepads pointer (the ones received in the callbacks) gets updated |
| 119 | + // automatically. |
| 120 | + BP32.update(); |
| 121 | + |
| 122 | + // It is safe to always do this before using the gamepad API. |
| 123 | + // This guarantees that the gamepad is valid and connected. |
| 124 | + for (int i = 0; i < BP32_MAX_GAMEPADS; i++) |
| 125 | + { |
| 126 | + GamepadPtr myGamepad = myGamepads[i]; |
| 127 | + |
| 128 | + if (myGamepad && myGamepad->isConnected()) |
| 129 | + { |
| 130 | + // There are different ways to query whether a button is pressed. |
| 131 | + // By query each button individually: |
| 132 | + // a(), b(), x(), y(), l1(), etc... |
| 133 | + if (myGamepad->a()) |
| 134 | + { |
| 135 | + static int colorIdx = 0; |
| 136 | + // Some gamepads like DS4 and DualSense support changing the color LED. |
| 137 | + // It is possible to change it by calling: |
| 138 | + switch (colorIdx % 3) |
| 139 | + { |
| 140 | + case 0: |
| 141 | + // Red |
| 142 | + myGamepad->setColorLED(255, 0, 0); |
| 143 | + break; |
| 144 | + case 1: |
| 145 | + // Green |
| 146 | + myGamepad->setColorLED(0, 255, 0); |
| 147 | + break; |
| 148 | + case 2: |
| 149 | + // Blue |
| 150 | + myGamepad->setColorLED(0, 0, 255); |
| 151 | + break; |
| 152 | + } |
| 153 | + colorIdx++; |
| 154 | + } |
| 155 | + |
| 156 | + if (myGamepad->b()) |
| 157 | + { |
| 158 | + // Turn on the 4 LED. Each bit represents one LED. |
| 159 | + static int led = 0; |
| 160 | + led++; |
| 161 | + // Some gamepads like the DS3, DualSense, Nintendo Wii, Nintendo Switch |
| 162 | + // support changing the "Player LEDs": those 4 LEDs that usually indicate |
| 163 | + // the "gamepad seat". |
| 164 | + // It is possible to change them by calling: |
| 165 | + myGamepad->setPlayerLEDs(led & 0x0f); |
| 166 | + } |
| 167 | + |
| 168 | + if (myGamepad->x()) |
| 169 | + { |
| 170 | + // Duration: 255 is ~2 seconds |
| 171 | + // force: intensity |
| 172 | + // Some gamepads like DS3, DS4, DualSense, Switch, Xbox One S support |
| 173 | + // rumble. |
| 174 | + // It is possible to set it by calling: |
| 175 | + myGamepad->setRumble(0xc0 /* force */, 0xc0 /* duration */); |
| 176 | + } |
| 177 | + |
| 178 | + // Another way to query the buttons, is by calling buttons(), or |
| 179 | + // miscButtons() which return a bitmask. |
| 180 | + // Some gamepads also have DPAD, axis and more. |
| 181 | + Console.printf( |
| 182 | + "idx=%d, dpad: 0x%02x, buttons: 0x%04x, axis L: %4d, %4d, axis R: %4d, %4d, brake: %4d, throttle: %4d, " |
| 183 | + "misc: 0x%02x, gyro x:%6d y:%6d z:%6d, accel x:%6d y:%6d z:%6d\n", |
| 184 | + i, // Gamepad Index |
| 185 | + myGamepad->dpad(), // DPAD |
| 186 | + myGamepad->buttons(), // bitmask of pressed buttons |
| 187 | + myGamepad->axisX(), // (-511 - 512) left X Axis |
| 188 | + myGamepad->axisY(), // (-511 - 512) left Y axis |
| 189 | + myGamepad->axisRX(), // (-511 - 512) right X axis |
| 190 | + myGamepad->axisRY(), // (-511 - 512) right Y axis |
| 191 | + myGamepad->brake(), // (0 - 1023): brake button |
| 192 | + myGamepad->throttle(), // (0 - 1023): throttle (AKA gas) button |
| 193 | + myGamepad->miscButtons(), // bitmak of pressed "misc" buttons |
| 194 | + myGamepad->gyroX(), // Gyro X |
| 195 | + myGamepad->gyroY(), // Gyro Y |
| 196 | + myGamepad->gyroZ(), // Gyro Z |
| 197 | + myGamepad->accelX(), // Accelerometer X |
| 198 | + myGamepad->accelY(), // Accelerometer Y |
| 199 | + myGamepad->accelZ() // Accelerometer Z |
| 200 | + ); |
| 201 | + |
| 202 | + // You can query the axis and other properties as well. See ArduinoController.h |
| 203 | + // For all the available functions. |
| 204 | + } |
| 205 | + } |
| 206 | + // The main loop must have some kind of "yield to lower priority task" event. |
| 207 | + // Otherwise the watchdog will get triggered. |
| 208 | + // If your main loop doesn't have one, just add a simple `vTaskDelay(1)`. |
| 209 | + // Detailed info here: |
| 210 | + // https://stackoverflow.com/questions/66278271/task-watchdog-got-triggered-the-tasks-did-not-reset-the-watchdog-in-time |
| 211 | + |
| 212 | + // vTaskDelay(1); |
| 213 | + delay(150); |
| 214 | +} |
0 commit comments