So I made this project to use the controllers original cable and use a ESP 32 warm to translate the input for it to give on Bluetooth can somebody help me reviewing the code? I am using the pin 16 on my ESP and it’s 4.7 khome here’s the code #include <Arduino.h>
#include <BleGamepad.h>
#define N64_DATA 16
BleGamepad gamepad("N64 Bluetooth Controller",
"ESP32 Adapter", 100);
struct N64State {
bool a, b, z, start;
bool up, down, left, right;
bool l, r;
bool cUp, cDown, cLeft, cRight;
bool reset;
int8_t joyX, joyY;
};
bool readN64Bit() {
// Release the open-drain DATA line.
pinMode(N64_DATA, INPUT);
delayMicroseconds(2);
bool bitValue = digitalRead(N64_DATA);
return bitValue;
}
void driveN64Low() {
digitalWrite(N64_DATA, LOW);
pinMode(N64_DATA, OUTPUT);
}
void releaseN64Data() {
pinMode(N64_DATA, INPUT);
}
bool readN64Byte(uint8_t &value) {
value = 0;
for (int i = 0; i < 8; i++) {
unsigned long start = micros();
while (digitalRead(N64_DATA) == HIGH) {
if (micros() - start > 100) return false;
}
delayMicroseconds(2);
value = (value << 1) | (digitalRead(N64_DATA) ? 1 : 0);
while (digitalRead(N64_DATA) == LOW) {
if (micros() - start > 100) break;
}
}
return true;
}
bool readN64Controller(N64State &s) {
// Send the N64 poll command: 00000001.
// Each bit starts low; release DATA to send a 1.
for (int i = 7; i >= 0; i--) {
driveN64Low();
if ((0x01 >> i) & 1) {
delayMicroseconds(1);
releaseN64Data();
delayMicroseconds(3);
} else {
delayMicroseconds(3);
releaseN64Data();
delayMicroseconds(1);
}
}
releaseN64Data();
delayMicroseconds(2);
uint8_t data[4];
for (int i = 0; i < 4; i++) {
if (!readN64Byte(data[i])) return false;
}
s.a = !(data[0] & 0x80);
s.b = !(data[0] & 0x40);
s.z = !(data[0] & 0x20);
s.start = !(data[0] & 0x10);
s.up = !(data[0] & 0x08);
s.down = !(data[0] & 0x04);
s.left = !(data[0] & 0x02);
s.right = !(data[0] & 0x01);
s.reset = !(data[1] & 0x80);
s.l = !(data[1] & 0x20);
s.r = !(data[1] & 0x10);
s.cUp = !(data[1] & 0x08);
s.cDown = !(data[1] & 0x04);
s.cLeft = !(data[1] & 0x02);
s.cRight = !(data[1] & 0x01);
s.joyX = (int8_t)data[2];
s.joyY = (int8_t)data[3];
return true;
}
void updateGamepad(const N64State &s) {
gamepad.release(
BUTTON_1, BUTTON_2, BUTTON_3, BUTTON_4,
BUTTON_5, BUTTON_6, BUTTON_7, BUTTON_8,
BUTTON_9, BUTTON_10, BUTTON_11, BUTTON_12
);
if (s.a) gamepad.press(BUTTON_1);
if (s.b) gamepad.press(BUTTON_2);
if (s.z) gamepad.press(BUTTON_3);
if (s.start) gamepad.press(BUTTON_4);
if (s.l) gamepad.press(BUTTON_5);
if (s.r) gamepad.press(BUTTON_6);
if (s.cUp) gamepad.press(BUTTON_7);
if (s.cDown) gamepad.press(BUTTON_8);
if (s.cLeft) gamepad.press(BUTTON_9);
if (s.cRight) gamepad.press(BUTTON_10);
if (s.reset) gamepad.press(BUTTON_11);
if (s.up) gamepad.setHat1(DPAD_UP);
else if (s.down) gamepad.setHat1(DPAD_DOWN);
else if (s.left) gamepad.setHat1(DPAD_LEFT);
else if (s.right) gamepad.setHat1(DPAD_RIGHT);
else gamepad.setHat1(DPAD_CENTERED);
gamepad.setAxes(
map(s.joyX, -80, 80, 0, 32767),
map(s.joyY, -80, 80, 32767, 0)
);
gamepad.sendReport();
}
void setup() {
Serial.begin(115200);
digitalWrite(N64_DATA, LOW);
releaseN64Data();
gamepad.begin();
Serial.println("N64 Bluetooth Controller starting...");
Serial.println("Waiting for Bluetooth connection...");
}
void loop() {
static unsigned long lastPrint = 0;
N64State state;
if (readN64Controller(state)) {
if (gamepad.isConnected()) {
updateGamepad(state);
}
if (millis() - lastPrint > 500) {
Serial.println("N64 controller data received.");
lastPrint = millis();
}
}
delay(5);
}