From 0a7aeb2a0791147b2169f6a692a726b43e5e85e8 Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sat, 24 Aug 2024 18:34:49 +0200 Subject: [PATCH] added the plane rudder --- SkyControlSuite.hid | Bin 0 -> 124 bytes SkyControlSuite/SkyControlSuite.h | 18 ++++++ SkyControlSuite/SkyControlSuite.ino | 90 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 SkyControlSuite.hid create mode 100644 SkyControlSuite/SkyControlSuite.h create mode 100644 SkyControlSuite/SkyControlSuite.ino diff --git a/SkyControlSuite.hid b/SkyControlSuite.hid new file mode 100644 index 0000000000000000000000000000000000000000..25929f398b23b0561c69a8b697887999bbf859fc GIT binary patch literal 124 zcmY#lU}X4rij#qh0T*Cp0?K>_$u0yDU>4^t1}28DAg(Bg0JGHoGecRWAj807QyF2b QMvy6Bu>(K`!xs<%08mE{nE(I) literal 0 HcmV?d00001 diff --git a/SkyControlSuite/SkyControlSuite.h b/SkyControlSuite/SkyControlSuite.h new file mode 100644 index 0000000..89f97cd --- /dev/null +++ b/SkyControlSuite/SkyControlSuite.h @@ -0,0 +1,18 @@ +// C:\Users\valen\Documents\Projets\SkyControlSuite\SkyControlSuite\SkyControlSuite.h + + +char ReportDescriptor[23] = { + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x04, // USAGE (Joystick) + 0xa1, 0x01, // COLLECTION (Application) + 0x05, 0x02, // USAGE_PAGE (Simulation Controls) + 0x09, 0xba, // USAGE (Rudder) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x85, 0x01, // REPORT_ID (1) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0xc0 // END_COLLECTION +}; + diff --git a/SkyControlSuite/SkyControlSuite.ino b/SkyControlSuite/SkyControlSuite.ino new file mode 100644 index 0000000..7465e58 --- /dev/null +++ b/SkyControlSuite/SkyControlSuite.ino @@ -0,0 +1,90 @@ +#include + +static const uint8_t ReportDescriptor[] PROGMEM = { + + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x04, // USAGE (Joystick) + 0xa1, 0x01, // COLLECTION (Application) + 0x05, 0x02, // USAGE_PAGE (Simulation Controls) + 0x09, 0xba, // USAGE (Rudder) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x85, 0x01, // REPORT_ID (1) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0xc0 // END_COLLECTION +}; + +const int rudderPin = A0; + +const int sensibility = 5; +const int maxRudder = 1023; +const int minRudder = 20; + +int middle = (maxRudder - minRudder) / 2; + +float smoothedValue = 0; // Valeur lissée; + +const int numReadings = 10; // Nombre d'échantillons pour la moyenne +int readings[numReadings]; // Tableau pour stocker les valeurs des échantillons + +int readIndex = 0; // Index pour le tableau +int total = 0; // Total des valeurs lues +int average = 0; // Valeur moyenne + +int oldAverage = average; + +uint8_t data[1]; + +void setup() { + static HIDSubDescriptor node(ReportDescriptor, sizeof(ReportDescriptor)); + HID().AppendDescriptor(&node); + + // Initialiser le tableau de lectures + for (int i = 0; i < numReadings; i++) { + readings[i] = 0; + } + + //Serial.begin(9600); + + pinMode(rudderPin, INPUT_PULLUP); + pinMode(LED_BUILTIN, OUTPUT); +} + +void loop() { + // Soustraire la lecture précédente de la somme + total = total - readings[readIndex]; + + // Lire la valeur du potentiomètre + readings[readIndex] = analogRead(rudderPin); + + // Ajouter la lecture actuelle à la somme + total = total + readings[readIndex]; + + // Calculer la moyenne + average = total / numReadings; + + //Serial.println(average); + + // Send value to the PC + int8_t mapped = map(average, minRudder, maxRudder, -127, 127); + int8_t value = constrain(mapped, -127, 127); + + data[0] = value; + //Serial.println(value); + HID().SendReport(0x01, data, sizeof(data)); + + // Switch on the LED if it's in the middle + digitalWrite(LED_BUILTIN, abs(value) <= sensibility); + + oldAverage = average; + + // Avancer à la lecture suivante dans le tableau + readIndex = readIndex + 1; + + // Recommencer au début du tableau si nécessaire + if (readIndex >= numReadings) { + readIndex = 0; + } +}