Created
November 14, 2024 22:50
-
-
Save apocas/621b3b5d3d07cb393d7e6c1f6722ed73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <WebServer.h> | |
#include <uri/UriRegex.h> | |
#include <BleConnectionStatus.h> | |
#include <BleCompositeHID.h> | |
#include <XboxGamepadDevice.h> | |
XboxGamepadDevice *gamepad; | |
BleCompositeHID compositeHID("Xbox Controller", "Microsoft Corp.", 100); | |
const char *SSID = "xxxxxxx"; | |
const char *PWD = "xxxxxx"; | |
WebServer server(80); | |
void setup_routing() { | |
server.on("/right", getRight); | |
server.on("/a", getA); | |
server.on("/b", getB); | |
server.on("/x", getX); | |
server.on("/y", getY); | |
server.on("/lb", getLB); | |
server.on("/rb", getRB); | |
server.on("/lsu", getLSU); | |
server.on("/lsd", getLSD); | |
server.on("/lsl", getLSL); | |
server.on("/lsr", getLSR); | |
server.on("/rsu", getRSU); | |
server.on("/rsd", getRSD); | |
server.on("/rsl", getRSL); | |
server.on("/rsr", getRSR); | |
server.on(UriRegex("^\\/s\\/(-?[0-9]+)\\/(-?[0-9]+)\\/(-?[0-9]+)\\/(-?[0-9]+)\\/(-?[0-9]+)$"), getS); | |
server.begin(); | |
} | |
void getRight() { | |
USBSerial.println("Right"); | |
server.send(200, "application/json", ""); | |
} | |
void getA() { | |
USBSerial.println("Button A"); | |
gamepad->press(XBOX_BUTTON_A); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->release(XBOX_BUTTON_A); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"A\"}"); | |
} | |
void getB() { | |
USBSerial.println("Button B"); | |
gamepad->press(XBOX_BUTTON_B); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->release(XBOX_BUTTON_B); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"B\"}"); | |
} | |
void getX() { | |
USBSerial.println("Button X"); | |
gamepad->press(XBOX_BUTTON_X); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->release(XBOX_BUTTON_X); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"X\"}"); | |
} | |
void getY() { | |
USBSerial.println("Button Y"); | |
gamepad->press(XBOX_BUTTON_Y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->release(XBOX_BUTTON_Y); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"Y\"}"); | |
} | |
void getLB() { | |
USBSerial.println("Button LB"); | |
gamepad->press(XBOX_BUTTON_LB); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->release(XBOX_BUTTON_LB); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"LB\"}"); | |
} | |
void getRB() { | |
USBSerial.println("Button RB"); | |
gamepad->press(XBOX_BUTTON_RB); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->release(XBOX_BUTTON_RB); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"RB\"}"); | |
} | |
void getLSU() { | |
USBSerial.println("Left Stick Up"); | |
int16_t x = 0; | |
int16_t y = XBOX_STICK_MIN; | |
gamepad->setLeftThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setLeftThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"LSU\"}"); | |
} | |
void getLSD() { | |
USBSerial.println("Left Stick Down"); | |
int16_t x = 0; | |
int16_t y = XBOX_STICK_MAX; | |
gamepad->setLeftThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setLeftThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"LSD\"}"); | |
} | |
void getLSL() { | |
USBSerial.println("Left Stick Left"); | |
int16_t x = XBOX_STICK_MIN; | |
int16_t y = 0; | |
gamepad->setLeftThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setLeftThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"LSL\"}"); | |
} | |
void getLSR() { | |
USBSerial.println("Left Stick Right"); | |
int16_t x = XBOX_STICK_MAX; | |
int16_t y = 0; | |
gamepad->setLeftThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setLeftThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"LSR\"}"); | |
} | |
void getRSU() { | |
USBSerial.println("Right Stick Up"); | |
int16_t x = 0; | |
int16_t y = XBOX_STICK_MIN; | |
gamepad->setRightThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setRightThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"RSU\"}"); | |
} | |
void getRSD() { | |
USBSerial.println("Right Stick Down"); | |
int16_t x = 0; | |
int16_t y = XBOX_STICK_MAX; | |
gamepad->setRightThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setRightThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"RSD\"}"); | |
} | |
void getRSL() { | |
USBSerial.println("Right Stick Left"); | |
int16_t x = XBOX_STICK_MIN; | |
int16_t y = 0; | |
gamepad->setRightThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setRightThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"RSL\"}"); | |
} | |
void getRSR() { | |
USBSerial.println("Right Stick Right"); | |
int16_t x = XBOX_STICK_MAX; | |
int16_t y = 0; | |
gamepad->setRightThumb(x, y); | |
gamepad->sendGamepadReport(); | |
delay(200); | |
gamepad->setRightThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"RSR\"}"); | |
} | |
void getS() { | |
int16_t leftStickX = (server.pathArg(0).toInt() * XBOX_STICK_MAX) / 100; | |
int16_t leftStickY = (server.pathArg(1).toInt() * XBOX_STICK_MAX) / 100; | |
int16_t rightStickX = (server.pathArg(2).toInt() * XBOX_STICK_MAX) / 100; | |
int16_t rightStickY = (server.pathArg(3).toInt() * XBOX_STICK_MAX) / 100; | |
int16_t duration = server.pathArg(4).toInt(); | |
USBSerial.print("Left Stick (X, Y): "); | |
USBSerial.print(leftStickX); | |
USBSerial.print(", "); | |
USBSerial.println(leftStickY); | |
USBSerial.print("Right Stick (X, Y): "); | |
USBSerial.print(rightStickX); | |
USBSerial.print(", "); | |
USBSerial.println(rightStickY); | |
gamepad->setLeftThumb(leftStickX, leftStickY); | |
gamepad->setRightThumb(rightStickX, rightStickY); | |
gamepad->sendGamepadReport(); | |
delay(duration); | |
gamepad->setLeftThumb(0, 0); | |
gamepad->setRightThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
server.send(200, "application/json", "{\"button\": \"S\"}"); | |
} | |
void setup() | |
{ | |
USBSerial.begin(115200); | |
XboxSeriesXControllerDeviceConfiguration* config = new XboxSeriesXControllerDeviceConfiguration(); | |
BLEHostConfiguration hostConfig = config->getIdealHostConfiguration(); | |
USBSerial.println("Using VID source: " + String(hostConfig.getVidSource(), HEX)); | |
USBSerial.println("Using VID: " + String(hostConfig.getVid(), HEX)); | |
USBSerial.println("Using PID: " + String(hostConfig.getPid(), HEX)); | |
USBSerial.println("Using GUID version: " + String(hostConfig.getGuidVersion(), HEX)); | |
USBSerial.println("Using serial number: " + String(hostConfig.getSerialNumber())); | |
gamepad = new XboxGamepadDevice(config); | |
compositeHID.addDevice(gamepad); | |
USBSerial.println("Starting composite HID device..."); | |
compositeHID.begin(hostConfig); | |
USBSerial.print("Connecting to Wi-Fi"); | |
WiFi.begin(SSID, PWD); | |
while (WiFi.status() != WL_CONNECTED) { | |
USBSerial.print("."); | |
delay(500); | |
} | |
USBSerial.print("Connected! IP Address: "); | |
USBSerial.println(WiFi.localIP()); | |
setup_routing(); | |
while(!compositeHID.isConnected()) { | |
gamepad->setLeftThumb(0, 0); | |
gamepad->setRightThumb(0, 0); | |
gamepad->sendGamepadReport(); | |
} | |
} | |
void loop() | |
{ | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment