From eabf3e1130eaea68be7007201053553674545b45 Mon Sep 17 00:00:00 2001 From: bleackstorm <49142331+bleackstorm@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:32:37 +0100 Subject: [PATCH] Update g29_crsf.ino --- g29_crsf.ino | 422 +++++++++++++++++++++++---------------------------- 1 file changed, 194 insertions(+), 228 deletions(-) diff --git a/g29_crsf.ino b/g29_crsf.ino index f2d3b9b..53c8846 100644 --- a/g29_crsf.ino +++ b/g29_crsf.ino @@ -5,44 +5,43 @@ #include // Brochage natif de la Waveshare RP2350-USB-A -#define HOST_PIN_DP 12 // USB-A D+ (D- est automatiquement sur GP13) -#define NUM_PIXELS 1 -#define PIN_LED_RGB 16 // LED RGB WS2812 intégrée -#define EEPROM_SIZE 32 +#define HOST_PIN_DP 12 // USB-A D+ (D- est automatiquement sur GP13) +#define NUM_PIXELS 1 +#define PIN_LED_RGB 16 // LED RGB WS2812 intégrée +#define EEPROM_SIZE 32 // Masques des palettes dans report[6] -#define PALETTE_MOINS_MASK 0x01 // L1 (Palette gauche : Rétrograder) -#define PALETTE_PLUS_MASK 0x02 // R1 (Palette droite : Monter un rapport) +#define PALETTE_MOINS_MASK 0x01 // L1 (Palette gauche : Rétrograder) +#define PALETTE_PLUS_MASK 0x02 // R1 (Palette droite : Monter un rapport) // Bornes officielles CRSF -#define CRSF_VAL_MIN 172 // ~988 us -#define CRSF_VAL_MID 992 // 1500 us (Neutre) -#define CRSF_VAL_MAX 1811 // ~2012 us +#define CRSF_VAL_MIN 172 // ~988 us +#define CRSF_VAL_MID 992 // 1500 us (Neutre) +#define CRSF_VAL_MAX 1811 // ~2012 us // Plages de gaz progressives (Marche avant : rapports 1, 2, 3) -#define GAZ_AVANT_MIN 1065 +#define GAZ_AVANT_MIN 1065 const uint16_t GAZ_AVANT_MAX_PAR_VITESSE[3] = { - 1100, // 1ère vitesse (Manœuvre) - 1150, // 2ème vitesse (Normale) - 1220 // 3ème vitesse (Rapide) + 1100, // 1ère vitesse (Manœuvre) + 1150, // 2ème vitesse (Normale) + 1220 // 3ème vitesse (Rapide) }; -#define GAZ_ARRIERE_MIN 905 -#define GAZ_ARRIERE_MAX 850 -#define DEADZONE_PEDALE 800 +#define GAZ_ARRIERE_MIN 905 +#define GAZ_ARRIERE_MAX 850 +#define DEADZONE_PEDALE 800 // Configuration de la plage de braquage et du retour de force (Auto-Center) du G29 -#define CENTRE_VOLANT_BRUT 32528 -#define ANGLE_MAX_VOLANT_G29 900.0f -#define ANGLE_SOUHAITE_TOTAL 180.0f // Butée logicielle ET matérielle (180° = ±90°) -#define EXPO_VOLANT 0.25f // 0.0f = Linéaire, 0.25f = Doux au centre (FPU RP2350) +#define CENTRE_VOLANT_BRUT 32528 +#define ANGLE_MAX_VOLANT_G29 900.0f +#define ANGLE_SOUHAITE_TOTAL 180.0f +#define EXPO_VOLANT 0.25f // 0.0f = Linéaire, 0.25f = Doux au centre (FPU RP2350) -#define FORCE_RESSORT_G29 140 // Force du retour au centre : 0 (mou) à 255 (très ferme) -#define PENTE_RESSORT_G29 7 // Raideur autour du centre : 1 (souple) à 7 (très franc) +#define FORCE_RESSORT_G29 140 // Force du retour au centre : 0 à 255 +#define PENTE_RESSORT_G29 7 // Raideur autour du centre : 1 à 7 const int32_t MAX_DELTA_VOLANT = (int32_t)((ANGLE_SOUHAITE_TOTAL / 2.0f) * (65535.0f / ANGLE_MAX_VOLANT_G29)); -// Note Waveshare : remplacer NEO_GRB par NEO_RGB si le Rouge et le Vert sont inversés Adafruit_NeoPixel rgb_led(NUM_PIXELS, PIN_LED_RGB, NEO_GRB + NEO_KHZ800); Adafruit_USBH_Host USBHost; @@ -58,14 +57,14 @@ struct MapCanaux { uint8_t accel; uint8_t frein; uint8_t embrayage; - uint8_t magicNumber; + uint8_t magicNumber; }; MapCanaux configCanaux; // Boîte séquentielle : -1 = R, 0 = N (Point Mort), 1 = 1ère, 2 = 2ème, 3 = 3ème -volatile int8_t rapportEngage = 0; -volatile bool palettePlusDejaEnfoncee = false; +volatile int8_t rapportEngage = 0; +volatile bool palettePlusDejaEnfoncee = false; volatile bool paletteMoinsDejaEnfoncee = false; volatile bool majLedRequise = true; @@ -79,11 +78,11 @@ volatile bool volant_est_pret = false; volatile uint32_t dernierRapportUSB_ms = 0; volatile uint32_t momentConnexionUSB_ms = 0; volatile bool logMountRequis = false; -volatile int8_t statutEnvoiLed = 0; +volatile int8_t statutEnvoiLed = 0; // <-- Déclaration ajoutée ici uint8_t dernierMasqueLedG29 = 0xFF; uint8_t etapeEnvoiLed = 0; -uint8_t etapeInitMoteur = 0; // 0..5 = Séquence Auto-Center & 180°, 6 = Terminé +uint8_t etapeInitMoteur = 0; char serialBuffer[32]; uint8_t serialBufferIdx = 0; @@ -95,7 +94,7 @@ void reinitialiserCanauxCRSF() { } void sauvegarderConfiguration() { - rp2040.idleOtherCore(); // Sécurité XIP Flash pour RP2350 Dual-Core + rp2040.idleOtherCore(); EEPROM.put(0, configCanaux); EEPROM.commit(); rp2040.resumeOtherCore(); @@ -108,16 +107,15 @@ void afficherConfiguration() { Serial.printf("Accel (A) : Canal %d\n", configCanaux.accel); Serial.printf("Frein (F) : Canal %d\n", configCanaux.frein); Serial.printf("Embrayage (E) : Canal %d\n", configCanaux.embrayage); - Serial.println("Commandes : V=x, A=x, F=x, E=x (1..16) ou '?'"); } void chargerConfiguration() { EEPROM.get(0, configCanaux); if (configCanaux.magicNumber != 42) { - configCanaux.volant = 1; - configCanaux.accel = 2; - configCanaux.frein = 3; - configCanaux.embrayage = 4; + configCanaux.volant = 1; + configCanaux.accel = 2; + configCanaux.frein = 3; + configCanaux.embrayage = 4; configCanaux.magicNumber = 42; sauvegarderConfiguration(); } @@ -137,14 +135,10 @@ void traiterLigneSerie(char* commande) { if (nouveauCanal >= 1 && nouveauCanal <= 16) { bool modifOk = true; switch (periph) { - case 'V': - case 'v': configCanaux.volant = nouveauCanal; break; - case 'A': - case 'a': configCanaux.accel = nouveauCanal; break; - case 'F': - case 'f': configCanaux.frein = nouveauCanal; break; - case 'E': - case 'e': configCanaux.embrayage = nouveauCanal; break; + case 'V': case 'v': configCanaux.volant = nouveauCanal; break; + case 'A': case 'a': configCanaux.accel = nouveauCanal; break; + case 'F': case 'f': configCanaux.frein = nouveauCanal; break; + case 'E': case 'e': configCanaux.embrayage = nouveauCanal; break; default: modifOk = false; break; } if (modifOk) { @@ -176,21 +170,21 @@ void verifierCommandesSerie() { // ------------------------------------------------------------------------- void actualiserLED() { if (!volant_est_pret) { - rgb_led.setPixelColor(0, rgb_led.Color(0, 0, 255)); // Bleu : Attente G29 + rgb_led.setPixelColor(0, rgb_led.Color(0, 0, 255)); // Bleu : Attente G29 } else { switch (rapportEngage) { - case -1: rgb_led.setPixelColor(0, rgb_led.Color(255, 0, 0)); break; // Rouge : R - case 0: rgb_led.setPixelColor(0, rgb_led.Color(255, 100, 0)); break; // Orange : N - case 1: rgb_led.setPixelColor(0, rgb_led.Color(0, 255, 255)); break; // Cyan : 1ère - case 2: rgb_led.setPixelColor(0, rgb_led.Color(0, 255, 0)); break; // Vert : 2ème - case 3: rgb_led.setPixelColor(0, rgb_led.Color(180, 0, 255)); break; // Violet : 3ème + case -1: rgb_led.setPixelColor(0, rgb_led.Color(255, 0, 0)); break; // Rouge : R + case 0: rgb_led.setPixelColor(0, rgb_led.Color(255, 100, 0)); break; // Orange : N + case 1: rgb_led.setPixelColor(0, rgb_led.Color(0, 255, 255)); break; // Cyan : 1ère + case 2: rgb_led.setPixelColor(0, rgb_led.Color(0, 255, 0)); break; // Vert : 2ème + case 3: rgb_led.setPixelColor(0, rgb_led.Color(180, 0, 255)); break; // Violet : 3ème } } rgb_led.show(); } // ------------------------------------------------------------------------- -// ENVOI CRSF DEPUIS LA SRAM DU RP2350 (Zéro latence Flash) +// ENVOI CRSF DEPUIS LA SRAM DU RP2350 // ------------------------------------------------------------------------- void __not_in_flash_func(envoyer_paquet_crsf)() { uint8_t packet[26]; @@ -199,32 +193,32 @@ void __not_in_flash_func(envoyer_paquet_crsf)() { for (int i = 0; i < 16; i++) { ch[i] = crsf_channels[i]; } + + packet[0] = 0xC8; + packet[1] = 24; + packet[2] = 0x16; - packet[0] = 0xC8; - packet[1] = 24; - packet[2] = 0x16; - - packet[3] = (uint8_t)(ch[0]); - packet[4] = (uint8_t)((ch[0] >> 8) | (ch[1] << 3)); - packet[5] = (uint8_t)((ch[1] >> 5) | (ch[2] << 6)); - packet[6] = (uint8_t)(ch[2] >> 2); - packet[7] = (uint8_t)((ch[2] >> 10) | (ch[3] << 1)); - packet[8] = (uint8_t)((ch[3] >> 7) | (ch[4] << 4)); - packet[9] = (uint8_t)((ch[4] >> 4) | (ch[5] << 7)); + packet[3] = (uint8_t)(ch[0]); + packet[4] = (uint8_t)((ch[0] >> 8) | (ch[1] << 3)); + packet[5] = (uint8_t)((ch[1] >> 5) | (ch[2] << 6)); + packet[6] = (uint8_t)(ch[2] >> 2); + packet[7] = (uint8_t)((ch[2] >> 10) | (ch[3] << 1)); + packet[8] = (uint8_t)((ch[3] >> 7) | (ch[4] << 4)); + packet[9] = (uint8_t)((ch[4] >> 4) | (ch[5] << 7)); packet[10] = (uint8_t)(ch[5] >> 1); - packet[11] = (uint8_t)((ch[5] >> 9) | (ch[6] << 2)); - packet[12] = (uint8_t)((ch[6] >> 6) | (ch[7] << 5)); + packet[11] = (uint8_t)((ch[5] >> 9) | (ch[6] << 2)); + packet[12] = (uint8_t)((ch[6] >> 6) | (ch[7] << 5)); packet[13] = (uint8_t)(ch[7] >> 3); packet[14] = (uint8_t)(ch[8]); - packet[15] = (uint8_t)((ch[8] >> 8) | (ch[9] << 3)); - packet[16] = (uint8_t)((ch[9] >> 5) | (ch[10] << 6)); + packet[15] = (uint8_t)((ch[8] >> 8) | (ch[9] << 3)); + packet[16] = (uint8_t)((ch[9] >> 5) | (ch[10] << 6)); packet[17] = (uint8_t)(ch[10] >> 2); packet[18] = (uint8_t)((ch[10] >> 10) | (ch[11] << 1)); - packet[19] = (uint8_t)((ch[11] >> 7) | (ch[12] << 4)); - packet[20] = (uint8_t)((ch[12] >> 4) | (ch[13] << 7)); + packet[19] = (uint8_t)((ch[11] >> 7) | (ch[12] << 4)); + packet[20] = (uint8_t)((ch[12] >> 4) | (ch[13] << 7)); packet[21] = (uint8_t)(ch[13] >> 1); - packet[22] = (uint8_t)((ch[13] >> 9) | (ch[14] << 2)); - packet[23] = (uint8_t)((ch[14] >> 6) | (ch[15] << 5)); + packet[22] = (uint8_t)((ch[13] >> 9) | (ch[14] << 2)); + packet[23] = (uint8_t)((ch[14] >> 6) | (ch[15] << 5)); packet[24] = (uint8_t)(ch[15] >> 3); uint8_t crc = 0; @@ -241,7 +235,7 @@ void __not_in_flash_func(envoyer_paquet_crsf)() { } // ========================================================================= -// CŒUR 0 : Initialisation rapide, CRSF 166Hz, Série, EEPROM et LED RGB +// CŒUR 0 : Initialisation, CRSF 166Hz, Série, EEPROM et LED RGB // ========================================================================= void setup() { Serial.begin(115200); @@ -251,12 +245,12 @@ void setup() { rgb_led.setBrightness(50); actualiserLED(); - delay(500); + delay(500); chargerConfiguration(); Serial1.setTX(0); Serial1.setRX(1); - Serial1.begin(420000); + Serial1.begin(420000); Serial.println("\n--- WAVESHARE RP2350-USB-A PRETE ---"); afficherConfiguration(); @@ -274,134 +268,107 @@ void loop() { if (logMountRequis) { logMountRequis = false; tuh_vid_pid_get(g29_dev_addr, (uint16_t*)&g29_vid, (uint16_t*)&g29_pid); - Serial.printf("[USB HOST] G29 detecte ! VID=%04X, PID=%04X, dev=%d, inst=%d, desc_len=%d\n", + Serial.printf("[USB HOST] G29 detecte ! VID=%04X, PID=%04X, dev=%d, inst=%d, desc_len=%d\n", g29_vid, g29_pid, g29_dev_addr, g29_instance, g29_desc_len); } - // Heartbeat toutes les 2 secondes dans le Moniteur Série static uint32_t dernierHeartbeat = 0; if (millis() - dernierHeartbeat >= 2000) { dernierHeartbeat = millis(); if (volant_est_pret && g29_vid == 0) { tuh_vid_pid_get(g29_dev_addr, (uint16_t*)&g29_vid, (uint16_t*)&g29_pid); } - Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/6 | Rapport=%d | Statut_LED=%d\n", + Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/6 | Rapport=%d | Statut_LED=%d\n", volant_est_pret, g29_pid, etapeInitMoteur, rapportEngage, statutEnvoiLed); } static uint32_t dernier_envoi = 0; - if (millis() - dernier_envoi >= 6) { // Envoi CRSF à ~166 Hz + if (millis() - dernier_envoi >= 6) { dernier_envoi = millis(); envoyer_paquet_crsf(); } } // ========================================================================= - -// CŒUR 1 : PILE PIO-USB ET ENVOI MULTI-FORMAT DES LEDS G29 - +// CŒUR 1 : PILE PIO-USB, AUTO-CENTER ET LEDS G29 // ========================================================================= +bool envoyerTrameG29_F3_Simple(const uint8_t cmd7[7]) { + if (!tuh_hid_send_ready(g29_dev_addr, g29_instance)) return false; + static uint8_t payload63[63]; + memset(payload63, 0, sizeof(payload63)); + memcpy(payload63, cmd7, 7); + return tuh_hid_send_report(g29_dev_addr, g29_instance, 0xF3, payload63, 63); +} + +void gererInitMoteurG29() { + if (!volant_est_pret || etapeInitMoteur >= 6) return; + if (millis() - momentConnexionUSB_ms < 600) return; + + uint16_t angle = (uint16_t)ANGLE_SOUHAITE_TOTAL; + const uint8_t cmdRange[7] = {0xF8, 0x81, (uint8_t)(angle & 0xFF), (uint8_t)(angle >> 8), 0x00, 0x00, 0x00}; + const uint8_t cmdSpring[7] = {0xFE, 0x0D, PENTE_RESSORT_G29 & 0x07, PENTE_RESSORT_G29 & 0x07, FORCE_RESSORT_G29, 0x00, 0x00}; + const uint8_t cmdEnable[7] = {0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + switch (etapeInitMoteur) { + case 0: if (envoyerTrameG29_F3_Simple(cmdRange)) etapeInitMoteur++; break; + case 1: if (envoyerTrameG29_F3_Simple(cmdSpring)) etapeInitMoteur++; break; + case 2: if (envoyerTrameG29_F3_Simple(cmdEnable)) etapeInitMoteur++; break; + case 3: etapeInitMoteur = 6; break; + } +} void envoyerLedsG29(uint8_t masque5bits) { - if (!volant_est_pret) return; - if (millis() - momentConnexionUSB_ms < 500) return; - // Dès que le masque change, lance la séquence d'envoi pour PID=C260 if (masque5bits != dernierMasqueLedG29) { - dernierMasqueLedG29 = masque5bits; - etapeEnvoiLed = 1; } if (etapeEnvoiLed == 0 || !tuh_hid_send_ready(g29_dev_addr, g29_instance)) return; uint8_t m = dernierMasqueLedG29 & 0x1F; - // On prépare un payload de 63 octets (9 blocs de 7 octets identiques : 9 x 7 = 63 octets) - - // Avec le Report ID ajouté par TinyUSB (1 octet), le paquet USB fera exactement 64 octets ! static uint8_t payload63[63]; - for (int i = 0; i < 63; i += 7) { - payload63[i + 0] = 0xF8; - payload63[i + 1] = 0x12; - payload63[i + 2] = m; - payload63[i + 3] = 0x00; - payload63[i + 4] = 0x00; - payload63[i + 5] = 0x00; - payload63[i + 6] = 0x00; } + if (etapeEnvoiLed == 1) { - - // ESSAI 1 : Report ID 0xF3 + 63 octets (Total USB = 64 octets) - if (tuh_hid_send_report(g29_dev_addr, g29_instance, 0xF3, payload63, 63)) { - statutEnvoiLed = 1; - etapeEnvoiLed = 2; } - - } - + } else if (etapeEnvoiLed == 2) { - - // ESSAI 2 : Report ID 0x30 + 63 octets (Utilisé sur certains descripteurs PS4 G29) - if (tuh_hid_send_report(g29_dev_addr, g29_instance, 0x30, payload63, 63)) { - statutEnvoiLed = 2; - etapeEnvoiLed = 3; } - - } - + } else if (etapeEnvoiLed == 3) { - - // ESSAI 3 : Report ID 0x05 + 31 octets (Rapport standard DualShock 4 Output) - if (tuh_hid_send_report(g29_dev_addr, g29_instance, 0x05, payload63, 31)) { - statutEnvoiLed = 3; - etapeEnvoiLed = 4; } - } - else if (etapeEnvoiLed == 4) { - - // ESSAI 4 : Décalage d'un octet d'en-tête (0x00 en premier octet après 0xF3) - static uint8_t payloadDecale[63]; - memset(payloadDecale, 0, sizeof(payloadDecale)); - - payloadDecale[0] = 0x00; // Header count/zero - + payloadDecale[0] = 0x00; for (int i = 1; i + 7 <= 63; i += 7) { - payloadDecale[i + 0] = 0xF8; - payloadDecale[i + 1] = 0x12; - payloadDecale[i + 2] = m; } - if (tuh_hid_send_report(g29_dev_addr, g29_instance, 0xF3, payloadDecale, 63)) { - statutEnvoiLed = 4; - - etapeEnvoiLed = 0; // Séquence terminée + etapeEnvoiLed = 0; } } } @@ -421,13 +388,18 @@ void loop1() { if (volant_est_pret && (millis() - dernierTickG29 >= 50)) { dernierTickG29 = millis(); + if (etapeInitMoteur < 6) { + gererInitMoteurG29(); + return; + } + uint8_t masqueCible = 0x00; switch (rapportEngage) { - case -1: masqueCible = ((millis() / 250) % 2 == 0) ? 0x1F : 0x00; break; // Clignotement R - case 0: masqueCible = 0x00; break; // Point Mort : éteint - case 1: masqueCible = 0x01; break; // 1ère : 1 LED - case 2: masqueCible = 0x03; break; // 2ème : 2 LEDs - case 3: masqueCible = 0x07; break; // 3ème : 3 LEDs + case -1: masqueCible = ((millis() / 250) % 2 == 0) ? 0x1F : 0x00; break; + case 0: masqueCible = 0x00; break; + case 1: masqueCible = 0x01; break; + case 2: masqueCible = 0x03; break; + case 3: masqueCible = 0x07; break; } envoyerLedsG29(masqueCible); } @@ -438,112 +410,106 @@ void loop1() { // ========================================================================= extern "C" { - void assignerCanalCRSF(uint8_t numeroCh, uint16_t valeur11bits) { - if (numeroCh >= 1 && numeroCh <= 16) { - crsf_channels[numeroCh - 1] = valeur11bits; - } +void assignerCanalCRSF(uint8_t numeroCh, uint16_t valeur11bits) { + if (numeroCh >= 1 && numeroCh <= 16) { + crsf_channels[numeroCh - 1] = valeur11bits; } +} - void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { - (void)desc_report; - g29_dev_addr = dev_addr; - g29_instance = instance; - g29_desc_len = desc_len; - dernierMasqueLedG29 = 0xFF; - etapeEnvoiLed = 0; - etapeInitMoteur = 0; // Lance la configuration Butées 180° + Auto-Center - momentConnexionUSB_ms = millis(); +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { + (void)desc_report; + g29_dev_addr = dev_addr; + g29_instance = instance; + g29_desc_len = desc_len; + dernierMasqueLedG29 = 0xFF; + etapeEnvoiLed = 0; + etapeInitMoteur = 0; + momentConnexionUSB_ms = millis(); + dernierRapportUSB_ms = millis(); + volant_est_pret = true; + majLedRequise = true; + logMountRequis = true; + + tuh_hid_receive_report(dev_addr, instance); +} + +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { + (void)dev_addr; + (void)instance; + volant_est_pret = false; + g29_vid = 0; + g29_pid = 0; + etapeInitMoteur = 0; + rapportEngage = 0; + reinitialiserCanauxCRSF(); + majLedRequise = true; +} + +void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { + if (len >= 51) { dernierRapportUSB_ms = millis(); - volant_est_pret = true; - majLedRequise = true; - logMountRequis = true; - tuh_hid_receive_report(dev_addr, instance); - } + uint16_t volantRaw = (report[44] << 8) | report[43]; + uint16_t accelRaw = 65535 - ((report[46] << 8) | report[45]); + uint16_t freinRaw = 65535 - ((report[48] << 8) | report[47]); + uint16_t embrayageRaw = 65535 - ((report[50] << 8) | report[49]); - void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { - (void)dev_addr; - (void)instance; - volant_est_pret = false; - g29_vid = 0; - g29_pid = 0; - etapeInitMoteur = 0; - rapportEngage = 0; // Retour automatique au Point Mort si câble USB débranché - reinitialiserCanauxCRSF(); - majLedRequise = true; - } + uint8_t boutonsEpaule = report[6]; + bool pPlusActuel = (boutonsEpaule & PALETTE_PLUS_MASK); + bool pMoinsActuel = (boutonsEpaule & PALETTE_MOINS_MASK); - void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { - if (len >= 51) { - dernierRapportUSB_ms = millis(); - - // 1. Décodage brut volant et pédales - uint16_t volantRaw = (report[44] << 8) | report[43]; - uint16_t accelRaw = 65535 - ((report[46] << 8) | report[45]); - uint16_t freinRaw = 65535 - ((report[48] << 8) | report[47]); - uint16_t embrayageRaw = 65535 - ((report[50] << 8) | report[49]); - - // 2. Gestion des palettes (report[6]) - uint8_t boutonsEpaule = report[6]; - bool pPlusActuel = (boutonsEpaule & PALETTE_PLUS_MASK); - bool pMoinsActuel = (boutonsEpaule & PALETTE_MOINS_MASK); - - if (pPlusActuel && !palettePlusDejaEnfoncee) { - if (rapportEngage < 3 && !(rapportEngage < 0 && accelRaw > DEADZONE_PEDALE)) { - rapportEngage++; - majLedRequise = true; - } - palettePlusDejaEnfoncee = true; - } else if (!pPlusActuel && palettePlusDejaEnfoncee) { - palettePlusDejaEnfoncee = false; + if (pPlusActuel && !palettePlusDejaEnfoncee) { + if (rapportEngage < 3 && !(rapportEngage < 0 && accelRaw > DEADZONE_PEDALE)) { + rapportEngage++; + majLedRequise = true; } + palettePlusDejaEnfoncee = true; + } else if (!pPlusActuel && palettePlusDejaEnfoncee) { + palettePlusDejaEnfoncee = false; + } - if (pMoinsActuel && !paletteMoinsDejaEnfoncee) { - if (rapportEngage > -1 && !(rapportEngage <= 0 && accelRaw > DEADZONE_PEDALE)) { - rapportEngage--; - majLedRequise = true; - } - paletteMoinsDejaEnfoncee = true; - } else if (!pMoinsActuel && paletteMoinsDejaEnfoncee) { - paletteMoinsDejaEnfoncee = false; + if (pMoinsActuel && !paletteMoinsDejaEnfoncee) { + if (rapportEngage > -1 && !(rapportEngage <= 0 && accelRaw > DEADZONE_PEDALE)) { + rapportEngage--; + majLedRequise = true; } + paletteMoinsDejaEnfoncee = true; + } else if (!pMoinsActuel && paletteMoinsDejaEnfoncee) { + paletteMoinsDejaEnfoncee = false; + } - // 3. Volant avec Expo calculée par la FPU matérielle du RP2350 - int32_t volantCentre = (int32_t)volantRaw - CENTRE_VOLANT_BRUT; - int32_t volantBorne = constrain(volantCentre, -MAX_DELTA_VOLANT, MAX_DELTA_VOLANT); + int32_t volantCentre = (int32_t)volantRaw - CENTRE_VOLANT_BRUT; + int32_t volantBorne = constrain(volantCentre, -MAX_DELTA_VOLANT, MAX_DELTA_VOLANT); + + float x = (float)volantBorne / (float)MAX_DELTA_VOLANT; + float y = x * (1.0f - EXPO_VOLANT + EXPO_VOLANT * (x * x)); + uint16_t vVolant = constrain((int32_t)(CRSF_VAL_MID - y * (CRSF_VAL_MAX - CRSF_VAL_MID)), CRSF_VAL_MIN, CRSF_VAL_MAX); - float x = (float)volantBorne / (float)MAX_DELTA_VOLANT; // [-1.0f .. +1.0f] - float y = x * (1.0f - EXPO_VOLANT + EXPO_VOLANT * (x * x)); - uint16_t vVolant = constrain((int32_t)(CRSF_VAL_MID - y * (CRSF_VAL_MAX - CRSF_VAL_MID)), CRSF_VAL_MIN, CRSF_VAL_MAX); - - // 4. Accélérateur progressif selon le rapport engagé - uint16_t vAccel = CRSF_VAL_MID; - if (accelRaw > DEADZONE_PEDALE) { - if (rapportEngage == -1) { - vAccel = constrain(map(accelRaw, DEADZONE_PEDALE, 65535, GAZ_ARRIERE_MIN, GAZ_ARRIERE_MAX), CRSF_VAL_MIN, CRSF_VAL_MAX); - } else if (rapportEngage >= 1 && rapportEngage <= 3) { - uint16_t maxVitesse = GAZ_AVANT_MAX_PAR_VITESSE[rapportEngage - 1]; - vAccel = constrain(map(accelRaw, DEADZONE_PEDALE, 65535, GAZ_AVANT_MIN, maxVitesse), CRSF_VAL_MIN, CRSF_VAL_MAX); - } + uint16_t vAccel = CRSF_VAL_MID; + if (accelRaw > DEADZONE_PEDALE) { + if (rapportEngage == -1) { + vAccel = constrain(map(accelRaw, DEADZONE_PEDALE, 65535, GAZ_ARRIERE_MIN, GAZ_ARRIERE_MAX), CRSF_VAL_MIN, CRSF_VAL_MAX); + } else if (rapportEngage >= 1 && rapportEngage <= 3) { + uint16_t maxVitesse = GAZ_AVANT_MAX_PAR_VITESSE[rapportEngage - 1]; + vAccel = constrain(map(accelRaw, DEADZONE_PEDALE, 65535, GAZ_AVANT_MIN, maxVitesse), CRSF_VAL_MIN, CRSF_VAL_MAX); } + } - // 5. Frein et Embrayage - uint16_t vFrein = (freinRaw > DEADZONE_PEDALE) - ? constrain(map(freinRaw, DEADZONE_PEDALE, 65535, CRSF_VAL_MID, CRSF_VAL_MAX), CRSF_VAL_MIN, CRSF_VAL_MAX) + uint16_t vFrein = (freinRaw > DEADZONE_PEDALE) + ? constrain(map(freinRaw, DEADZONE_PEDALE, 65535, CRSF_VAL_MID, CRSF_VAL_MAX), CRSF_VAL_MIN, CRSF_VAL_MAX) + : CRSF_VAL_MID; + + uint16_t vEmbrayage = (embrayageRaw > DEADZONE_PEDALE) + ? constrain(map(embrayageRaw, DEADZONE_PEDALE, 65535, CRSF_VAL_MID, CRSF_VAL_MAX), CRSF_VAL_MIN, CRSF_VAL_MAX) : CRSF_VAL_MID; - uint16_t vEmbrayage = (embrayageRaw > DEADZONE_PEDALE) - ? constrain(map(embrayageRaw, DEADZONE_PEDALE, 65535, CRSF_VAL_MID, CRSF_VAL_MAX), CRSF_VAL_MIN, CRSF_VAL_MAX) - : CRSF_VAL_MID; - - // 6. Assignation CRSF - assignerCanalCRSF(configCanaux.volant, vVolant); - assignerCanalCRSF(configCanaux.accel, vAccel); - assignerCanalCRSF(configCanaux.frein, vFrein); - assignerCanalCRSF(configCanaux.embrayage, vEmbrayage); - } - - tuh_hid_receive_report(dev_addr, instance); + assignerCanalCRSF(configCanaux.volant, vVolant); + assignerCanalCRSF(configCanaux.accel, vAccel); + assignerCanalCRSF(configCanaux.frein, vFrein); + assignerCanalCRSF(configCanaux.embrayage, vEmbrayage); } -} // extern "C" + tuh_hid_receive_report(dev_addr, instance); +} + +} // extern "C"