Hello all, hope everyone is doing well. I've been working on a project with the Xbox Adaptive Controller and an ESP32. Here is a little background.
I have a physical disability that effects my muscles (cerebral palsy) and I'm not able to use a regular Xbox controller. I bought an Xbox Adaptive Controller, hooked up 19 relays to it and an ESP32, wrote a python script that runs on my communication device (controlled by my eyes) which sends messages to the ESP32 via bluetooth telling which relays to trip/untrip. This works great.
The only draw back to this has been, with the thumb, I only have options of on or off. Meaning I can only move them either 0% or 100%, no middle ground. The adaptive controller has two USB ports where you can plug in joysticks and they become the thumb sticks. So I had the idea of using a Leopard with a bluetooth module and the joystick library to give me analog joystick control and wrote the following code.
#include <SoftwareSerial.h>
#include "Joystick.h"
SoftwareSerial mySerial(10, 11); // RX, TX
String message = ""; // Buffer to store incoming message
String action;
int button;
int xValue, yValue;
bool firstRun = false;
void setup() {
Joystick.begin();
mySerial.begin(9600);
Serial.begin(9600);
}
void loop() {
if(!firstRun){
Joystick.setZAxis(0);
Joystick.setXAxis(0);
Joystick.setYAxis(0);
firstRun=true;
}
// Check if data is available in the serial buffer
while (mySerial.available()) {
char c = (char)mySerial.read(); // Read one character
if (c == '\n') {
// When newline is encountered, process the full message
processMessage(message);
message = ""; // Clear the buffer for the next message
} else {
// Accumulate the characters into the buffer
message += c;
}
}
}
void processMessage(String msg) {
if (msg.length() > 0) {
// The first character is the action (e.g., 'p' or 'r')
action = msg.charAt(0);
// The rest of the message (from index 1 onwards) is the button number
String tmpButton = msg.substring(1);
if (action == "p" || action == "r") {
button = tmpButton.toInt();
// Handle digitalWrite for buttons 0-8
if (button >= 0 && button < 9) {
if (action == "p") {
digitalWrite(button, HIGH);
} else if (action == "r") {
digitalWrite(button, LOW);
}
}
// Handle Joystick button actions for button >= 10
else if (button >= 10) {
button -= 10;
if (action == "p") {
Joystick.pressButton(button);
} else if (action == "r") {
Joystick.releaseButton(button);
}
}
}
else if(action == "l") {
int commaIndex = msg.indexOf(',');
if (commaIndex != -1) {
// Extract the X value (substring after 'l' and before the comma)
String xStr = msg.substring(1, commaIndex);
// Extract the Y value (substring after the comma)
String yStr = msg.substring(commaIndex + 1);
// Convert the strings to integers
xValue = xStr.toInt();
yValue = yStr.toInt();
// Set the X and Y axes of the joystick
Joystick.setXAxis(xValue);
Joystick.setYAxis(yValue);
}
}
}
}
The idea is, I'm still going to have a couple of relays for the D-pad since you apparently can't have D-pad commands go through the USB ports, but you can send commands such as Button 1, Button 2, Button 3, etc. through the USB. The problem I'm having is, no matter what I send to the leopard via bluetooth, the left thumb stick always goes to the left and stays there, even if I sent "l0,0". The problem does not exist when I plug the leopard into my computer, only with the adaptive controller.
I know this is kind of difficult to follow but I'm hoping someone might be able to help me figure out what I'm doing wrong?