#pragma once // Forward declarations (fonctions définies dans crsf_protocol.h et g29_hid.h) void actualiserLED(); bool envoyerPlageG29(uint16_t angle); bool envoyerRessortG29(uint8_t force); bool envoyerForceG29(uint8_t forceByte); bool arreterForceG29(); void gererInitMoteurG29(); // ------------------------------------------------------------------------- // GESTION SÉRIE NON BLOQUANTE // ------------------------------------------------------------------------- void demanderOuExecuterSauvegarde() { if (estActuellementArme()) { sauvegardeDifferee = true; Serial.println(">> Modification appliquee (sauvegarde Flash differee au desarmement)."); } else { momentDernierChangementDurete_ms = 0; sauvegardeDifferee = false; sauvegarderConfiguration(); } } static bool parseStrictInt(const char* s, int& outVal) { if (!s || *s == '\0') return false; char* endPtr = nullptr; long v = strtol(s, &endPtr, 10); if (endPtr == s || *endPtr != '\0') return false; outVal = (int)v; return true; } static bool parseStrictFloat(const char* s, float& outVal) { if (!s || *s == '\0') return false; char* endPtr = nullptr; float v = strtof(s, &endPtr); if (endPtr == s || *endPtr != '\0' || isnan(v) || isinf(v)) return false; outVal = v; return true; } uint8_t calculerCrc8Crsf(const uint8_t* data, size_t len); void traiterLigneSerie(char* commande, bool depuisUsb) { // Sécurité : les commandes CLI sans CRC (B=BOOTSEL, R=Reset, V=/A=/F=/E=...) ne sont // acceptées QUE sur l'USB local (Serial), jamais sur l'UART Radxa / UDP (Serial2). if (!depuisUsb && commande[0] != '$') { return; } if (commande[0] == '?') { afficherConfiguration(); return; } if (commande[0] == 'B' || commande[0] == 'b') { if (estActuellementArme()) { Serial.println("!! Redemarrage BOOTSEL refuse pendant la conduite (desarmer d'abord) !!"); return; } Serial.println(">> Redemarrage en mode BOOTSEL..."); Serial.flush(); rp2040.rebootToBootloader(); return; } if (commande[0] == 'S' || commande[0] == 's') { if (estActuellementArme()) { Serial.println("!! Sauvegarde Flash refusee pendant la conduite (desarmer d'abord) !!"); return; } momentDernierChangementDurete_ms = 0; sauvegardeDifferee = false; sauvegarderConfiguration(); return; } if (commande[0] == 'R' || commande[0] == 'r') { if (estActuellementArme()) { Serial.println("!! Reinitialisation refusee pendant la conduite (desarmer d'abord) !!"); return; } rp2040.idleOtherCore(); reinitialiserConfigurationDefaut(); reinitialiserCanauxCRSF(); rapportEngage = 0; estArme = false; extArme = false; extRapport = 0; extAccelPct = 0; extFreinPct = 0; extEmbrayagePct = 0; extVolantPct = 0; extRcActif = false; __atomic_store_n(&sourceActive, SRC_G29, __ATOMIC_RELEASE); __atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE); __atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE); momentDernierChangementDurete_ms = 0; sauvegardeDifferee = false; rp2040.resumeOtherCore(); __atomic_store_n(&majRangeRequise, true, __ATOMIC_RELEASE); __atomic_store_n(&majRessortRequise, true, __ATOMIC_RELEASE); sauvegarderConfiguration(); afficherConfiguration(); return; } if (strlen(commande) >= 3 && commande[1] == '=') { char periph = commande[0]; if (periph == 'V' || periph == 'v' || periph == 'A' || periph == 'a' || periph == 'F' || periph == 'f' || periph == 'E' || periph == 'e') { if (estActuellementArme()) { Serial.println("!! Remappage des canaux refuse pendant la conduite (desarmer d'abord) !!"); return; } int nouveauCanal = 0; if (!parseStrictInt(&commande[2], nouveauCanal) || nouveauCanal < 1 || nouveauCanal > 16) { Serial.println("!! Canal invalide : doit etre un entier entre 1 et 16 !!"); return; } if (estCanalReserve((uint8_t)nouveauCanal)) { Serial.printf("!! Canal %d reserve (ARM=CH%d, HT=CH%d..CH%d) !!\n", nouveauCanal, CANAL_ARM, CANAL_HT_PAN, CANAL_HT_ROLL); return; } uint8_t cV = configSysteme.volant; uint8_t cA = configSysteme.accel; uint8_t cF = configSysteme.frein; uint8_t cE = configSysteme.embrayage; bool conflit = false; switch (periph) { case 'V': case 'v': if (nouveauCanal == cA || nouveauCanal == cF || nouveauCanal == cE) conflit = true; break; case 'A': case 'a': if (nouveauCanal == cV || nouveauCanal == cF || nouveauCanal == cE) conflit = true; break; case 'F': case 'f': if (nouveauCanal == cV || nouveauCanal == cA || nouveauCanal == cE) conflit = true; break; case 'E': case 'e': if (nouveauCanal == cV || nouveauCanal == cA || nouveauCanal == cF) conflit = true; break; } if (conflit) { Serial.printf("!! Conflit : le canal %d est deja utilise par un autre axe !!\n", nouveauCanal); return; } rp2040.idleOtherCore(); switch (periph) { case 'V': case 'v': configSysteme.volant = nouveauCanal; break; case 'A': case 'a': configSysteme.accel = nouveauCanal; break; case 'F': case 'f': configSysteme.frein = nouveauCanal; break; case 'E': case 'e': configSysteme.embrayage = nouveauCanal; break; } reinitialiserCanauxCRSF(); rp2040.resumeOtherCore(); sauvegarderConfiguration(); afficherConfiguration(); } else if (periph == 'D' || periph == 'd') { int val = 0; if (parseStrictInt(&commande[2], val) && val >= 0 && val <= 255) { configSysteme.forceRessort = (uint8_t)val; // Atomique sur ARM Cortex-M33 __atomic_store_n(&majRessortRequise, true, __ATOMIC_RELEASE); demanderOuExecuterSauvegarde(); afficherConfiguration(); } else { Serial.println("!! Durete (D) doit etre un entier entre 0 et 255 !!"); } } else if (periph == 'P' || periph == 'p') { if (estActuellementArme()) { Serial.println("!! Reglage de plage (P) refuse pendant la conduite (desarmer d'abord) !!"); return; } int val = 0; if (parseStrictInt(&commande[2], val) && val >= 40 && val <= 900) { rp2040.idleOtherCore(); configSysteme.angleSouhaiteTotal = (uint16_t)val; recalculerParametresVolant(); rp2040.resumeOtherCore(); __atomic_store_n(&majRangeRequise, true, __ATOMIC_RELEASE); sauvegarderConfiguration(); afficherConfiguration(); } else { Serial.println("!! Plage d'angle (P) doit etre un entier entre 40 et 900 degres !!"); } } else if (periph == 'X' || periph == 'x') { if (estActuellementArme()) { Serial.println("!! Reglage d'expo (X) refuse pendant la conduite (desarmer d'abord) !!"); return; } float val = 0.0f; if (parseStrictFloat(&commande[2], val) && val >= 0.0f && val <= 0.8f) { rp2040.idleOtherCore(); configSysteme.expoVolant = val; rp2040.resumeOtherCore(); sauvegarderConfiguration(); afficherConfiguration(); } else { Serial.println("!! Expo volant (X) doit etre un nombre entre 0.0 et 0.8 !!"); } } else if (periph == 'T' || periph == 't') { int val = 0; if (parseStrictInt(&commande[2], val) && val >= 0 && val <= 100) { configSysteme.intensiteVibration = (uint8_t)val; // Atomique sur ARM Cortex-M33 __atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE); demanderOuExecuterSauvegarde(); afficherConfiguration(); } else { Serial.println("!! Vibrations (T) doit etre un entier entre 0 et 100% !!"); } } else if (periph == 'W' || periph == 'w') { int val = 0; if (parseStrictInt(&commande[2], val)) { cockpitEchoUSB = (val != 0); Serial.printf(">> [COCKPIT] Echo trames $G29 sur USB : %s\n", cockpitEchoUSB ? "ON" : "OFF"); } } return; } // Validation CRC-8 DVB-S2 (*XX) obligatoire sur toutes les trames $... (anti-corruption UART) if (commande[0] == '$') { char* star = strrchr(commande, '*'); if (!star || strlen(star + 1) != 2) { return; } char* endPtr = nullptr; unsigned long crcRecu = strtoul(star + 1, &endPtr, 16); if (endPtr != star + 3) { return; } size_t bodyLen = (size_t)(star - commande); uint8_t crcCalc = calculerCrc8Crsf((const uint8_t*)commande, bodyLen); if ((uint8_t)crcRecu != crcCalc) { return; } *star = '\0'; } // Injection Head-Tracking depuis SteamFrame FPV : $HT,,, (172..1811) if (strncmp(commande, "$HT,", 4) == 0) { int pan = CRSF_VAL_MID, tilt = CRSF_VAL_MID, roll = CRSF_VAL_MID; if (sscanf(commande + 4, "%d,%d,%d", &pan, &tilt, &roll) == 3) { crsfChannelStore(CANAL_HT_PAN - 1, (uint16_t)constrain(pan, CRSF_VAL_MIN, CRSF_VAL_MAX)); crsfChannelStore(CANAL_HT_TILT - 1, (uint16_t)constrain(tilt, CRSF_VAL_MIN, CRSF_VAL_MAX)); crsfChannelStore(CANAL_HT_ROLL - 1, (uint16_t)constrain(roll, CRSF_VAL_MIN, CRSF_VAL_MAX)); dernierPaquetHT_ms = millis(); htActif = true; } return; } // Sélection de la source de pilotage : $SRC,G29 ou $SRC,EXT (Steam Controller) if (strncmp(commande, "$SRC,", 5) == 0) { const char* mode = commande + 5; uint8_t cible = sourceActive; if (strcmp(mode, "G29") == 0 || strcmp(mode, "g29") == 0) { cible = SRC_G29; } else if (strcmp(mode, "EXT") == 0 || strcmp(mode, "ext") == 0 || strcmp(mode, "STEAM") == 0) { cible = SRC_EXT; } else { Serial.printf("!! [SRC] Source inconnue : %s (attendu G29 ou EXT) !!\n", mode); return; } if (cible == sourceActive) { cockpitEtatModifie = true; return; } if (estActuellementArme()) { Serial.println("!! [SRC] Changement de controleur refuse : desarmer d'abord !!"); cockpitEtatModifie = true; return; } rp2040.idleOtherCore(); __atomic_store_n(&sourceActive, cible, __ATOMIC_RELEASE); extRcActif = false; __atomic_store_n(&extArme, false, __ATOMIC_RELEASE); __atomic_store_n(&extRapport, 0, __ATOMIC_RELEASE); extAccelPct = 0; extFreinPct = 0; extEmbrayagePct = 0; extVolantPct = 0; __atomic_store_n(&estArme, false, __ATOMIC_RELEASE); __atomic_store_n(&rapportEngage, 0, __ATOMIC_RELEASE); boutonArmDejaEnfonce = true; palettePlusDejaEnfoncee = true; paletteMoinsDejaEnfoncee = true; etatBoutonsCockpitPrec = 0xFFFF; reinitialiserCanauxCRSF(); rp2040.resumeOtherCore(); __atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE); __atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE); Serial.printf(">> [SRC] Source active basculee sur : %s\n", (sourceActive == SRC_EXT) ? "EXT (Steam Controller)" : "G29 (Volant)"); return; } // Synchronisation des plages de vitesses CRSF depuis SteamFrame FPV : // $GEARS,,,,,, if (strncmp(commande, "$GEARS,", 7) == 0) { int fMin = 0, g1 = 0, g2 = 0, g3 = 0, rMin = 0, rMax = 0; if (sscanf(commande + 7, "%d,%d,%d,%d,%d,%d", &fMin, &g1, &g2, &g3, &rMin, &rMax) == 6) { // Bornes strictes (max > min en AV, max < min en AR) pour éliminer toute division par zéro dans map(). uint16_t vFMin = (uint16_t)constrain(fMin, CRSF_VAL_MID, CRSF_VAL_MAX - 1); uint16_t vG1 = (uint16_t)constrain(g1, (int)vFMin + 1, CRSF_VAL_MAX); uint16_t vG2 = (uint16_t)constrain(g2, (int)vFMin + 1, CRSF_VAL_MAX); uint16_t vG3 = (uint16_t)constrain(g3, (int)vFMin + 1, CRSF_VAL_MAX); uint16_t vRMin = (uint16_t)constrain(rMin, CRSF_VAL_MIN + 1, CRSF_VAL_MID); uint16_t vRMax = (uint16_t)constrain(rMax, CRSF_VAL_MIN, (int)vRMin - 1); bool modifie = (configSysteme.gazAvantMin != vFMin) || (configSysteme.gazAvantMax[0] != vG1) || (configSysteme.gazAvantMax[1] != vG2) || (configSysteme.gazAvantMax[2] != vG3) || (configSysteme.gazArriereMin != vRMin) || (configSysteme.gazArriereMax != vRMax); if (modifie) { if (estActuellementArme()) { // Sécurité : jamais d'idleOtherCore / EEPROM.commit() en conduite armée (gel Flash 50-100 ms). // On applique les plafonds en RAM et on diffère l'écriture Flash au désarmement. configSysteme.gazAvantMin = vFMin; configSysteme.gazAvantMax[0] = vG1; configSysteme.gazAvantMax[1] = vG2; configSysteme.gazAvantMax[2] = vG3; configSysteme.gazArriereMin = vRMin; configSysteme.gazArriereMax = vRMax; sauvegardeDifferee = true; Serial.printf(">> [VITESSES] Maj RAM (Flash differee au desarmement) : AV_MIN=%u | 1=%u | 2=%u | 3=%u | AR_MIN=%u | AR_MAX=%u\n", vFMin, vG1, vG2, vG3, vRMin, vRMax); } else { rp2040.idleOtherCore(); configSysteme.gazAvantMin = vFMin; configSysteme.gazAvantMax[0] = vG1; configSysteme.gazAvantMax[1] = vG2; configSysteme.gazAvantMax[2] = vG3; configSysteme.gazArriereMin = vRMin; configSysteme.gazArriereMax = vRMax; rp2040.resumeOtherCore(); sauvegarderConfiguration(); Serial.printf(">> [VITESSES] AV_MIN=%u | 1=%u | 2=%u | 3=%u | AR_MIN=%u | AR_MAX=%u\n", vFMin, vG1, vG2, vG3, vRMin, vRMax); } } } return; } // Injection conduite externe (Steam Controller via SteamFrame FPV) : // $RC,,,,,, if (strncmp(commande, "$RC,", 4) == 0) { if (__atomic_load_n(&sourceActive, __ATOMIC_ACQUIRE) != SRC_EXT) { return; } int steer = CRSF_VAL_MID, accel = CRSF_VAL_MID, brake = CRSF_VAL_MID, clutch = CRSF_VAL_MID; int arm = 0, gear = 0; if (sscanf(commande + 4, "%d,%d,%d,%d,%d,%d", &steer, &accel, &brake, &clutch, &arm, &gear) == 6) { uint16_t vSteer = (uint16_t)constrain(steer, CRSF_VAL_MIN, CRSF_VAL_MAX); uint16_t vAccel = (uint16_t)constrain(accel, CRSF_VAL_MIN, CRSF_VAL_MAX); uint16_t vBrake = (uint16_t)constrain(brake, CRSF_VAL_MIN, CRSF_VAL_MAX); uint16_t vClutch = (uint16_t)constrain(clutch, CRSF_VAL_MIN, CRSF_VAL_MAX); int8_t vGear = (int8_t)constrain(gear, -1, 3); bool demandeArm = (arm != 0); // Sécurité : refuser un armement (0 -> 1) si rapport != N ou gaz non neutres if (demandeArm && !extArme) { if (vGear == 0 && vAccel == CRSF_VAL_MID) { __atomic_store_n(&extArme, true, __ATOMIC_RELEASE); momentArmement_ms = millis(); momentDesarmement_ms = 0; __atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE); __atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE); Serial.println(">> [ARM EXT] ARME (Steam Controller)"); } } else if (!demandeArm && extArme) { __atomic_store_n(&extArme, false, __ATOMIC_RELEASE); momentDesarmement_ms = millis(); momentArmement_ms = 0; __atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE); __atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE); Serial.println(">> [ARM EXT] DESARME (Steam Controller)"); } if (vGear != extRapport) { __atomic_store_n(&extRapport, vGear, __ATOMIC_RELEASE); __atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE); __atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE); } // Défense en profondeur : appliquer aussi les plafonds CRSF par rapport côté RP2350 if (!extArme || extRapport == 0) { vAccel = CRSF_VAL_MID; } else if (extRapport >= 1 && extRapport <= 3) { uint16_t maxV = configSysteme.gazAvantMax[extRapport - 1]; vAccel = (uint16_t)constrain((int)vAccel, CRSF_VAL_MID, (int)maxV); } else if (extRapport == -1) { vAccel = (uint16_t)constrain((int)vAccel, (int)configSysteme.gazArriereMax, CRSF_VAL_MID); } uint8_t accelPct = 0; if (extArme && extRapport >= 1 && extRapport <= 3 && vAccel >= configSysteme.gazAvantMin) { uint16_t maxV = configSysteme.gazAvantMax[extRapport - 1]; if (maxV > configSysteme.gazAvantMin) { accelPct = (uint8_t)constrain(map(vAccel, configSysteme.gazAvantMin, maxV, 0, 100), 0, 100); } } else if (extArme && extRapport == -1 && vAccel <= configSysteme.gazArriereMin) { if (configSysteme.gazArriereMin > configSysteme.gazArriereMax) { accelPct = (uint8_t)constrain(map(vAccel, configSysteme.gazArriereMin, configSysteme.gazArriereMax, 0, 100), 0, 100); } } uint8_t brakePct = (vBrake > CRSF_VAL_MID) ? (uint8_t)constrain(map(vBrake, CRSF_VAL_MID, CRSF_VAL_MAX, 0, 100), 0, 100) : 0; uint8_t clutchPct = (vClutch > CRSF_VAL_MID) ? (uint8_t)constrain(map(vClutch, CRSF_VAL_MID, CRSF_VAL_MAX, 0, 100), 0, 100) : 0; int8_t steerPct = (int8_t)constrain( (int)(((float)((int)CRSF_VAL_MID - (int)vSteer) / (float)(CRSF_VAL_MID - CRSF_VAL_MIN)) * 100.0f), -100, 100 ); extAccelPct = accelPct; extFreinPct = brakePct; extEmbrayagePct = clutchPct; extVolantPct = steerPct; assignerCanalCRSF(configSysteme.volant, vSteer); assignerCanalCRSF(configSysteme.accel, vAccel); assignerCanalCRSF(configSysteme.frein, vBrake); assignerCanalCRSF(configSysteme.embrayage, vClutch); crsfChannelStore(CANAL_ARM - 1, extArme ? CRSF_VAL_MAX : CRSF_VAL_MIN); dernierPaquetRC_ms = millis(); if (!extRcActif) { extRcActif = true; __atomic_store_n(&majLedRequise, true, __ATOMIC_RELEASE); __atomic_store_n(&cockpitEtatModifie, true, __ATOMIC_RELEASE); } } return; } } void lireFluxSerie(Stream& port, char* buf, uint8_t& idx, bool& overflow, size_t maxLen, bool depuisUsb) { int budget = 96; // Borner le temps passé dans loop() pour préserver la cadence CRSF 166 Hz while (port.available() > 0 && budget-- > 0) { char c = (char)port.read(); if (c == '\n' || c == '\r') { if (!overflow && idx > 0) { buf[idx] = '\0'; traiterLigneSerie(buf, depuisUsb); } idx = 0; overflow = false; } else if (overflow) { // Ignorer le reste de la ligne trop longue jusqu'au prochain retour à la ligne } else if (idx < maxLen - 1) { buf[idx++] = c; } else { idx = 0; overflow = true; } } } void verifierCommandesSerie() { lireFluxSerie(Serial, serialBuffer, serialBufferIdx, serialBufferOverflow, sizeof(serialBuffer), true); lireFluxSerie(Serial2, serial2Buffer, serial2BufferIdx, serial2BufferOverflow, sizeof(serial2Buffer), false); }