Files
SteamFrame-FPV-Control-Center/g29_crsf.ino
T

641 lines
23 KiB
Arduino

#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <pio_usb.h>
#include <Adafruit_NeoPixel.h>
#include <EEPROM.h>
#include <hardware/watchdog.h>
// 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
// Sécurité : failsafe, watchdog et identification du volant
#define TIMEOUT_FAILSAFE_MS 100 // Sans rapport USB valide depuis X ms -> canaux au neutre
#define WATCHDOG_TIMEOUT_MS 500 // Reboot matériel si le cœur 0 se fige
#define G29_VID 0x046D
#define G29_PID_PS4 0xC260 // G29 en mode PS4 (le mode PS3 = 0xC24F a un autre format de rapport)
#define G29_REPORT_ID 0x01
// 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 BOUTON_ARM_MASK 0x08 // R2 (Bascule Armé / Désarmé)
// Armement ELRS : AUX1 = canal 5 (réservé, ne peut pas être mappé ailleurs)
#define CANAL_ARM 5
// 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
// Plages de gaz progressives (Marche avant : rapports 1, 2, 3)
#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)
};
#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
#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 à 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));
Adafruit_NeoPixel rgb_led(NUM_PIXELS, PIN_LED_RGB, NEO_GRB + NEO_KHZ800);
Adafruit_USBH_Host USBHost;
volatile uint16_t crsf_channels[16] = {
CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID,
CRSF_VAL_MIN, CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID, // Canal 5 (ARM) : désarmé
CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID,
CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID
};
struct MapCanaux {
uint8_t volant;
uint8_t accel;
uint8_t frein;
uint8_t embrayage;
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 bool paletteMoinsDejaEnfoncee = false;
volatile bool majLedRequise = true;
// Armement (bascule par R2) : écrit uniquement par le cœur 1
volatile bool estArme = false;
volatile bool boutonArmDejaEnfonce = false;
volatile int8_t logArmRequis = 0; // 1 = armé, -1 = désarmé, 2 = armement refusé
// État USB Host et diagnostic inter-cœurs
volatile uint8_t g29_dev_addr = 0;
volatile uint8_t g29_instance = 0;
volatile uint16_t g29_desc_len = 0;
volatile uint16_t g29_vid = 0;
volatile uint16_t g29_pid = 0;
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; // <-- Déclaration ajoutée ici
// Failsafe et diagnostic
volatile bool failsafeActif = true; // Écrit uniquement par le cœur 0
volatile bool demandeRetourNeutre = false; // Cœur 0 -> Cœur 1 : forcer le point mort
volatile uint32_t compteurRapportsOK = 0; // Écrit uniquement par le cœur 1
volatile uint32_t compteurRapportsRejetes = 0;
volatile bool logRejetRequis = false;
volatile uint16_t rejet_vid = 0;
volatile uint16_t rejet_pid = 0;
uint8_t dernierMasqueLedG29 = 0xFF;
uint8_t etapeEnvoiLed = 0;
uint8_t etapeInitMoteur = 0;
char serialBuffer[32];
uint8_t serialBufferIdx = 0;
void reinitialiserCanauxCRSF() {
for (int i = 0; i < 16; i++) {
crsf_channels[i] = CRSF_VAL_MID;
}
crsf_channels[CANAL_ARM - 1] = CRSF_VAL_MIN; // Toujours désarmé par défaut
}
void sauvegarderConfiguration() {
rp2040.idleOtherCore();
EEPROM.put(0, configCanaux);
EEPROM.commit();
rp2040.resumeOtherCore();
Serial.println(">> Configuration sauvegardée en mémoire Flash !");
}
void afficherConfiguration() {
Serial.println("--- Mapping Canaux (Waveshare RP2350-USB-A) ---");
Serial.printf("Volant (V) : Canal %d\n", configCanaux.volant);
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.printf("ARM (R2) : Canal %d (reserve)\n", CANAL_ARM);
}
void chargerConfiguration() {
EEPROM.get(0, configCanaux);
bool conflitArm = configCanaux.volant == CANAL_ARM || configCanaux.accel == CANAL_ARM ||
configCanaux.frein == CANAL_ARM || configCanaux.embrayage == CANAL_ARM;
if (configCanaux.magicNumber != 42 || conflitArm) {
if (conflitArm) Serial.println("!! Un axe etait mappe sur le canal ARM : configuration reinitialisee !!");
configCanaux.volant = 1;
configCanaux.accel = 2;
configCanaux.frein = 3;
configCanaux.embrayage = 4;
configCanaux.magicNumber = 42;
sauvegarderConfiguration();
}
}
// -------------------------------------------------------------------------
// GESTION SÉRIE NON BLOQUANTE
// -------------------------------------------------------------------------
void traiterLigneSerie(char* commande) {
if (commande[0] == '?') {
afficherConfiguration();
return;
}
if (strlen(commande) >= 3 && commande[1] == '=') {
char periph = commande[0];
int nouveauCanal = atoi(&commande[2]);
if (nouveauCanal == CANAL_ARM) {
Serial.printf("!! Canal %d reserve a l'armement (R2) !!\n", CANAL_ARM);
return;
}
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;
default: modifOk = false; break;
}
if (modifOk) {
reinitialiserCanauxCRSF();
sauvegarderConfiguration();
afficherConfiguration();
}
}
}
}
void verifierCommandesSerie() {
while (Serial.available() > 0) {
char c = (char)Serial.read();
if (c == '\n' || c == '\r') {
if (serialBufferIdx > 0) {
serialBuffer[serialBufferIdx] = '\0';
traiterLigneSerie(serialBuffer);
serialBufferIdx = 0;
}
} else if (serialBufferIdx < sizeof(serialBuffer) - 1) {
serialBuffer[serialBufferIdx++] = c;
}
}
}
// -------------------------------------------------------------------------
// GESTION LED RGB SUR GP16
// -------------------------------------------------------------------------
void actualiserLED() {
uint8_t r = 0, g = 0, b = 0;
if (!volant_est_pret) {
b = 255; // Bleu : Attente G29
} else if (failsafeActif) {
r = 255; g = 255; b = 255; // Blanc : Failsafe (G29 muet)
} else {
switch (rapportEngage) {
case -1: r = 255; g = 0; b = 0; break; // Rouge : R
case 0: r = 255; g = 100; b = 0; break; // Orange : N
case 1: r = 0; g = 255; b = 255; break; // Cyan : 1ère
case 2: r = 0; g = 255; b = 0; break; // Vert : 2ème
case 3: r = 180; g = 0; b = 255; break; // Violet : 3ème
}
if (!estArme) { // Désarmé : couleur du rapport très atténuée
r /= 10; g /= 10; b /= 10;
}
}
rgb_led.setPixelColor(0, rgb_led.Color(r, g, b));
rgb_led.show();
}
// -------------------------------------------------------------------------
// ENVOI CRSF DEPUIS LA SRAM DU RP2350
// -------------------------------------------------------------------------
void __not_in_flash_func(envoyer_paquet_crsf)(bool neutre) {
uint8_t packet[26];
uint16_t ch[16];
for (int i = 0; i < 16; i++) {
ch[i] = neutre ? CRSF_VAL_MID : crsf_channels[i];
}
if (neutre) ch[CANAL_ARM - 1] = CRSF_VAL_MIN; // Failsafe : toujours désarmé
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[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[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[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[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[24] = (uint8_t)(ch[15] >> 3);
uint8_t crc = 0;
for (int i = 2; i < 25; i++) {
crc ^= packet[i];
for (int j = 0; j < 8; j++) {
if (crc & 0x80) crc = (crc << 1) ^ 0xD5;
else crc <<= 1;
}
}
packet[25] = crc;
Serial1.write(packet, 26);
}
// =========================================================================
// CŒUR 0 : Initialisation, CRSF 166Hz, Série, EEPROM et LED RGB
// =========================================================================
void setup() {
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);
rgb_led.begin();
rgb_led.setBrightness(50);
actualiserLED();
delay(500);
chargerConfiguration();
Serial1.setTX(0);
Serial1.setRX(1);
Serial1.begin(420000);
Serial.println("\n--- WAVESHARE RP2350-USB-A PRETE ---");
afficherConfiguration();
// Watchdog matériel : reboot si loop() ne le nourrit plus pendant WATCHDOG_TIMEOUT_MS
if (watchdog_caused_reboot()) {
Serial.println("!! [WATCHDOG] Redemarrage suite a un blocage !!");
}
rp2040.wdt_begin(WATCHDOG_TIMEOUT_MS);
}
// Évalue le failsafe (cœur 0). Retourne true si les canaux doivent être forcés au neutre.
bool evaluerFailsafe() {
// Lire l'horodatage AVANT millis() : si le cœur 1 le met à jour entre les deux,
// la soustraction reste positive (pas de faux déclenchement par débordement).
uint32_t dernier = dernierRapportUSB_ms;
uint32_t age = millis() - dernier;
bool stale = !volant_est_pret || (age > TIMEOUT_FAILSAFE_MS);
if (stale && !failsafeActif) {
failsafeActif = true;
demandeRetourNeutre = true; // Au retour du signal, la voiture repart au point mort
majLedRequise = true;
Serial.printf("!! [FAILSAFE] ACTIF (dernier rapport USB il y a %lu ms) !!\n", (unsigned long)age);
} else if (!stale && failsafeActif) {
failsafeActif = false;
majLedRequise = true;
Serial.println("[FAILSAFE] Signal G29 retabli");
}
return stale;
}
void loop() {
rp2040.wdt_reset();
verifierCommandesSerie();
if (majLedRequise) {
majLedRequise = false;
actualiserLED();
Serial.printf("[BOITE] Rapport engage : %d\n", rapportEngage);
}
if (logMountRequis) {
logMountRequis = false;
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);
}
if (logRejetRequis) {
logRejetRequis = false;
Serial.printf("[USB HOST] Peripherique ignore (VID=%04X, PID=%04X) : pas un G29 en mode PS4\n",
rejet_vid, rejet_pid);
}
int8_t evtArm = logArmRequis;
if (evtArm != 0) {
logArmRequis = 0;
if (evtArm == 1) Serial.println(">> [ARM] ARME (R2)");
else if (evtArm == -1) Serial.println(">> [ARM] DESARME");
else Serial.println("!! [ARM] Refuse : passer au point mort (N) et relacher l'accelerateur !!");
}
static uint32_t dernierHeartbeat = 0;
static uint32_t dernierCompteOK = 0;
if (millis() - dernierHeartbeat >= 2000) {
uint32_t dt = millis() - dernierHeartbeat;
dernierHeartbeat = millis();
uint32_t compteOK = compteurRapportsOK;
uint32_t rapportsParSec = ((compteOK - dernierCompteOK) * 1000UL) / (dt ? dt : 1);
dernierCompteOK = compteOK;
Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/6 | Rapport=%d | Arme=%d | Statut_LED=%d | USB=%lu rap/s | Rejetes=%lu | Failsafe=%d\n",
volant_est_pret, g29_pid, etapeInitMoteur, rapportEngage, estArme, statutEnvoiLed,
(unsigned long)rapportsParSec, (unsigned long)compteurRapportsRejetes, failsafeActif);
}
static uint32_t dernier_envoi = 0;
if (millis() - dernier_envoi >= 6) {
dernier_envoi = millis();
envoyer_paquet_crsf(evaluerFailsafe());
}
}
// =========================================================================
// CŒUR 1 : PILE PIO-USB, AUTO-CENTER ET LEDS G29
// =========================================================================
bool envoyerTrameG29_0x30(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, 0x30, payload63, sizeof(payload63));
}
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, (uint8_t)(PENTE_RESSORT_G29 & 0x07), (uint8_t)(PENTE_RESSORT_G29 & 0x07), (uint8_t)FORCE_RESSORT_G29, 0x00, 0x00};
const uint8_t cmdEnable[7] = {0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
switch (etapeInitMoteur) {
case 0: if (envoyerTrameG29_0x30(cmdRange)) etapeInitMoteur++; break;
case 1: if (envoyerTrameG29_0x30(cmdSpring)) etapeInitMoteur++; break;
case 2: if (envoyerTrameG29_0x30(cmdEnable)) etapeInitMoteur++; break;
case 3: etapeInitMoteur = 6; break;
}
}
void envoyerLedsG29(uint8_t masque5bits) {
if (!volant_est_pret) return;
if (millis() - momentConnexionUSB_ms < 500) return;
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;
// Format validé sur G29 PS4 physique :
// Report ID = 0x30, payload 63 octets débutant par F8 12 <masque> suivi de zéros
static uint8_t payload63[63];
memset(payload63, 0, sizeof(payload63));
payload63[0] = 0xF8;
payload63[1] = 0x12;
payload63[2] = m;
if (tuh_hid_send_report(g29_dev_addr, g29_instance, 0x30, payload63, sizeof(payload63))) {
statutEnvoiLed = 1;
etapeEnvoiLed = 0;
}
}
void setup1() {
sleep_ms(100);
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
pio_cfg.pin_dp = HOST_PIN_DP;
USBHost.configure_pio_usb(0, &pio_cfg);
USBHost.begin(0);
}
void loop1() {
USBHost.task();
static uint32_t dernierTickG29 = 0;
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;
case 0: masqueCible = 0x00; break;
case 1: masqueCible = 0x01; break;
case 2: masqueCible = 0x03; break;
case 3: masqueCible = 0x07; break;
}
envoyerLedsG29(masqueCible);
}
}
// =========================================================================
// CALLBACKS HID USB HOST (Exécutés sur Core 1)
// =========================================================================
extern "C" {
void assignerCanalCRSF(uint8_t numeroCh, uint16_t valeur11bits) {
if (numeroCh >= 1 && numeroCh <= 16 && numeroCh != CANAL_ARM) {
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;
// Filtrage : n'accepter que le G29 en mode PS4, et une seule interface à la fois
uint16_t vid = 0, pid = 0;
tuh_vid_pid_get(dev_addr, &vid, &pid);
if (vid != G29_VID || pid != G29_PID_PS4 || volant_est_pret) {
rejet_vid = vid;
rejet_pid = pid;
logRejetRequis = true;
return; // Pas de tuh_hid_receive_report() : on ignore ce périphérique
}
g29_vid = vid;
g29_pid = pid;
g29_dev_addr = dev_addr;
g29_instance = instance;
g29_desc_len = desc_len;
dernierMasqueLedG29 = 0xFF;
etapeEnvoiLed = 0;
etapeInitMoteur = 0;
rapportEngage = 0;
demandeRetourNeutre = false;
estArme = false;
boutonArmDejaEnfonce = true; // Ignorer R2 s'il est déjà enfoncé au branchement
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) {
// Ignorer la déconnexion d'un autre périphérique que le G29
if (!volant_est_pret || dev_addr != g29_dev_addr || instance != g29_instance) return;
volant_est_pret = false;
g29_vid = 0;
g29_pid = 0;
etapeInitMoteur = 0;
rapportEngage = 0;
estArme = false;
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) {
// Ignorer tout rapport ne venant pas du G29, ou n'ayant pas le bon format
if (dev_addr != g29_dev_addr || instance != g29_instance) {
return; // Ne pas réarmer la réception d'un périphérique étranger
}
if (len < 51 || report[0] != G29_REPORT_ID) {
compteurRapportsRejetes++;
tuh_hid_receive_report(dev_addr, instance);
return;
}
{
compteurRapportsOK++;
// Retour du signal après un failsafe : repartir au point mort ET désarmé
if (demandeRetourNeutre) {
demandeRetourNeutre = false;
if (rapportEngage != 0) {
rapportEngage = 0;
majLedRequise = true;
}
if (estArme) {
estArme = false;
logArmRequis = -1;
majLedRequise = true;
}
boutonArmDejaEnfonce = true; // Exiger un relâchement de R2 avant de réarmer
}
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]);
uint8_t boutonsEpaule = report[6];
bool pPlusActuel = (boutonsEpaule & PALETTE_PLUS_MASK);
bool pMoinsActuel = (boutonsEpaule & PALETTE_MOINS_MASK);
bool armActuel = (boutonsEpaule & BOUTON_ARM_MASK);
// Bascule ARM / DÉSARMÉ sur front montant de R2.
// Armement autorisé uniquement au point mort et accélérateur relâché.
if (armActuel && !boutonArmDejaEnfonce) {
if (estArme) {
estArme = false;
logArmRequis = -1;
} else if (rapportEngage == 0 && accelRaw <= DEADZONE_PEDALE) {
estArme = true;
logArmRequis = 1;
} else {
logArmRequis = 2;
}
majLedRequise = true;
boutonArmDejaEnfonce = true;
} else if (!armActuel && boutonArmDejaEnfonce) {
boutonArmDejaEnfonce = 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;
}
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);
uint16_t vAccel = CRSF_VAL_MID;
if (estArme && accelRaw > DEADZONE_PEDALE) { // Désarmé : gaz coupés quoi qu'il arrive
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 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;
assignerCanalCRSF(configCanaux.volant, vVolant);
assignerCanalCRSF(configCanaux.accel, vAccel);
assignerCanalCRSF(configCanaux.frein, vFrein);
assignerCanalCRSF(configCanaux.embrayage, vEmbrayage);
crsf_channels[CANAL_ARM - 1] = estArme ? CRSF_VAL_MAX : CRSF_VAL_MIN;
// Horodatage APRÈS l'écriture des canaux : à la sortie d'un failsafe, le cœur 0
// ne peut pas renvoyer d'anciennes valeurs (d'avant la coupure) pendant un paquet.
dernierRapportUSB_ms = millis();
}
tuh_hid_receive_report(dev_addr, instance);
}
} // extern "C"