add test unitaire
This commit is contained in:
191
main.cpp
Normal file
191
main.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
#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);
|
||||
}
|
||||
73
main.py
73
main.py
@@ -1,73 +0,0 @@
|
||||
#include <BleGamepad.h>
|
||||
|
||||
BleGamepad bleGamepad("Manette ModulX", "Nono", 100);
|
||||
|
||||
// --------- BOUTONS ---------
|
||||
// Boutons câblés entre GPIO et GND
|
||||
const int buttonPins[8] = {
|
||||
13, 12, 14, 27,
|
||||
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 pour éviter les tremblements
|
||||
const int deadzone = 180;
|
||||
|
||||
// Convertit ADC ESP32 0-4095 vers axe manette -32767 à 32767
|
||||
int16_t mapJoystick(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 setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
pinMode(buttonPins[i], INPUT_PULLUP);
|
||||
}
|
||||
|
||||
bleGamepad.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (bleGamepad.isConnected()) {
|
||||
|
||||
// Gestion des 8 boutons
|
||||
for (int i = 0; i < 8; i++) {
|
||||
bool pressed = digitalRead(buttonPins[i]) == LOW;
|
||||
|
||||
if (pressed) {
|
||||
bleGamepad.press(i + 1); // boutons 1 à 8
|
||||
} else {
|
||||
bleGamepad.release(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Lecture des joysticks
|
||||
int16_t lx = mapJoystick(joyLX);
|
||||
int16_t ly = mapJoystick(joyLY);
|
||||
int16_t rx = mapJoystick(joyRX);
|
||||
int16_t ry = mapJoystick(joyRY);
|
||||
|
||||
// Envoie des axes
|
||||
// X, Y = joystick gauche
|
||||
// RX, RY = joystick droit
|
||||
bleGamepad.setAxes(lx, ly, 0, rx, ry, 0);
|
||||
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
73
test.py
73
test.py
@@ -1,73 +0,0 @@
|
||||
#include <BleGamepad.h>
|
||||
|
||||
BleGamepad bleGamepad("Manette ESP32", "Nino", 100);
|
||||
|
||||
// --------- BOUTONS ---------
|
||||
// Boutons câblés entre GPIO et GND
|
||||
const int buttonPins[8] = {
|
||||
13, 12, 14, 27,
|
||||
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 pour éviter les tremblements
|
||||
const int deadzone = 180;
|
||||
|
||||
// Convertit ADC ESP32 0-4095 vers axe manette -32767 à 32767
|
||||
int16_t mapJoystick(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 setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
pinMode(buttonPins[i], INPUT_PULLUP);
|
||||
}
|
||||
|
||||
bleGamepad.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (bleGamepad.isConnected()) {
|
||||
|
||||
// Gestion des 8 boutons
|
||||
for (int i = 0; i < 8; i++) {
|
||||
bool pressed = digitalRead(buttonPins[i]) == LOW;
|
||||
|
||||
if (pressed) {
|
||||
bleGamepad.press(i + 1); // boutons 1 à 8
|
||||
} else {
|
||||
bleGamepad.release(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Lecture des joysticks
|
||||
int16_t lx = mapJoystick(joyLX);
|
||||
int16_t ly = mapJoystick(joyLY);
|
||||
int16_t rx = mapJoystick(joyRX);
|
||||
int16_t ry = mapJoystick(joyRY);
|
||||
|
||||
// Envoie des axes
|
||||
// X, Y = joystick gauche
|
||||
// RX, RY = joystick droit
|
||||
bleGamepad.setAxes(lx, ly, 0, rx, ry, 0);
|
||||
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
52
test_bluetooth.cpp
Normal file
52
test_bluetooth.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <BleCompositeHID.h>
|
||||
#include <GamepadDevice.h>
|
||||
|
||||
BleCompositeHID compositeHID("Modulx Controller V4", "Nono", 100);
|
||||
GamepadDevice gamepad;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("===== TEST BLE COMPOSITE HID =====");
|
||||
|
||||
compositeHID.addDevice(&gamepad);
|
||||
compositeHID.begin();
|
||||
|
||||
Serial.println("BLE demarre.");
|
||||
Serial.println("Nom Bluetooth : Modulx Controller V4");
|
||||
Serial.println(" AHHHHHHH Essaie de l'appairer");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static bool lastConnected = false;
|
||||
bool connected = compositeHID.isConnected();
|
||||
|
||||
if (connected != lastConnected) {
|
||||
if (connected) {
|
||||
Serial.println("[BLE] Connecte !");
|
||||
} else {
|
||||
Serial.println("[BLE] Deconnecte.");
|
||||
}
|
||||
|
||||
lastConnected = connected;
|
||||
}
|
||||
|
||||
if (connected) {
|
||||
Serial.println("[BLE] Test bouton 1");
|
||||
|
||||
gamepad.press(1);
|
||||
gamepad.sendGamepadReport();
|
||||
delay(200);
|
||||
|
||||
gamepad.release(1);
|
||||
gamepad.sendGamepadReport();
|
||||
delay(1800);
|
||||
} else {
|
||||
Serial.println("[BLE] En attente connexion...");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
52
test_boutons_droite.cpp
Normal file
52
test_boutons_droite.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
// Test uniquement des 5 boutons droite
|
||||
const int rightButtons[5] = {
|
||||
25, 33, 32, 4, 17
|
||||
};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("===== TEST BOUTONS DROITE =====");
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
pinMode(rightButtons[i], INPUT_PULLUP);
|
||||
|
||||
Serial.print("Bouton droite ");
|
||||
Serial.print(i + 1);
|
||||
Serial.print(" sur GPIO ");
|
||||
Serial.println(rightButtons[i]);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Cablage attendu : GPIO ---- bouton ---- GND");
|
||||
Serial.println("Relache = HIGH");
|
||||
Serial.println("Appuye = LOW");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("----- ETAT BOUTONS DROITE -----");
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int state = digitalRead(rightButtons[i]);
|
||||
|
||||
Serial.print("Bouton D");
|
||||
Serial.print(i + 1);
|
||||
Serial.print(" GPIO ");
|
||||
Serial.print(rightButtons[i]);
|
||||
Serial.print(" = ");
|
||||
|
||||
if (state == LOW) {
|
||||
Serial.println("APPUYE");
|
||||
} else {
|
||||
Serial.println("relache");
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
delay(500);
|
||||
}
|
||||
52
test_boutons_gauche.cpp
Normal file
52
test_boutons_gauche.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
// Test uniquement des 5 boutons gauche
|
||||
const int leftButtons[5] = {
|
||||
13, 12, 14, 27, 26
|
||||
};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("===== TEST BOUTONS GAUCHE =====");
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
pinMode(leftButtons[i], INPUT_PULLUP);
|
||||
|
||||
Serial.print("Bouton gauche ");
|
||||
Serial.print(i + 1);
|
||||
Serial.print(" sur GPIO ");
|
||||
Serial.println(leftButtons[i]);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Cablage attendu : GPIO ---- bouton ---- GND");
|
||||
Serial.println("Relache = HIGH");
|
||||
Serial.println("Appuye = LOW");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("----- ETAT BOUTONS GAUCHE -----");
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int state = digitalRead(leftButtons[i]);
|
||||
|
||||
Serial.print("Bouton G");
|
||||
Serial.print(i + 1);
|
||||
Serial.print(" GPIO ");
|
||||
Serial.print(leftButtons[i]);
|
||||
Serial.print(" = ");
|
||||
|
||||
if (state == LOW) {
|
||||
Serial.println("APPUYE");
|
||||
} else {
|
||||
Serial.println("relache");
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
delay(500);
|
||||
}
|
||||
130
test_joystick_droite.cpp
Normal file
130
test_joystick_droite.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <BleCompositeHID.h>
|
||||
#include <GamepadDevice.h>
|
||||
|
||||
BleCompositeHID compositeHID("Modulx Controller V4", "Nono", 100);
|
||||
GamepadDevice gamepad;
|
||||
|
||||
// Joystick droit
|
||||
const int JOY_RIGHT_X = 34; // VRX
|
||||
const int JOY_RIGHT_Y = 35; // VRY
|
||||
|
||||
// Centre réel mesuré au démarrage
|
||||
int centerRightX = 2048;
|
||||
int centerRightY = 2048;
|
||||
|
||||
// Zone morte autour du centre réel
|
||||
const int DEADZONE = 250;
|
||||
|
||||
unsigned long lastPrint = 0;
|
||||
const unsigned long PRINT_INTERVAL = 300;
|
||||
|
||||
int16_t readJoystickAxisCalibrated(int pin, int centerValue) {
|
||||
int raw = analogRead(pin);
|
||||
int centered = raw - centerValue;
|
||||
|
||||
if (abs(centered) < DEADZONE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Conversion selon le côté
|
||||
if (centered > 0) {
|
||||
return map(centered, DEADZONE, 4095 - centerValue, 0, 32767);
|
||||
} else {
|
||||
return map(centered, -DEADZONE, -centerValue, 0, -32767);
|
||||
}
|
||||
}
|
||||
|
||||
void calibrateRightJoystick() {
|
||||
long sumX = 0;
|
||||
long sumY = 0;
|
||||
const int samples = 100;
|
||||
|
||||
Serial.println("Calibration joystick droit...");
|
||||
Serial.println("Ne touche pas au joystick.");
|
||||
|
||||
delay(1000);
|
||||
|
||||
for (int i = 0; i < samples; i++) {
|
||||
sumX += analogRead(JOY_RIGHT_X);
|
||||
sumY += analogRead(JOY_RIGHT_Y);
|
||||
delay(5);
|
||||
}
|
||||
|
||||
centerRightX = sumX / samples;
|
||||
centerRightY = sumY / samples;
|
||||
|
||||
Serial.print("Centre joystick droit X = ");
|
||||
Serial.println(centerRightX);
|
||||
|
||||
Serial.print("Centre joystick droit Y = ");
|
||||
Serial.println(centerRightY);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
Serial.println();
|
||||
Serial.println("===== TEST BLE COMPOSITE HID + JOYSTICK DROIT =====");
|
||||
|
||||
analogReadResolution(12);
|
||||
|
||||
calibrateRightJoystick();
|
||||
|
||||
compositeHID.addDevice(&gamepad);
|
||||
compositeHID.begin();
|
||||
|
||||
Serial.println("BLE demarre.");
|
||||
Serial.println("Nom Bluetooth : Modulx Controller V4");
|
||||
Serial.println("Joystick droit : VRX GPIO34 / VRY GPIO35");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static bool lastConnected = false;
|
||||
bool connected = compositeHID.isConnected();
|
||||
|
||||
if (connected != lastConnected) {
|
||||
if (connected) {
|
||||
Serial.println("[BLE] Connecte !");
|
||||
} else {
|
||||
Serial.println("[BLE] Deconnecte.");
|
||||
}
|
||||
|
||||
lastConnected = connected;
|
||||
}
|
||||
|
||||
int rawX = analogRead(JOY_RIGHT_X);
|
||||
int rawY = analogRead(JOY_RIGHT_Y);
|
||||
|
||||
int16_t rightX = readJoystickAxisCalibrated(JOY_RIGHT_X, centerRightX);
|
||||
int16_t rightY = readJoystickAxisCalibrated(JOY_RIGHT_Y, centerRightY);
|
||||
|
||||
if (millis() - lastPrint >= PRINT_INTERVAL) {
|
||||
lastPrint = millis();
|
||||
|
||||
Serial.print("[JOY DROIT] raw X=");
|
||||
Serial.print(rawX);
|
||||
Serial.print(" raw Y=");
|
||||
Serial.print(rawY);
|
||||
|
||||
Serial.print(" | center X=");
|
||||
Serial.print(centerRightX);
|
||||
Serial.print(" center Y=");
|
||||
Serial.print(centerRightY);
|
||||
|
||||
Serial.print(" | mapped X=");
|
||||
Serial.print(rightX);
|
||||
Serial.print(" mapped Y=");
|
||||
Serial.println(rightY);
|
||||
}
|
||||
|
||||
if (connected) {
|
||||
gamepad.setRightThumb(rightX, rightY);
|
||||
gamepad.sendGamepadReport();
|
||||
delay(20);
|
||||
} else {
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
0
test_joystick_gauche.cpp
Normal file
0
test_joystick_gauche.cpp
Normal file
Reference in New Issue
Block a user