From 426ab7c7d6a285eb36be759ad80d9ad280015c34 Mon Sep 17 00:00:00 2001 From: bleack Date: Sun, 22 Nov 2026 13:12:04 +0100 Subject: [PATCH] feat(gui,firmware): configurable CRSF gear speed limits and responsive GPUI layout --- firmware/g29_crsf/g29_crsf.ino | 83 +++++++++-- src/config.rs | 24 ++++ src/controller.rs | 126 +++++++++++++++-- src/engine.rs | 36 ++++- src/gui.rs | 100 +++++++++++++- src/gui_gpui.rs | 243 ++++++++++++++++++++++++++++++++- 6 files changed, 578 insertions(+), 34 deletions(-) diff --git a/firmware/g29_crsf/g29_crsf.ino b/firmware/g29_crsf/g29_crsf.ino index f74101a..f801114 100644 --- a/firmware/g29_crsf/g29_crsf.ino +++ b/firmware/g29_crsf/g29_crsf.ino @@ -116,7 +116,7 @@ const uint16_t GAZ_AVANT_MAX_PAR_VITESSE[3] = { #define PENTE_RESSORT_G29 7 // Raideur autour du centre : 1 a 7 #define ETAPES_INIT_FFB_TOTAL 2 // Nombre d'etapes d'init FFB initiales (0=Range, 1=Spring) #define VIBRATION_DEFAUT 50 // Intensite par defaut des vibrations (0 a 100%) -#define MAGIC_NUMBER_CONFIG 0x4A // Identifiant de validite en EEPROM (mis a jour pour 50% vibrations) +#define MAGIC_NUMBER_CONFIG 0x4B // Identifiant de validite en EEPROM (mis a jour pour vitesses CRSF) Adafruit_NeoPixel rgb_led(NUM_PIXELS, PIN_LED_RGB, NEO_GRB + NEO_KHZ800); Adafruit_USBH_Host USBHost; @@ -137,6 +137,10 @@ struct __attribute__((packed)) ConfigG29 { uint16_t angleSouhaiteTotal; // Plage totale en degrés : 40 à 900 float expoVolant; // Expo volant : 0.0f à 0.8f uint8_t intensiteVibration; // Intensité globale des vibrations : 0 à 100% + uint16_t gazAvantMin; // Seuil de démarrage marche avant (défaut 1065) + uint16_t gazAvantMax[3]; // Plafonds 1ère, 2ème, 3ème vitesse + uint16_t gazArriereMin; // Seuil de démarrage marche arrière (défaut 905) + uint16_t gazArriereMax; // Plafond marche arrière (défaut 800) uint8_t magicNumber; // Identifiant de validité en EEPROM (MAGIC_NUMBER_CONFIG) }; @@ -277,6 +281,12 @@ void reinitialiserConfigurationDefaut() { configSysteme.angleSouhaiteTotal = ANGLE_DEFAUT_TOTAL; configSysteme.expoVolant = EXPO_DEFAUT; configSysteme.intensiteVibration = VIBRATION_DEFAUT; + configSysteme.gazAvantMin = GAZ_AVANT_MIN; + configSysteme.gazAvantMax[0] = GAZ_AVANT_MAX_PAR_VITESSE[0]; + configSysteme.gazAvantMax[1] = GAZ_AVANT_MAX_PAR_VITESSE[1]; + configSysteme.gazAvantMax[2] = GAZ_AVANT_MAX_PAR_VITESSE[2]; + configSysteme.gazArriereMin = GAZ_ARRIERE_MIN; + configSysteme.gazArriereMax = GAZ_ARRIERE_MAX; configSysteme.magicNumber = MAGIC_NUMBER_CONFIG; recalculerParametresVolant(); } @@ -313,7 +323,14 @@ void afficherConfiguration() { Serial.printf("Source active : %s [$SRC,G29 | $SRC,EXT]\n", (sourceActive == SRC_EXT) ? "EXT (Steam Controller)" : "G29 (Volant)"); Serial.printf("Vibrations (T) : %d%% [Boutons +/- ou T=0..100] (0 = desactive)\n", configSysteme.intensiteVibration); - Serial.println("Commandes : V=ch, A=ch, F=ch, E=ch, D=val, P=val, X=val, T=val, $SRC,G29|EXT, $RC,..., $HT,..., S(save), R(reset), B(bootsel), ?"); + Serial.printf("Vitesses CRSF : AV_MIN=%u | 1=%u | 2=%u | 3=%u | AR_MIN=%u | AR_MAX=%u\n", + configSysteme.gazAvantMin, + configSysteme.gazAvantMax[0], + configSysteme.gazAvantMax[1], + configSysteme.gazAvantMax[2], + configSysteme.gazArriereMin, + configSysteme.gazArriereMax); + Serial.println("Commandes : V=ch, A=ch, F=ch, E=ch, D=val, P=val, X=val, T=val, $SRC,G29|EXT, $GEARS,..., $RC,..., $HT,..., S(save), R(reset), B(bootsel), ?"); } void chargerConfiguration() { @@ -345,10 +362,17 @@ void chargerConfiguration() { bool vibValide = (configSysteme.intensiteVibration <= 100); - if (configSysteme.magicNumber != MAGIC_NUMBER_CONFIG || conflitArm || !canauxValides || !canauxDistincts || !expoValide || !angleValide || !vibValide) { + bool vitessesValides = (configSysteme.gazAvantMin >= CRSF_VAL_MID && configSysteme.gazAvantMin <= CRSF_VAL_MAX) && + (configSysteme.gazAvantMax[0] >= configSysteme.gazAvantMin && configSysteme.gazAvantMax[0] <= CRSF_VAL_MAX) && + (configSysteme.gazAvantMax[1] >= configSysteme.gazAvantMin && configSysteme.gazAvantMax[1] <= CRSF_VAL_MAX) && + (configSysteme.gazAvantMax[2] >= configSysteme.gazAvantMin && configSysteme.gazAvantMax[2] <= CRSF_VAL_MAX) && + (configSysteme.gazArriereMin >= CRSF_VAL_MIN && configSysteme.gazArriereMin <= CRSF_VAL_MID) && + (configSysteme.gazArriereMax >= CRSF_VAL_MIN && configSysteme.gazArriereMax <= configSysteme.gazArriereMin); + + if (configSysteme.magicNumber != MAGIC_NUMBER_CONFIG || conflitArm || !canauxValides || !canauxDistincts || !expoValide || !angleValide || !vibValide || !vitessesValides) { if (conflitArm) Serial.println("!! Un axe etait mappe sur le canal ARM : configuration reinitialisee !!"); else if (!canauxValides || !canauxDistincts) Serial.println("!! Conflit ou canal invalide en EEPROM : configuration reinitialisee !!"); - else if (!expoValide || !angleValide || !vibValide) Serial.println("!! Valeur invalide en EEPROM : configuration reinitialisee !!"); + else if (!expoValide || !angleValide || !vibValide || !vitessesValides) Serial.println("!! Valeur invalide en EEPROM : configuration reinitialisee !!"); else Serial.println(">> Initialisation configuration d'usine par defaut..."); reinitialiserConfigurationDefaut(); sauvegarderConfiguration(); @@ -585,6 +609,41 @@ void traiterLigneSerie(char* commande) { 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) { + uint16_t vFMin = (uint16_t)constrain(fMin, CRSF_VAL_MID, CRSF_VAL_MAX); + uint16_t vG1 = (uint16_t)constrain(g1, (int)vFMin, CRSF_VAL_MAX); + uint16_t vG2 = (uint16_t)constrain(g2, (int)vFMin, CRSF_VAL_MAX); + uint16_t vG3 = (uint16_t)constrain(g3, (int)vFMin, CRSF_VAL_MAX); + uint16_t vRMin = (uint16_t)constrain(rMin, CRSF_VAL_MIN, CRSF_VAL_MID); + uint16_t vRMax = (uint16_t)constrain(rMax, CRSF_VAL_MIN, (int)vRMin); + + bool modifie = (configSysteme.gazAvantMin != vFMin) || + (configSysteme.gazAvantMax[0] != vG1) || + (configSysteme.gazAvantMax[1] != vG2) || + (configSysteme.gazAvantMax[2] != vG3) || + (configSysteme.gazArriereMin != vRMin) || + (configSysteme.gazArriereMax != vRMax); + + configSysteme.gazAvantMin = vFMin; + configSysteme.gazAvantMax[0] = vG1; + configSysteme.gazAvantMax[1] = vG2; + configSysteme.gazAvantMax[2] = vG3; + configSysteme.gazArriereMin = vRMin; + configSysteme.gazArriereMax = vRMax; + + if (modifie) { + 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) { @@ -631,11 +690,11 @@ void traiterLigneSerie(char* commande) { } uint8_t accelPct = 0; - if (extArme && extRapport >= 1 && extRapport <= 3 && vAccel >= GAZ_AVANT_MIN) { - uint16_t maxV = GAZ_AVANT_MAX_PAR_VITESSE[extRapport - 1]; - accelPct = (uint8_t)constrain(map(vAccel, GAZ_AVANT_MIN, maxV, 0, 100), 0, 100); - } else if (extArme && extRapport == -1 && vAccel <= GAZ_ARRIERE_MIN) { - accelPct = (uint8_t)constrain(map(vAccel, GAZ_ARRIERE_MIN, GAZ_ARRIERE_MAX, 0, 100), 0, 100); + if (extArme && extRapport >= 1 && extRapport <= 3 && vAccel >= configSysteme.gazAvantMin) { + uint16_t maxV = configSysteme.gazAvantMax[extRapport - 1]; + accelPct = (uint8_t)constrain(map(vAccel, configSysteme.gazAvantMin, maxV, 0, 100), 0, 100); + } else if (extArme && extRapport == -1 && vAccel <= configSysteme.gazArriereMin) { + 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) @@ -1875,10 +1934,10 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i 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); + vAccel = constrain(map(accelRaw, DEADZONE_PEDALE, 65535, configSysteme.gazArriereMin, configSysteme.gazArriereMax), 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 maxVitesse = configSysteme.gazAvantMax[rapportEngage - 1]; + vAccel = constrain(map(accelRaw, DEADZONE_PEDALE, 65535, configSysteme.gazAvantMin, maxVitesse), CRSF_VAL_MIN, CRSF_VAL_MAX); } } diff --git a/src/config.rs b/src/config.rs index 424c5b1..88da6f9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -72,6 +72,30 @@ pub struct AppConfig { #[arg(long, default_value_t = 100)] pub controller_vibration: u8, + /// Seuil CRSF de démarrage des gaz en marche avant (1ère, 2ème, 3ème) + #[arg(long, default_value_t = 1065)] + pub gaz_avant_min: u16, + + /// Plafond CRSF de la 1ère vitesse (Manœuvre) + #[arg(long, default_value_t = 1100)] + pub gear_1_max: u16, + + /// Plafond CRSF de la 2ème vitesse (Normale) + #[arg(long, default_value_t = 1150)] + pub gear_2_max: u16, + + /// Plafond CRSF de la 3ème vitesse (Rapide) + #[arg(long, default_value_t = 1220)] + pub gear_3_max: u16, + + /// Seuil CRSF de démarrage des gaz en marche arrière (R) + #[arg(long, default_value_t = 905)] + pub gaz_arriere_min: u16, + + /// Plafond CRSF de la marche arrière (R) + #[arg(long, default_value_t = 800)] + pub gear_reverse_max: u16, + /// Codec vidéo reçu depuis le VRX OpenIPC / Ruby ("h264" ou "h265") #[arg(long, default_value = "h264")] pub codec: String, diff --git a/src/controller.rs b/src/controller.rs index 54ab49e..50713cc 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -58,6 +58,46 @@ const GAZ_AVANT_MAX_PAR_VITESSE: [u16; 3] = [ const GAZ_ARRIERE_MIN: u16 = 905; const GAZ_ARRIERE_MAX: u16 = 800; +/// Bornes CRSF configurables de la boîte séquentielle (marche avant 1/2/3 et marche arrière R). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct GearSpeedLimits { + pub forward_min: u16, + pub forward_max: [u16; 3], + pub reverse_min: u16, + pub reverse_max: u16, +} + +impl Default for GearSpeedLimits { + fn default() -> Self { + Self { + forward_min: GAZ_AVANT_MIN, + forward_max: GAZ_AVANT_MAX_PAR_VITESSE, + reverse_min: GAZ_ARRIERE_MIN, + reverse_max: GAZ_ARRIERE_MAX, + } + } +} + +impl GearSpeedLimits { + /// Contraint chaque valeur dans les bornes CRSF valides et cohérentes. + pub fn sanitized(self) -> Self { + let forward_min = self.forward_min.clamp(CRSF_CHANNEL_MID, CRSF_CHANNEL_MAX); + let forward_max = [ + self.forward_max[0].clamp(forward_min, CRSF_CHANNEL_MAX), + self.forward_max[1].clamp(forward_min, CRSF_CHANNEL_MAX), + self.forward_max[2].clamp(forward_min, CRSF_CHANNEL_MAX), + ]; + let reverse_min = self.reverse_min.clamp(CRSF_CHANNEL_MIN, CRSF_CHANNEL_MID); + let reverse_max = self.reverse_max.clamp(CRSF_CHANNEL_MIN, reverse_min); + Self { + forward_min, + forward_max, + reverse_min, + reverse_max, + } + } +} + const TOAST_DURATION_MS: u64 = 1500; const RC_SEND_INTERVAL_MS: u64 = 10; // 100 Hz vers RP2350 const HAPTICS_INTERVAL_MS: u64 = 40; // 25 Hz (identique au G29) @@ -353,7 +393,13 @@ pub fn steering_to_crsf(raw_norm: f32, deadzone: f32, expo: f32, invert: bool) - } /// Normalise une gâchette [0.0..1.0] avec zone morte et calcule le canal gaz CRSF selon la boîte séquentielle. -pub fn throttle_to_crsf(throttle_norm: f32, deadzone: f32, armed: bool, gear: i8) -> (u8, u16) { +pub fn throttle_to_crsf( + throttle_norm: f32, + deadzone: f32, + armed: bool, + gear: i8, + limits: GearSpeedLimits, +) -> (u8, u16) { let dz = deadzone.clamp(0.0, 0.45); let clamped = throttle_norm.clamp(0.0, 1.0); let active = if clamped <= dz { @@ -367,13 +413,14 @@ pub fn throttle_to_crsf(throttle_norm: f32, deadzone: f32, armed: bool, gear: i8 return (pct, CRSF_CHANNEL_MID); } + let s = limits.sanitized(); let crsf = if gear == -1 { - let span = GAZ_ARRIERE_MIN as f32 - GAZ_ARRIERE_MAX as f32; - (GAZ_ARRIERE_MIN as f32 - active * span).round() as u16 + let span = s.reverse_min as f32 - s.reverse_max as f32; + (s.reverse_min as f32 - active * span).round() as u16 } else if (1..=3).contains(&gear) { - let max_v = GAZ_AVANT_MAX_PAR_VITESSE[(gear - 1) as usize]; - let span = max_v as f32 - GAZ_AVANT_MIN as f32; - (GAZ_AVANT_MIN as f32 + active * span).round() as u16 + let max_v = s.forward_max[(gear - 1) as usize]; + let span = max_v as f32 - s.forward_min as f32; + (s.forward_min as f32 + active * span).round() as u16 } else { CRSF_CHANNEL_MID }; @@ -404,6 +451,19 @@ pub fn encode_src_command(source: ControlSource) -> String { format!("$SRC,{}\n", source.wire_tag()) } +pub fn encode_gears_command(limits: GearSpeedLimits) -> String { + let s = limits.sanitized(); + format!( + "$GEARS,{},{},{},{},{},{}\n", + s.forward_min, + s.forward_max[0], + s.forward_max[1], + s.forward_max[2], + s.reverse_min, + s.reverse_max, + ) +} + pub fn encode_rc_frame( steer: u16, accel: u16, @@ -1346,6 +1406,7 @@ impl SteamControllerWorker { active_source: Arc, vibration_pct: Arc, rumble_test_flag: Arc, + gear_speed_limits: Arc>, control: EngineControlHandle, g29_state_opt: Option>>, telemetry_state_opt: Option>>, @@ -1384,6 +1445,7 @@ impl SteamControllerWorker { active_source, vibration_pct, rumble_test_flag, + gear_speed_limits, control, g29_state_opt, telemetry_state_opt, @@ -1411,6 +1473,7 @@ impl SteamControllerWorker { active_source: Arc, vibration_pct: Arc, rumble_test_flag: Arc, + gear_speed_limits: Arc>, control: EngineControlHandle, g29_state_opt: Option>>, telemetry_state_opt: Option>>, @@ -1426,13 +1489,27 @@ impl SteamControllerWorker { let mut last_haptics_tick = Instant::now() - Duration::from_millis(HAPTICS_INTERVAL_MS); let mut rumble_is_zero = true; - // Synchronisation initiale de la source souhaitée auprès de la RP2350 + // Synchronisation initiale de la source souhaitée et des vitesses auprès de la RP2350 let init_cmd = encode_src_command(last_source); let _ = socket.send_to(init_cmd.as_bytes(), target); + let mut last_sent_gears = gear_speed_limits.lock().sanitized(); + let init_gears_cmd = encode_gears_command(last_sent_gears); + let _ = socket.send_to(init_gears_cmd.as_bytes(), target); while !stop_flag.load(Ordering::Acquire) { let loop_start = Instant::now(); let desired_source = ControlSource::from_u8(active_source.load(Ordering::Acquire)); + let current_gears = gear_speed_limits.lock().sanitized(); + if current_gears != last_sent_gears { + last_sent_gears = current_gears; + let gears_cmd = encode_gears_command(current_gears); + let _ = socket.send_to(gears_cmd.as_bytes(), target); + log::info!( + "[Contrôleur] Vitesses CRSF synchronisées vers {} : {}", + target, + gears_cmd.trim() + ); + } let mut haptics_force_tick = false; // 1. Changement de source actif (G29 <-> Steam Controller) @@ -1563,6 +1640,7 @@ impl SteamControllerWorker { cfg.trigger_deadzone, st.armed, st.gear, + current_gears, ); let gas_released = pre_throt_pct == 0; @@ -1589,6 +1667,7 @@ impl SteamControllerWorker { cfg.trigger_deadzone, st.armed, st.gear, + current_gears, ); st.steering_norm = steer_norm; st.throttle_pct = throt_pct; @@ -1623,8 +1702,13 @@ impl SteamControllerWorker { } else { // Mode veille (G29 actif) : mise à jour lecture seule pour le moniteur des Réglages // sans armer, sans déclencher les actions cockpit et sans envoyer de trame $RC. - let (throt_pct, _) = - throttle_to_crsf(throttle_norm, cfg.trigger_deadzone, false, 0); + let (throt_pct, _) = throttle_to_crsf( + throttle_norm, + cfg.trigger_deadzone, + false, + 0, + current_gears, + ); let mut st = state.lock(); if let Some(&last_key) = pressed_keys.last() { st.last_button = key_code_label(last_key); @@ -1874,16 +1958,26 @@ mod tests { #[test] fn test_sequential_gearbox_throttle_and_brake() { + let def = GearSpeedLimits::default(); // Désarmé : toujours au neutre - assert_eq!(throttle_to_crsf(1.0, 0.05, false, 2), (100, CRSF_CHANNEL_MID)); + assert_eq!(throttle_to_crsf(1.0, 0.05, false, 2, def), (100, CRSF_CHANNEL_MID)); // Point mort N : toujours au neutre - assert_eq!(throttle_to_crsf(1.0, 0.05, true, 0), (100, CRSF_CHANNEL_MID)); + assert_eq!(throttle_to_crsf(1.0, 0.05, true, 0, def), (100, CRSF_CHANNEL_MID)); // 1ère vitesse plein gaz -> 1100 - assert_eq!(throttle_to_crsf(1.0, 0.05, true, 1), (100, 1100)); + assert_eq!(throttle_to_crsf(1.0, 0.05, true, 1, def), (100, 1100)); // 3ème vitesse plein gaz -> 1220 - assert_eq!(throttle_to_crsf(1.0, 0.05, true, 3), (100, 1220)); + assert_eq!(throttle_to_crsf(1.0, 0.05, true, 3, def), (100, 1220)); // Marche arrière plein gaz -> 800 - assert_eq!(throttle_to_crsf(1.0, 0.05, true, -1), (100, 800)); + assert_eq!(throttle_to_crsf(1.0, 0.05, true, -1, def), (100, 800)); + // Valeurs personnalisées + let custom = GearSpeedLimits { + forward_min: 1080, + forward_max: [1140, 1250, 1400], + reverse_min: 890, + reverse_max: 720, + }; + assert_eq!(throttle_to_crsf(1.0, 0.05, true, 2, custom), (100, 1250)); + assert_eq!(throttle_to_crsf(1.0, 0.05, true, -1, custom), (100, 720)); // Frein plein -> 1811 assert_eq!(brake_to_crsf(1.0, 0.05), (100, CRSF_CHANNEL_MAX)); assert_eq!(brake_to_crsf(0.02, 0.05), (0, CRSF_CHANNEL_MID)); @@ -1900,6 +1994,10 @@ mod tests { encode_rc_frame(992, 1150, 992, 992, true, 2), "$RC,992,1150,992,992,1,2\n" ); + assert_eq!( + encode_gears_command(GearSpeedLimits::default()), + "$GEARS,1065,1100,1150,1220,905,800\n" + ); } #[test] diff --git a/src/engine.rs b/src/engine.rs index 5243fcf..d5bbc7c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,6 +1,7 @@ use crate::config::AppConfig; use crate::controller::{ - ControlSource, SteamControllerConfig, SteamControllerState, SteamControllerWorker, + ControlSource, GearSpeedLimits, SteamControllerConfig, SteamControllerState, + SteamControllerWorker, }; use crate::dome_projection::{DomeWarpLut, ProjectionMode}; use crate::dvr::{DvrContainer, DvrMode, DvrRecorder}; @@ -120,6 +121,7 @@ pub struct EngineControlHandle { control_source_flag: Arc, controller_vibration_pct: Arc, controller_rumble_test: Arc, + gear_speed_limits: Arc>, monitor_ref: Arc>, optical_params: Arc>, dvr_toggle_cmd: Arc>>, @@ -266,6 +268,24 @@ impl EngineControlHandle { self.controller_rumble_test.store(true, Ordering::Release); log::info!("[Action Utilisateur] Test haptique Steam Controller déclenché."); } + + /// Met à jour à chaud les seuils/plafonds CRSF des vitesses (G29 + Steam Controller). + pub fn set_gear_speed_limits(&self, limits: GearSpeedLimits) { + let sanitized = limits.sanitized(); + let mut guard = self.gear_speed_limits.lock(); + if *guard != sanitized { + *guard = sanitized; + log::info!( + "[Action Utilisateur] Vitesses CRSF réglées : AV_MIN={} | 1ère={} | 2ème={} | 3ème={} | AR_MIN={} | AR_MAX={}", + sanitized.forward_min, + sanitized.forward_max[0], + sanitized.forward_max[1], + sanitized.forward_max[2], + sanitized.reverse_min, + sanitized.reverse_max + ); + } + } } pub struct FpvEngineController { @@ -282,6 +302,7 @@ pub struct FpvEngineController { control_source_flag: Arc, controller_vibration_pct: Arc, controller_rumble_test: Arc, + gear_speed_limits: Arc>, worker_handle: Option>, } @@ -315,6 +336,7 @@ impl FpvEngineController { control_source_flag: Arc::new(AtomicU8::new(ControlSource::G29.as_u8())), controller_vibration_pct: Arc::new(AtomicU8::new(100)), controller_rumble_test: Arc::new(AtomicBool::new(false)), + gear_speed_limits: Arc::new(Mutex::new(GearSpeedLimits::default())), worker_handle: None, } } @@ -327,6 +349,7 @@ impl FpvEngineController { control_source_flag: Arc::clone(&self.control_source_flag), controller_vibration_pct: Arc::clone(&self.controller_vibration_pct), controller_rumble_test: Arc::clone(&self.controller_rumble_test), + gear_speed_limits: Arc::clone(&self.gear_speed_limits), monitor_ref: Arc::clone(&self.monitor), optical_params: Arc::clone(&self.optical_params), dvr_toggle_cmd: Arc::clone(&self.dvr_toggle_cmd), @@ -391,6 +414,13 @@ impl FpvEngineController { self.controller_vibration_pct .store(cfg.controller_vibration.min(100), Ordering::Relaxed); self.controller_rumble_test.store(false, Ordering::Relaxed); + *self.gear_speed_limits.lock() = GearSpeedLimits { + forward_min: cfg.gaz_avant_min, + forward_max: [cfg.gear_1_max, cfg.gear_2_max, cfg.gear_3_max], + reverse_min: cfg.gaz_arriere_min, + reverse_max: cfg.gear_reverse_max, + } + .sanitized(); *self.optical_params.lock() = LiveOpticalParams { camera_fov_deg: cfg.camera_fov_deg, @@ -448,6 +478,7 @@ impl FpvEngineController { let control_source_clone = Arc::clone(&self.control_source_flag); let controller_vib_clone = Arc::clone(&self.controller_vibration_pct); let controller_test_clone = Arc::clone(&self.controller_rumble_test); + let gear_limits_clone = Arc::clone(&self.gear_speed_limits); let handle = thread::Builder::new() .name("fpv-engine-worker".into()) @@ -469,6 +500,7 @@ impl FpvEngineController { control_source_clone, controller_vib_clone, controller_test_clone, + gear_limits_clone, ); }) .expect("Impossible de créer le thread fpv-engine-worker"); @@ -799,6 +831,7 @@ fn run_engine_worker( control_source_flag: Arc, controller_vibration_pct: Arc, controller_rumble_test: Arc, + gear_speed_limits: Arc>, ) { let mut active_dvr: Option> = None; log::info!( @@ -892,6 +925,7 @@ fn run_engine_worker( Arc::clone(&control_source_flag), Arc::clone(&controller_vibration_pct), Arc::clone(&controller_rumble_test), + Arc::clone(&gear_speed_limits), control_for_ctrl, g29_rx_opt.as_ref().map(|rx| rx.state()), Some(Arc::clone(&shared_telem_state)), diff --git a/src/gui.rs b/src/gui.rs index 9f17f9e..188706f 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,5 +1,6 @@ use crate::config::AppConfig; -use crate::controller::ControlSource; +use crate::controller::{ControlSource, GearSpeedLimits}; +use crate::nomad_elrs::{CRSF_CHANNEL_MAX, CRSF_CHANNEL_MID, CRSF_CHANNEL_MIN}; use crate::dome_projection::ProjectionMode; use crate::dvr::{DvrContainer, DvrMode}; use crate::engine::{EngineControlHandle, FpvEngineController, LiveOpticalParams, RuntimeMonitor, SubsystemStatus}; @@ -1043,6 +1044,103 @@ impl eframe::App for SteamFrameGuiApp { ui.end_row(); }); + ui.add_space(6.0); + ui.label( + RichText::new("Vitesses CRSF (Volant G29 + Steam Controller)") + .strong(), + ); + let mut gears_changed = false; + egui::Grid::new("ctrl_gears_grid") + .num_columns(2) + .spacing([8.0, 6.0]) + .show(ui, |ui| { + ui.label("Seuil Gaz Avant (Min) :"); + gears_changed |= ui + .add(egui::Slider::new( + &mut self.cfg.gaz_avant_min, + CRSF_CHANNEL_MID..=CRSF_CHANNEL_MAX, + )) + .changed(); + ui.end_row(); + + ui.label("1ère Vitesse (Manœuvre) :"); + gears_changed |= ui + .add(egui::Slider::new( + &mut self.cfg.gear_1_max, + self.cfg.gaz_avant_min..=CRSF_CHANNEL_MAX, + )) + .changed(); + ui.end_row(); + + ui.label("2ème Vitesse (Normale) :"); + gears_changed |= ui + .add(egui::Slider::new( + &mut self.cfg.gear_2_max, + self.cfg.gaz_avant_min..=CRSF_CHANNEL_MAX, + )) + .changed(); + ui.end_row(); + + ui.label("3ème Vitesse (Rapide) :"); + gears_changed |= ui + .add(egui::Slider::new( + &mut self.cfg.gear_3_max, + self.cfg.gaz_avant_min..=CRSF_CHANNEL_MAX, + )) + .changed(); + ui.end_row(); + + ui.label("Seuil Gaz Arrière (Min) :"); + gears_changed |= ui + .add(egui::Slider::new( + &mut self.cfg.gaz_arriere_min, + CRSF_CHANNEL_MIN..=CRSF_CHANNEL_MID, + )) + .changed(); + ui.end_row(); + + ui.label("Plafond Marche Arrière (R) :"); + gears_changed |= ui + .add(egui::Slider::new( + &mut self.cfg.gear_reverse_max, + CRSF_CHANNEL_MIN..=self.cfg.gaz_arriere_min, + )) + .changed(); + ui.end_row(); + }); + + if ui.button("↺ Vitesses par défaut").clicked() { + let def = GearSpeedLimits::default(); + self.cfg.gaz_avant_min = def.forward_min; + self.cfg.gear_1_max = def.forward_max[0]; + self.cfg.gear_2_max = def.forward_max[1]; + self.cfg.gear_3_max = def.forward_max[2]; + self.cfg.gaz_arriere_min = def.reverse_min; + self.cfg.gear_reverse_max = def.reverse_max; + gears_changed = true; + } + + if gears_changed { + let sanitized = GearSpeedLimits { + forward_min: self.cfg.gaz_avant_min, + forward_max: [ + self.cfg.gear_1_max, + self.cfg.gear_2_max, + self.cfg.gear_3_max, + ], + reverse_min: self.cfg.gaz_arriere_min, + reverse_max: self.cfg.gear_reverse_max, + } + .sanitized(); + self.cfg.gaz_avant_min = sanitized.forward_min; + self.cfg.gear_1_max = sanitized.forward_max[0]; + self.cfg.gear_2_max = sanitized.forward_max[1]; + self.cfg.gear_3_max = sanitized.forward_max[2]; + self.cfg.gaz_arriere_min = sanitized.reverse_min; + self.cfg.gear_reverse_max = sanitized.reverse_max; + self.controls.set_gear_speed_limits(sanitized); + } + ui.add_space(6.0); egui::Frame::group(ui.style()) .fill(Color32::from_rgb(16, 20, 28)) diff --git a/src/gui_gpui.rs b/src/gui_gpui.rs index be2d884..94791b6 100644 --- a/src/gui_gpui.rs +++ b/src/gui_gpui.rs @@ -1,5 +1,5 @@ use crate::config::AppConfig; -use crate::controller::{ControlSource, SteamControllerState}; +use crate::controller::{ControlSource, GearSpeedLimits, SteamControllerState}; use crate::dome_projection::ProjectionMode; use crate::dvr::{DvrContainer, DvrMode}; use crate::engine::{ @@ -230,9 +230,10 @@ impl SteamFrameGpuiApp { } self.latest_pose = mon.latest_head_pose; self.latest_telem = mon.latest_telemetry.clone(); - if self.latest_g29.packets_received != mon.latest_g29.packets_received - || self.latest_g29.connected != mon.latest_g29.connected + if self.latest_g29.connected != mon.latest_g29.connected || self.latest_g29.last_button != mon.latest_g29.last_button + || (self.active_tab == ActiveTab::Settings + && self.latest_g29.packets_received != mon.latest_g29.packets_received) { changed = true; } @@ -632,6 +633,8 @@ impl Render for SteamFrameGpuiApp { .flex() .flex_col() .flex_1() + .min_h_0() + .min_w_0() .overflow_hidden() .child(match self.active_tab { ActiveTab::Cockpit => self.render_cockpit_view(cx).into_any_element(), @@ -651,6 +654,7 @@ impl SteamFrameGpuiApp { div() .flex() .flex_row() + .flex_wrap() .justify_between() .items_center() .bg(rgb(COLOR_SURFACE_CARD)) @@ -664,6 +668,7 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() .items_center() .gap_2p5() .child( @@ -681,6 +686,7 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() .items_center() .gap_1() .bg(rgb(COLOR_SURFACE_ELEVATED)) @@ -705,6 +711,7 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() .items_center() .gap_2() .child( @@ -907,6 +914,8 @@ impl SteamFrameGpuiApp { .flex() .flex_col() .size_full() + .min_h_0() + .min_w_0() .relative() .bg(rgb(0x030508)) .border_1() @@ -915,9 +924,20 @@ impl SteamFrameGpuiApp { .overflow_hidden() .items_center() .justify_center() - // 2.1 Flux Vidéo + // 2.1 Flux Vidéo (cadrage proportionnel intégral sans rognage) .child(if let Some(ref img_src) = self.current_render_image { - div().size_full().child(img(img_src.clone()).size_full()) + div() + .flex() + .items_center() + .justify_center() + .size_full() + .min_h_0() + .min_w_0() + .child( + img(img_src.clone()) + .size_full() + .object_fit(gpui::ObjectFit::Contain), + ) } else { div() .flex() @@ -940,6 +960,8 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() + .justify_center() .gap_2() .pt_2() .child( @@ -1132,6 +1154,8 @@ impl SteamFrameGpuiApp { .flex() .flex_col() .size_full() + .min_h_0() + .min_w_0() .id("settings_view_scroll") .overflow_y_scroll() .bg(rgb(COLOR_SURFACE_CARD)) @@ -1144,8 +1168,10 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() .justify_between() .items_center() + .gap_2() .border_b_1() .border_color(rgb(COLOR_BORDER_SUBTLE)) .pb_3() @@ -1206,6 +1232,7 @@ impl SteamFrameGpuiApp { "Mode Acquisition Vidéo", div() .flex() + .flex_wrap() .gap_2() .child(self.render_selectable_pill( "pill_mode_rndis", @@ -1231,6 +1258,7 @@ impl SteamFrameGpuiApp { "Canal 5.8 GHz (RTL8812EU)", div() .flex() + .flex_wrap() .gap_1p5() .child(self.render_channel_button(149, 5745, cx)) .child(self.render_channel_button(153, 5765, cx)) @@ -1242,6 +1270,7 @@ impl SteamFrameGpuiApp { "Codec Décodage", div() .flex() + .flex_wrap() .gap_2() .child(self.render_selectable_pill( "pill_codec_h264", @@ -1266,6 +1295,7 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() .gap_2() .pt_2() .child( @@ -1373,6 +1403,7 @@ impl SteamFrameGpuiApp { "Cible Réseau (clic pour éditer)", div() .flex() + .flex_wrap() .items_center() .gap_1() .child( @@ -1536,6 +1567,7 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() .gap_2() .pt_1() .child( @@ -1594,6 +1626,7 @@ impl SteamFrameGpuiApp { .child( div() .flex() + .flex_wrap() .gap_2() .pt_1() .child( @@ -1715,6 +1748,7 @@ impl SteamFrameGpuiApp { "Sens Direction Steam Controller", div() .flex() + .flex_wrap() .items_center() .gap_2() .child(self.render_selectable_pill( @@ -1868,6 +1902,91 @@ impl SteamFrameGpuiApp { .child("📳 Tester Vibrations"), ), )) + .child(self.render_setting_row( + "Réglage des Vitesses CRSF (G29 + Steam Controller)", + div() + .flex() + .flex_col() + .gap_2() + .p_2p5() + .rounded_md() + .bg(rgb(COLOR_BG_DARK)) + .border_1() + .border_color(rgb(COLOR_BORDER_SUBTLE)) + .child(self.render_gear_stepper_row( + "av_min", + "Seuil Gaz Avant (Min)", + self.cfg.gaz_avant_min, + COLOR_TEXT_MAIN, + |cfg, d| cfg.gaz_avant_min = cfg.gaz_avant_min.saturating_add_signed(d), + cx, + )) + .child(self.render_gear_stepper_row( + "g1", + "1ère Vitesse (Manœuvre)", + self.cfg.gear_1_max, + COLOR_CYAN, + |cfg, d| cfg.gear_1_max = cfg.gear_1_max.saturating_add_signed(d), + cx, + )) + .child(self.render_gear_stepper_row( + "g2", + "2ème Vitesse (Normale)", + self.cfg.gear_2_max, + COLOR_GREEN, + |cfg, d| cfg.gear_2_max = cfg.gear_2_max.saturating_add_signed(d), + cx, + )) + .child(self.render_gear_stepper_row( + "g3", + "3ème Vitesse (Rapide)", + self.cfg.gear_3_max, + COLOR_ORANGE, + |cfg, d| cfg.gear_3_max = cfg.gear_3_max.saturating_add_signed(d), + cx, + )) + .child(self.render_gear_stepper_row( + "ar_min", + "Seuil Gaz Arrière (Min)", + self.cfg.gaz_arriere_min, + COLOR_TEXT_MAIN, + |cfg, d| cfg.gaz_arriere_min = cfg.gaz_arriere_min.saturating_add_signed(d), + cx, + )) + .child(self.render_gear_stepper_row( + "ar_max", + "Plafond Marche Arrière (R)", + self.cfg.gear_reverse_max, + COLOR_RED, + |cfg, d| cfg.gear_reverse_max = cfg.gear_reverse_max.saturating_add_signed(d), + cx, + )) + .child( + div() + .flex() + .justify_end() + .pt_1() + .child( + div() + .id("btn_gears_reset_default") + .px_2p5() + .py_1() + .rounded_md() + .bg(rgb(COLOR_SURFACE_ELEVATED)) + .border_1() + .border_color(rgb(COLOR_BORDER_SUBTLE)) + .text_xs() + .font_weight(gpui::FontWeight::SEMIBOLD) + .text_color(rgb(COLOR_TEXT_MAIN)) + .cursor_pointer() + .on_click(cx.listener(|this, _, _, cx| { + this.reset_gear_speed_limits(); + cx.notify(); + })) + .child("↺ Valeurs par défaut (1065 / 1100-1150-1220 / 905-800)"), + ), + ), + )) .child(self.render_controller_monitor(cx)) .child( div() @@ -1894,6 +2013,7 @@ impl SteamFrameGpuiApp { "Affichage Casque VR (OpenXR)", div() .flex() + .flex_wrap() .gap_2() .child(self.render_selectable_pill( "pill_vr_on", @@ -1919,6 +2039,7 @@ impl SteamFrameGpuiApp { "Mode de Projection VR", div() .flex() + .flex_wrap() .gap_2() .child(self.render_selectable_pill( "pill_proj_dome", @@ -2004,6 +2125,7 @@ impl SteamFrameGpuiApp { "État Télémétrie & OSD", div() .flex() + .flex_wrap() .gap_2() .child(self.render_selectable_pill( "pill_telem_on", @@ -2047,6 +2169,7 @@ impl SteamFrameGpuiApp { "Format Conteneur", div() .flex() + .flex_wrap() .gap_2() .child(self.render_selectable_pill( "pill_dvr_mkv", @@ -2082,6 +2205,7 @@ impl SteamFrameGpuiApp { "Mode Enregistrement", div() .flex() + .flex_wrap() .gap_2() .child(self.render_selectable_pill( "pill_dvr_copy", @@ -2167,8 +2291,9 @@ impl SteamFrameGpuiApp { div() .flex() .flex_col() - .min_w(px(380.0)) + .min_w(px(300.0)) .flex_1() + .overflow_hidden() .bg(rgb(COLOR_SURFACE_ELEVATED)) .border_1() .border_color(rgb(COLOR_BORDER_SUBTLE)) @@ -2242,6 +2367,7 @@ impl SteamFrameGpuiApp { div() .flex() + .flex_wrap() .items_center() .justify_between() .gap_2() @@ -2266,6 +2392,107 @@ impl SteamFrameGpuiApp { ) } + fn apply_gear_speed_limits(&mut self) { + let sanitized = GearSpeedLimits { + forward_min: self.cfg.gaz_avant_min, + forward_max: [ + self.cfg.gear_1_max, + self.cfg.gear_2_max, + self.cfg.gear_3_max, + ], + reverse_min: self.cfg.gaz_arriere_min, + reverse_max: self.cfg.gear_reverse_max, + } + .sanitized(); + self.cfg.gaz_avant_min = sanitized.forward_min; + self.cfg.gear_1_max = sanitized.forward_max[0]; + self.cfg.gear_2_max = sanitized.forward_max[1]; + self.cfg.gear_3_max = sanitized.forward_max[2]; + self.cfg.gaz_arriere_min = sanitized.reverse_min; + self.cfg.gear_reverse_max = sanitized.reverse_max; + self.controls.set_gear_speed_limits(sanitized); + } + + fn reset_gear_speed_limits(&mut self) { + let def = GearSpeedLimits::default(); + self.cfg.gaz_avant_min = def.forward_min; + self.cfg.gear_1_max = def.forward_max[0]; + self.cfg.gear_2_max = def.forward_max[1]; + self.cfg.gear_3_max = def.forward_max[2]; + self.cfg.gaz_arriere_min = def.reverse_min; + self.cfg.gear_reverse_max = def.reverse_max; + self.controls.set_gear_speed_limits(def); + } + + fn render_gear_stepper_row( + &self, + id_prefix: &'static str, + label: &'static str, + value: u16, + accent_color: u32, + mutator: fn(&mut AppConfig, i16), + cx: &mut Context, + ) -> impl IntoElement { + let step_btn = |suffix: &'static str, txt: &'static str, delta: i16| { + let btn_id = gpui::SharedString::from(format!("btn_gear_{}_{}", id_prefix, suffix)); + div() + .id(btn_id) + .px_2() + .py_0p5() + .rounded_md() + .bg(rgb(COLOR_SURFACE_ELEVATED)) + .border_1() + .border_color(rgb(COLOR_BORDER_SUBTLE)) + .text_xs() + .font_weight(gpui::FontWeight::BOLD) + .cursor_pointer() + .on_click(cx.listener(move |this, _, _, cx| { + mutator(&mut this.cfg, delta); + this.apply_gear_speed_limits(); + cx.notify(); + })) + .child(txt) + }; + + div() + .flex() + .flex_wrap() + .items_center() + .justify_between() + .gap_2() + .child( + div() + .text_xs() + .text_color(rgb(COLOR_TEXT_MAIN)) + .child(label), + ) + .child( + div() + .flex() + .items_center() + .gap_1p5() + .child(step_btn("m10", "−10", -10)) + .child(step_btn("m5", "−5", -5)) + .child( + div() + .w(px(56.0)) + .py_0p5() + .rounded_md() + .bg(rgb(COLOR_SURFACE_CARD)) + .border_1() + .border_color(rgb(accent_color)) + .text_xs() + .font_weight(gpui::FontWeight::BOLD) + .text_color(rgb(accent_color)) + .flex() + .justify_center() + .child(format!("{}", value)), + ) + .child(step_btn("p5", "+5", 5)) + .child(step_btn("p10", "+10", 10)), + ) + } + fn render_setting_row(&self, label: &str, control: impl IntoElement) -> impl IntoElement { div() .flex() @@ -2373,6 +2600,8 @@ impl SteamFrameGpuiApp { .flex() .flex_col() .size_full() + .min_h_0() + .min_w_0() .id("telemetry_view_scroll") .overflow_y_scroll() .bg(rgb(COLOR_SURFACE_CARD)) @@ -2775,6 +3004,8 @@ impl SteamFrameGpuiApp { .flex() .flex_col() .flex_1() + .min_h_0() + .min_w_0() .id("logs_scroll_tab") .overflow_y_scroll() .px_2()