191 lines
4.4 KiB
C++
191 lines
4.4 KiB
C++
#include <Arduino.h>
|
|
#include <BleGamepad.h>
|
|
#include <BleGamepadConfiguration.h>
|
|
|
|
BleGamepad bleGamepad("Test Manette ESP32", "Nino", 100);
|
|
|
|
// -------------------- BOUTONS --------------------
|
|
// 4 boutons à gauche
|
|
const int leftButtons[4] = {
|
|
13, 12, 14, 27
|
|
};
|
|
|
|
// 4 boutons à droite
|
|
const int rightButtons[4] = {
|
|
26, 25, 33, 32
|
|
};
|
|
|
|
// -------------------- JOYSTICKS --------------------
|
|
// Joystick gauche
|
|
const int joyLX = 34;
|
|
const int joyLY = 35;
|
|
|
|
// Joystick droit
|
|
const int joyRX = 39;
|
|
const int joyRY = 36;
|
|
|
|
// Zone morte
|
|
const int deadzone = 180;
|
|
|
|
// Pour limiter l'affichage série
|
|
unsigned long lastPrint = 0;
|
|
const unsigned long printInterval = 300;
|
|
|
|
int16_t readJoystickAxis(int pin) {
|
|
int value = analogRead(pin); // 0 à 4095
|
|
int centered = value - 2048;
|
|
|
|
if (abs(centered) < deadzone) {
|
|
return 0;
|
|
}
|
|
|
|
return map(value, 0, 4095, -32767, 32767);
|
|
}
|
|
|
|
void testBluetoothConnection() {
|
|
static bool lastConnectionState = false;
|
|
bool currentConnectionState = bleGamepad.isConnected();
|
|
|
|
if (currentConnectionState != lastConnectionState) {
|
|
if (currentConnectionState) {
|
|
Serial.println("[BT] Connecte a un appareil !");
|
|
} else {
|
|
Serial.println("[BT] Deconnecte.");
|
|
}
|
|
|
|
lastConnectionState = currentConnectionState;
|
|
}
|
|
}
|
|
|
|
void testButtons() {
|
|
static bool lastLeftState[4] = {HIGH, HIGH, HIGH, HIGH};
|
|
static bool lastRightState[4] = {HIGH, HIGH, HIGH, HIGH};
|
|
|
|
// Boutons gauche = boutons 1 à 4
|
|
for (int i = 0; i < 4; i++) {
|
|
bool currentState = digitalRead(leftButtons[i]);
|
|
|
|
if (currentState != lastLeftState[i]) {
|
|
if (currentState == LOW) {
|
|
Serial.print("[BOUTON GAUCHE ");
|
|
Serial.print(i + 1);
|
|
Serial.println("] Appuye");
|
|
|
|
if (bleGamepad.isConnected()) {
|
|
bleGamepad.press(i + 1);
|
|
}
|
|
} else {
|
|
Serial.print("[BOUTON GAUCHE ");
|
|
Serial.print(i + 1);
|
|
Serial.println("] Relache");
|
|
|
|
if (bleGamepad.isConnected()) {
|
|
bleGamepad.release(i + 1);
|
|
}
|
|
}
|
|
|
|
lastLeftState[i] = currentState;
|
|
}
|
|
}
|
|
|
|
// Boutons droite = boutons 5 à 8
|
|
for (int i = 0; i < 4; i++) {
|
|
bool currentState = digitalRead(rightButtons[i]);
|
|
|
|
if (currentState != lastRightState[i]) {
|
|
if (currentState == LOW) {
|
|
Serial.print("[BOUTON DROITE ");
|
|
Serial.print(i + 1);
|
|
Serial.println("] Appuye");
|
|
|
|
if (bleGamepad.isConnected()) {
|
|
bleGamepad.press(i + 5);
|
|
}
|
|
} else {
|
|
Serial.print("[BOUTON DROITE ");
|
|
Serial.print(i + 1);
|
|
Serial.println("] Relache");
|
|
|
|
if (bleGamepad.isConnected()) {
|
|
bleGamepad.release(i + 5);
|
|
}
|
|
}
|
|
|
|
lastRightState[i] = currentState;
|
|
}
|
|
}
|
|
}
|
|
|
|
void testJoysticks() {
|
|
if (millis() - lastPrint >= printInterval) {
|
|
lastPrint = millis();
|
|
|
|
int rawLX = analogRead(joyLX);
|
|
int rawLY = analogRead(joyLY);
|
|
int rawRX = analogRead(joyRX);
|
|
int rawRY = analogRead(joyRY);
|
|
|
|
int16_t lx = readJoystickAxis(joyLX);
|
|
int16_t ly = readJoystickAxis(joyLY);
|
|
int16_t rx = readJoystickAxis(joyRX);
|
|
int16_t ry = readJoystickAxis(joyRY);
|
|
|
|
Serial.println("----- JOYSTICKS -----");
|
|
|
|
Serial.print("[JOY GAUCHE] raw X=");
|
|
Serial.print(rawLX);
|
|
Serial.print(" raw Y=");
|
|
Serial.print(rawLY);
|
|
Serial.print(" | mapped X=");
|
|
Serial.print(lx);
|
|
Serial.print(" mapped Y=");
|
|
Serial.println(ly);
|
|
|
|
Serial.print("[JOY DROITE] raw X=");
|
|
Serial.print(rawRX);
|
|
Serial.print(" raw Y=");
|
|
Serial.print(rawRY);
|
|
Serial.print(" | mapped X=");
|
|
Serial.print(rx);
|
|
Serial.print(" mapped Y=");
|
|
Serial.println(ry);
|
|
|
|
if (bleGamepad.isConnected()) {
|
|
bleGamepad.setAxes(lx, ly, 0, rx, ry, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(1000);
|
|
|
|
Serial.println();
|
|
Serial.println("===== TEST MANETTE ESP32 =====");
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
pinMode(leftButtons[i], INPUT_PULLUP);
|
|
pinMode(rightButtons[i], INPUT_PULLUP);
|
|
}
|
|
|
|
analogReadResolution(12);
|
|
|
|
BleGamepadConfiguration bleGamepadConfig;
|
|
bleGamepadConfig.setAutoReport(true);
|
|
bleGamepadConfig.setButtonCount(8);
|
|
bleGamepadConfig.setWhichAxes(true, true, false, true, true, false, false, false);
|
|
|
|
bleGamepad.begin(&bleGamepadConfig);
|
|
|
|
Serial.println("Bluetooth HID demarre.");
|
|
Serial.println("Cherche la manette : Test Manette ESP32");
|
|
Serial.println("Attente connexion...");
|
|
}
|
|
|
|
void loop() {
|
|
testBluetoothConnection();
|
|
testButtons();
|
|
testJoysticks();
|
|
|
|
delay(20);
|
|
} |