diff --git a/firmware/g29_crsf/g29_crsf.ino b/firmware/g29_crsf/g29_crsf.ino index 7a99aab..4163e3c 100644 --- a/firmware/g29_crsf/g29_crsf.ino +++ b/firmware/g29_crsf/g29_crsf.ino @@ -209,6 +209,7 @@ volatile uint32_t momentDesarmement_ms = 0; volatile uint8_t rugositeTerrainExterne = 0; // 0 à 255 (pilotable via CRSF télémétrie / FC) volatile float dernierAccelPct = 0.0f; // 0.0 à 100.0% volatile float dernierFreinPct = 0.0f; // 0.0 à 100.0% +volatile float dernierEmbrayagePct = 0.0f; // 0.0 à 100.0% volatile uint32_t momentDernierChocRapport_ms = 0; // Horodatage du passage de vitesse volatile int8_t directionDernierChocRapport = 0; // -1 gauche, +1 droite volatile bool forceEstAuNeutre = false; @@ -232,6 +233,8 @@ volatile bool extArme = false; volatile int8_t extRapport = 0; volatile uint8_t extAccelPct = 0; volatile uint8_t extFreinPct = 0; +volatile uint8_t extEmbrayagePct = 0; +volatile int8_t extVolantPct = 0; volatile uint32_t dernierPaquetRC_ms = 0; static inline bool estActuellementArme() { @@ -406,6 +409,8 @@ void traiterLigneSerie(char* commande) { extRapport = 0; extAccelPct = 0; extFreinPct = 0; + extEmbrayagePct = 0; + extVolantPct = 0; extRcActif = false; sourceActive = SRC_G29; majLedRequise = true; @@ -634,8 +639,17 @@ void traiterLigneSerie(char* commande) { 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); @@ -1030,14 +1044,31 @@ void gererSortieCockpit() { int brake = (sourceActive == SRC_EXT) ? (int)extFreinPct : (int)constrain(dernierFreinPct + 0.5f, 0.0f, 100.0f); + int clutch = (sourceActive == SRC_EXT) + ? (int)extEmbrayagePct + : (int)constrain(dernierEmbrayagePct + 0.5f, 0.0f, 100.0f); + int32_t dMax = (maxDeltaVolant > 0) ? maxDeltaVolant : DELTA_MAX_VOLANT_BRUT; + int steer = (sourceActive == SRC_EXT) + ? (int)extVolantPct + : (int)constrain(((float)dernierVolantCentre / (float)dMax) * 100.0f, -100.0f, 100.0f); + uint16_t ch1 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.volant - 1]; + uint16_t ch2 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.accel - 1]; + uint16_t ch3 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.frein - 1]; + uint16_t ch4 = failsafeActif ? CRSF_VAL_MID : crsf_channels[configSysteme.embrayage - 1]; const char* srcStr = (sourceActive == SRC_EXT) ? "EXT" : "G29"; - char bufEtat[112]; + char bufEtat[176]; snprintf(bufEtat, sizeof(bufEtat), - "$G29,ARM=%d,GEAR=%d,THROT=%d,BRAKE=%d,FFB=%d,VIB=%d,FAIL=%d,SRC=%s", + "$G29,ARM=%d,GEAR=%d,THROT=%d,BRAKE=%d,CLUTCH=%d,STEER=%d,CH1=%u,CH2=%u,CH3=%u,CH4=%u,FFB=%d,VIB=%d,FAIL=%d,SRC=%s", armeOut ? 1 : 0, (int)rapportOut, throt, brake, + clutch, + steer, + (unsigned int)ch1, + (unsigned int)ch2, + (unsigned int)ch3, + (unsigned int)ch4, (int)configSysteme.forceRessort, (int)configSysteme.intensiteVibration, failsafeActif ? 1 : 0, @@ -1061,6 +1092,8 @@ void gererTimeoutRcExterne() { extArme = false; extAccelPct = 0; extFreinPct = 0; + extEmbrayagePct = 0; + extVolantPct = 0; reinitialiserCanauxCRSF(); majLedRequise = true; cockpitEtatModifie = true; @@ -1550,6 +1583,7 @@ void reinitialiserEtatVolant() { dernierVolantCentre = 0; dernierAccelPct = 0.0f; dernierFreinPct = 0.0f; + dernierEmbrayagePct = 0.0f; rugositeTerrainExterne = 0; } @@ -1639,9 +1673,10 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i uint16_t freinRaw = 65535 - ((report[48] << 8) | report[47]); uint16_t embrayageRaw = 65535 - ((report[50] << 8) | report[49]); - // Pourcentages des pédales pour la synthèse haptique (0.0 à 100.0%) + // Pourcentages des pédales pour la synthèse haptique et le moniteur cockpit (0.0 à 100.0%) dernierAccelPct = (accelRaw > DEADZONE_PEDALE) ? (((float)(accelRaw - DEADZONE_PEDALE) / (float)(65535 - DEADZONE_PEDALE)) * 100.0f) : 0.0f; dernierFreinPct = (freinRaw > DEADZONE_PEDALE) ? (((float)(freinRaw - DEADZONE_PEDALE) / (float)(65535 - DEADZONE_PEDALE)) * 100.0f) : 0.0f; + dernierEmbrayagePct = (embrayageRaw > DEADZONE_PEDALE) ? (((float)(embrayageRaw - DEADZONE_PEDALE) / (float)(65535 - DEADZONE_PEDALE)) * 100.0f) : 0.0f; uint8_t boutonsEpaule = report[6]; bool pPlusActuel = (boutonsEpaule & PALETTE_PLUS_MASK); diff --git a/src/controller.rs b/src/controller.rs index ba26d48..f393b3a 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -197,6 +197,8 @@ pub struct SteamControllerState { pub clutch_crsf: u16, pub packets_sent: u64, pub send_errors: u64, + pub last_button: String, + pub last_button_at: Option, pub toast_text: String, pub toast_until: Option, } @@ -219,6 +221,8 @@ impl Default for SteamControllerState { clutch_crsf: CRSF_CHANNEL_MID, packets_sent: 0, send_errors: 0, + last_button: String::new(), + last_button_at: None, toast_text: String::new(), toast_until: None, } @@ -259,8 +263,14 @@ impl SteamControllerState { failsafe: !self.connected || hub_failsafe, armed: self.armed, gear: self.gear, + steering_pct: (self.steering_norm * 100.0).round().clamp(-100.0, 100.0) as i8, throttle_pct: self.throttle_pct, brake_pct: self.brake_pct, + clutch_pct: self.clutch_pct, + steer_crsf: self.steer_crsf, + throttle_crsf: self.throttle_crsf, + brake_crsf: self.brake_crsf, + clutch_crsf: self.clutch_crsf, ffb_raw: 0, ffb_pct: 0, vib_pct: 0, @@ -271,12 +281,37 @@ impl SteamControllerState { } else { None }, + last_button: self.last_button.clone(), + last_button_at: self.last_button_at, toast_text: self.toast_text.clone(), toast_until: self.toast_until, } } } +pub fn key_code_label(key: u16) -> String { + match key { + BTN_START => "Menu / Start (ARM)".to_string(), + BTN_SELECT => "View / Select (DVR)".to_string(), + BTN_MODE => "Steam (Tare HT)".to_string(), + BTN_SOUTH => "A (Tare HT)".to_string(), + BTN_EAST => "B (DVR)".to_string(), + BTN_NORTH => "Y (OSD)".to_string(), + BTN_WEST => "X (Proj VR)".to_string(), + BTN_TR => "RB (Rapport +)".to_string(), + BTN_TL => "LB (Rapport -)".to_string(), + BTN_GRIPR => "R4 Grip (Rapport +)".to_string(), + BTN_GRIPL => "L4 Grip (Rapport -)".to_string(), + BTN_GRIPL2 => "L5 Grip (Embrayage)".to_string(), + BTN_THUMBR => "R3 (Tare HT)".to_string(), + BTN_DPAD_UP => "D-Pad Haut (Tilt +1)".to_string(), + BTN_DPAD_DOWN => "D-Pad Bas (Tilt -1)".to_string(), + BTN_DPAD_LEFT => "D-Pad Gauche (Pan -1)".to_string(), + BTN_DPAD_RIGHT => "D-Pad Droite (Pan +1)".to_string(), + other => format!("KEY_{}", other), + } +} + // ========================================================================= // LOGIQUE DE CALCUL DES CANAUX CRSF & BOÎTE SÉQUENTIELLE // ========================================================================= @@ -878,29 +913,29 @@ impl SteamControllerWorker { } // 4. Traitement axes, boîte séquentielle, boutons cockpit & émission $RC à 100 Hz - if desired_source == ControlSource::SteamController { - if let Some(ref gp) = gamepad { - let steer_norm = gp.steer_axis.normalized_signed(); - let throttle_norm = gp.throttle_axis.normalized_unsigned(); - let brake_norm = gp.brake_axis.normalized_unsigned(); - let clutch_pressed = gp.clutch_pressed; + if let Some(ref gp) = gamepad { + let steer_norm = gp.steer_axis.normalized_signed(); + let throttle_norm = gp.throttle_axis.normalized_unsigned(); + let brake_norm = gp.brake_axis.normalized_unsigned(); + let clutch_pressed = gp.clutch_pressed; - let steer_crsf = steering_to_crsf( - steer_norm, - cfg.steering_deadzone, - cfg.steering_expo, - cfg.invert_steering, - ); - let (brake_pct, brake_crsf) = brake_to_crsf(brake_norm, cfg.trigger_deadzone); - let clutch_pct = if clutch_pressed { 100 } else { 0 }; - let clutch_crsf = if clutch_pressed { - CRSF_CHANNEL_MAX - } else { - CRSF_CHANNEL_MID - }; + let steer_crsf = steering_to_crsf( + steer_norm, + cfg.steering_deadzone, + cfg.steering_expo, + cfg.invert_steering, + ); + let (brake_pct, brake_crsf) = brake_to_crsf(brake_norm, cfg.trigger_deadzone); + let clutch_pct = if clutch_pressed { 100 } else { 0 }; + let clutch_crsf = if clutch_pressed { + CRSF_CHANNEL_MAX + } else { + CRSF_CHANNEL_MID + }; - // Évaluer les boutons avant le calcul final des gaz - let (armed, gear, throttle_pct, throttle_crsf) = { + if desired_source == ControlSource::SteamController { + // Mode actif : armement, boîte séquentielle, raccourcis cockpit et émission $RC 100 Hz + let (armed, gear, throttle_crsf) = { let mut st = state.lock(); let (pre_throt_pct, _) = throttle_to_crsf( throttle_norm, @@ -911,8 +946,14 @@ impl SteamControllerWorker { let gas_released = pre_throt_pct == 0; for &key in &pressed_keys { + st.last_button = key_code_label(key); + st.last_button_at = Some(Instant::now()); Self::handle_key_press(key, gas_released, &mut st, &control); } + if clutch_pressed && st.clutch_pct == 0 { + st.last_button = key_code_label(BTN_GRIPL2); + st.last_button_at = Some(Instant::now()); + } let (throt_pct, throt_crsf) = throttle_to_crsf( throttle_norm, @@ -929,10 +970,9 @@ impl SteamControllerWorker { st.brake_crsf = brake_crsf; st.clutch_crsf = clutch_crsf; - (st.armed, st.gear, throt_pct, throt_crsf) + (st.armed, st.gear, throt_crsf) }; - let _ = throttle_pct; let frame = encode_rc_frame( steer_crsf, throttle_crsf, @@ -951,6 +991,27 @@ impl SteamControllerWorker { st.send_errors = st.send_errors.wrapping_add(1); } } + } 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 mut st = state.lock(); + if let Some(&last_key) = pressed_keys.last() { + st.last_button = key_code_label(last_key); + st.last_button_at = Some(Instant::now()); + } else if clutch_pressed && st.clutch_pct == 0 { + st.last_button = key_code_label(BTN_GRIPL2); + st.last_button_at = Some(Instant::now()); + } + st.steering_norm = steer_norm; + st.throttle_pct = throt_pct; + st.brake_pct = brake_pct; + st.clutch_pct = clutch_pct; + st.steer_crsf = steer_crsf; + st.throttle_crsf = CRSF_CHANNEL_MID; + st.brake_crsf = brake_crsf; + st.clutch_crsf = clutch_crsf; } } diff --git a/src/g29_bridge.rs b/src/g29_bridge.rs index 36ce4b3..816ff94 100644 --- a/src/g29_bridge.rs +++ b/src/g29_bridge.rs @@ -1,5 +1,6 @@ use crate::controller::ControlSource; use crate::engine::EngineControlHandle; +use crate::nomad_elrs::{CRSF_CHANNEL_MAX, CRSF_CHANNEL_MID, CRSF_CHANNEL_MIN}; use crate::telemetry_osd::{TelemetryReceiver, TelemetryState}; use anyhow::Result; use parking_lot::Mutex; @@ -19,14 +20,22 @@ pub struct G29State { pub failsafe: bool, pub armed: bool, pub gear: i8, + pub steering_pct: i8, pub throttle_pct: u8, pub brake_pct: u8, + pub clutch_pct: u8, + pub steer_crsf: u16, + pub throttle_crsf: u16, + pub brake_crsf: u16, + pub clutch_crsf: u16, pub ffb_raw: u8, pub ffb_pct: u8, pub vib_pct: u8, pub remote_source: ControlSource, pub packets_received: u64, pub last_packet: Option, + pub last_button: String, + pub last_button_at: Option, pub toast_text: String, pub toast_until: Option, } @@ -38,14 +47,22 @@ impl Default for G29State { failsafe: false, armed: false, gear: 0, + steering_pct: 0, throttle_pct: 0, brake_pct: 0, + clutch_pct: 0, + steer_crsf: CRSF_CHANNEL_MID, + throttle_crsf: CRSF_CHANNEL_MID, + brake_crsf: CRSF_CHANNEL_MID, + clutch_crsf: CRSF_CHANNEL_MID, ffb_raw: 140, ffb_pct: 55, vib_pct: 50, remote_source: ControlSource::G29, packets_received: 0, last_packet: None, + last_button: String::new(), + last_button_at: None, toast_text: String::new(), toast_until: None, } @@ -279,6 +296,36 @@ impl G29BridgeReceiver { st.brake_pct = val.min(100); } } + "CLUTCH" => { + if let Ok(val) = v.trim().parse::() { + st.clutch_pct = val.min(100); + } + } + "STEER" => { + if let Ok(val) = v.trim().parse::() { + st.steering_pct = val.clamp(-100, 100); + } + } + "CH1" => { + if let Ok(val) = v.trim().parse::() { + st.steer_crsf = val.clamp(CRSF_CHANNEL_MIN, CRSF_CHANNEL_MAX); + } + } + "CH2" => { + if let Ok(val) = v.trim().parse::() { + st.throttle_crsf = val.clamp(CRSF_CHANNEL_MIN, CRSF_CHANNEL_MAX); + } + } + "CH3" => { + if let Ok(val) = v.trim().parse::() { + st.brake_crsf = val.clamp(CRSF_CHANNEL_MIN, CRSF_CHANNEL_MAX); + } + } + "CH4" => { + if let Ok(val) = v.trim().parse::() { + st.clutch_crsf = val.clamp(CRSF_CHANNEL_MIN, CRSF_CHANNEL_MAX); + } + } "FFB" => { if let Ok(val) = v.trim().parse::() { st.ffb_raw = val; @@ -398,11 +445,15 @@ impl G29BridgeReceiver { G29ButtonEvent::Options | G29ButtonEvent::L3 => None, }; - if let Some(msg) = toast { + { let mut st = state.lock(); st.connected = true; st.last_packet = Some(Instant::now()); - st.set_toast(msg); + st.last_button = btn_name.trim().to_string(); + st.last_button_at = Some(Instant::now()); + if let Some(msg) = toast { + st.set_toast(msg); + } } } } @@ -443,7 +494,7 @@ mod tests { } G29BridgeReceiver::process_line( - "$G29,ARM=1,GEAR=-1,THROT=0,BRAKE=80,FFB=200,VIB=50,FAIL=0,SRC=EXT", + "$G29,ARM=1,GEAR=-1,THROT=0,BRAKE=80,CLUTCH=45,STEER=-35,CH1=1280,CH2=890,CH3=1640,CH4=1360,FFB=200,VIB=50,FAIL=0,SRC=EXT", &state, None, None, @@ -452,6 +503,12 @@ mod tests { let st = state.lock(); assert_eq!(st.gear_label(), "R"); assert_eq!(st.brake_pct, 80); + assert_eq!(st.clutch_pct, 45); + assert_eq!(st.steering_pct, -35); + assert_eq!(st.steer_crsf, 1280); + assert_eq!(st.throttle_crsf, 890); + assert_eq!(st.brake_crsf, 1640); + assert_eq!(st.clutch_crsf, 1360); assert_eq!(st.remote_source, ControlSource::SteamController); assert_eq!(st.active_toast(), Some("FFB 78%")); } @@ -462,6 +519,7 @@ mod tests { let state = Arc::new(Mutex::new(G29State::default())); G29BridgeReceiver::process_line("$G29_EVT,BTN=CROSS", &state, None, None); let st = state.lock(); + assert_eq!(st.last_button, "CROSS"); assert_eq!(st.active_toast(), Some("TARE HT OK")); } diff --git a/src/gui.rs b/src/gui.rs index e1fc0bd..15a61ea 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -26,6 +26,7 @@ pub struct SteamFrameGuiApp { video_texture: Option, last_displayed_frame_seq: u64, control_switch_error: String, + monitor_source_override: Option, } impl SteamFrameGuiApp { @@ -54,6 +55,7 @@ impl SteamFrameGuiApp { video_texture: None, last_displayed_frame_seq: 0, control_switch_error: String::new(), + monitor_source_override: None, } } @@ -289,6 +291,7 @@ impl eframe::App for SteamFrameGuiApp { g29_status, controller_status, active_ctrl_source, + latest_g29, latest_ctrl, dvr_status, dvr_recording, @@ -328,6 +331,7 @@ impl eframe::App for SteamFrameGuiApp { mon.g29_status.clone(), mon.controller_status.clone(), mon.active_control_source, + mon.latest_g29.clone(), mon.latest_controller.clone(), mon.dvr_status.clone(), mon.dvr_recording, @@ -1017,6 +1021,169 @@ impl eframe::App for SteamFrameGuiApp { ui.end_row(); }); + ui.add_space(6.0); + egui::Frame::group(ui.style()) + .fill(Color32::from_rgb(16, 20, 28)) + .inner_margin(egui::Margin::same(8.0)) + .show(ui, |ui| { + let viewed = self + .monitor_source_override + .unwrap_or(active_ctrl_source); + ui.horizontal(|ui| { + ui.label( + RichText::new(format!( + "📊 Moniteur — {}", + viewed.label() + )) + .strong() + .color(Color32::from_rgb(90, 220, 255)), + ); + ui.selectable_value( + &mut self.monitor_source_override, + None, + "Auto", + ); + ui.selectable_value( + &mut self.monitor_source_override, + Some(ControlSource::G29), + "G29", + ); + ui.selectable_value( + &mut self.monitor_source_override, + Some(ControlSource::SteamController), + "Steam", + ); + }); + + let ( + online, + failsafe, + armed, + gear_lbl, + steer_pct, + steer_crsf, + throt_pct, + throt_crsf, + brake_pct, + brake_crsf, + clutch_pct, + clutch_crsf, + last_btn, + detail, + ) = if viewed == ControlSource::SteamController { + ( + latest_ctrl.connected, + !latest_ctrl.connected, + latest_ctrl.armed, + latest_ctrl.gear_label(), + latest_ctrl.steering_norm * 100.0, + latest_ctrl.steer_crsf, + latest_ctrl.throttle_pct, + latest_ctrl.throttle_crsf, + latest_ctrl.brake_pct, + latest_ctrl.brake_crsf, + latest_ctrl.clutch_pct, + latest_ctrl.clutch_crsf, + if latest_ctrl.last_button.is_empty() { + "Aucun".to_string() + } else { + latest_ctrl.last_button.clone() + }, + format!( + "$RC envoyés : {} (Err : {})", + latest_ctrl.packets_sent, latest_ctrl.send_errors + ), + ) + } else { + let live = latest_g29.is_live(); + ( + live, + !live || latest_g29.failsafe, + latest_g29.armed, + latest_g29.gear_label(), + latest_g29.steering_pct as f32, + latest_g29.steer_crsf, + latest_g29.throttle_pct, + latest_g29.throttle_crsf, + latest_g29.brake_pct, + latest_g29.brake_crsf, + latest_g29.clutch_pct, + latest_g29.clutch_crsf, + if latest_g29.last_button.is_empty() { + "Aucun".to_string() + } else { + latest_g29.last_button.clone() + }, + format!( + "FFB {}% | Vib {}% | RX {} | RP2350: {}", + latest_g29.ffb_pct, + latest_g29.vib_pct, + latest_g29.packets_received, + latest_g29.remote_source.wire_tag() + ), + ) + }; + + ui.horizontal_wrapped(|ui| { + ui.colored_label( + if online { + Color32::from_rgb(80, 235, 130) + } else { + Color32::GRAY + }, + if online { "● CONNECTÉ" } else { "○ HORS LIGNE" }, + ); + ui.label(RichText::new(format!("BOÎTE [{}]", gear_lbl)).strong()); + ui.colored_label( + if failsafe { + Color32::from_rgb(255, 80, 80) + } else if armed { + Color32::from_rgb(80, 235, 130) + } else { + Color32::from_rgb(255, 185, 60) + }, + if failsafe { + "FAILSAFE (CH5: 172)" + } else if armed { + "ARMED (CH5: 1811)" + } else { + "SAFE (CH5: 172)" + }, + ); + }); + + let steer_frac = ((steer_pct + 100.0) / 200.0).clamp(0.0, 1.0); + ui.add( + egui::ProgressBar::new(steer_frac) + .text(format!( + "Direction CH1 : {:+.0}% (CRSF {})", + steer_pct, steer_crsf + )), + ); + ui.add( + egui::ProgressBar::new((throt_pct as f32 / 100.0).clamp(0.0, 1.0)) + .text(format!( + "Accélérateur CH2 : {}% (CRSF {})", + throt_pct, throt_crsf + )), + ); + ui.add( + egui::ProgressBar::new((brake_pct as f32 / 100.0).clamp(0.0, 1.0)) + .text(format!( + "Frein CH3 : {}% (CRSF {})", + brake_pct, brake_crsf + )), + ); + ui.add( + egui::ProgressBar::new((clutch_pct as f32 / 100.0).clamp(0.0, 1.0)) + .text(format!( + "Embrayage CH4 : {}% (CRSF {})", + clutch_pct, clutch_crsf + )), + ); + ui.small(format!("🔘 Dernier bouton : {} • {}", last_btn, detail)); + }); + ui.small("Stick G: Dir | R2: Gaz | L2: Frein | RB/LB ou R4/L4: Boîte R/N/1/2/3 | Menu: Armer (au N)"); }); diff --git a/src/gui_gpui.rs b/src/gui_gpui.rs index bb69f57..f657827 100644 --- a/src/gui_gpui.rs +++ b/src/gui_gpui.rs @@ -86,6 +86,7 @@ pub struct SteamFrameGpuiApp { latest_g29: G29State, latest_controller: SteamControllerState, control_switch_error: String, + monitor_source_override: Option, log_entries: Vec, log_err_count: usize, log_warn_count: usize, @@ -151,6 +152,7 @@ impl SteamFrameGpuiApp { latest_g29: G29State::default(), latest_controller: SteamControllerState::default(), control_switch_error: String::new(), + monitor_source_override: None, log_entries: Vec::new(), log_err_count: 0, log_warn_count: 0, @@ -230,6 +232,7 @@ impl SteamFrameGpuiApp { 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 + || self.latest_g29.last_button != mon.latest_g29.last_button { changed = true; } @@ -240,8 +243,12 @@ impl SteamFrameGpuiApp { || self.latest_controller.gear != mon.latest_controller.gear || self.latest_controller.throttle_pct != mon.latest_controller.throttle_pct || self.latest_controller.brake_pct != mon.latest_controller.brake_pct + || self.latest_controller.clutch_pct != mon.latest_controller.clutch_pct + || self.latest_controller.last_button != mon.latest_controller.last_button || (self.latest_controller.steering_norm - mon.latest_controller.steering_norm).abs() - > 0.02 + > 0.005 + || (self.active_tab == ActiveTab::Settings + && self.latest_controller.packets_sent != mon.latest_controller.packets_sent) { changed = true; } @@ -1736,6 +1743,7 @@ impl SteamFrameGpuiApp { )), ), )) + .child(self.render_controller_monitor(cx)) .child( div() .p_2() @@ -2748,6 +2756,443 @@ impl SteamFrameGpuiApp { .child(format!("{} : {}", label, if is_on { "ON" } else { "OFF" })) } + fn render_monitor_progress_bar( + &self, + label: &str, + pct: u8, + crsf: u16, + bar_color: u32, + ) -> impl IntoElement { + let frac = (pct as f32 / 100.0).clamp(0.0, 1.0); + div() + .flex() + .flex_col() + .gap_0p5() + .child( + div() + .flex() + .justify_between() + .text_xs() + .child(div().text_color(rgb(COLOR_TEXT_MAIN)).child(label.to_string())) + .child( + div() + .text_color(rgb(COLOR_TEXT_MUTED)) + .child(format!("{:3}% | CRSF {}", pct, crsf)), + ), + ) + .child( + div() + .w_full() + .h(px(7.0)) + .bg(rgb(COLOR_SURFACE_CARD)) + .border_1() + .border_color(rgb(COLOR_BORDER_SUBTLE)) + .rounded_full() + .overflow_hidden() + .child( + div() + .h_full() + .w(DefiniteLength::Fraction(frac)) + .bg(rgb(bar_color)), + ), + ) + } + + fn render_monitor_bipolar_bar( + &self, + label: &str, + pct_signed: f32, + crsf: u16, + bar_color: u32, + ) -> impl IntoElement { + let clamped = pct_signed.clamp(-100.0, 100.0); + let left_frac = if clamped < 0.0 { + (-clamped / 100.0).clamp(0.0, 1.0) + } else { + 0.0 + }; + let right_frac = if clamped > 0.0 { + (clamped / 100.0).clamp(0.0, 1.0) + } else { + 0.0 + }; + + div() + .flex() + .flex_col() + .gap_0p5() + .child( + div() + .flex() + .justify_between() + .text_xs() + .child(div().text_color(rgb(COLOR_TEXT_MAIN)).child(label.to_string())) + .child( + div() + .text_color(rgb(COLOR_TEXT_MUTED)) + .child(format!("{:+.0}% | CRSF {}", clamped, crsf)), + ), + ) + .child( + div() + .flex() + .items_center() + .w_full() + .h(px(8.0)) + .bg(rgb(COLOR_SURFACE_CARD)) + .border_1() + .border_color(rgb(COLOR_BORDER_SUBTLE)) + .rounded_full() + .overflow_hidden() + // Moitié gauche (remplissage depuis le centre vers la gauche) + .child( + div() + .flex() + .justify_end() + .w(DefiniteLength::Fraction(0.5)) + .h_full() + .child( + div() + .h_full() + .w(DefiniteLength::Fraction(left_frac)) + .bg(rgb(bar_color)), + ), + ) + // Repère central + .child(div().w(px(2.0)).h_full().bg(rgb(COLOR_TEXT_MUTED))) + // Moitié droite (remplissage depuis le centre vers la droite) + .child( + div() + .flex() + .justify_start() + .w(DefiniteLength::Fraction(0.5)) + .h_full() + .child( + div() + .h_full() + .w(DefiniteLength::Fraction(right_frac)) + .bg(rgb(bar_color)), + ), + ), + ) + } + + fn render_controller_monitor(&self, cx: &mut Context) -> impl IntoElement { + let viewed_source = self + .monitor_source_override + .unwrap_or(self.active_control_source); + let is_steam_view = viewed_source == ControlSource::SteamController; + + let ( + is_online, + is_failsafe, + is_armed, + gear, + gear_lbl, + steer_pct, + steer_crsf, + throttle_pct, + throttle_crsf, + brake_pct, + brake_crsf, + clutch_pct, + clutch_crsf, + last_btn, + detail_line, + ) = if is_steam_view { + let sc = &self.latest_controller; + let detail = if sc.connected { + format!( + "Périphérique : {} ({}) • Trames $RC : {} (Err : {})", + sc.device_name, sc.device_path, sc.packets_sent, sc.send_errors + ) + } else { + "En attente d'un Steam Controller / Gamepad Linux (/dev/input/event*)".to_string() + }; + ( + sc.connected, + !sc.connected, + sc.armed, + sc.gear, + sc.gear_label(), + sc.steering_norm * 100.0, + sc.steer_crsf, + sc.throttle_pct, + sc.throttle_crsf, + sc.brake_pct, + sc.brake_crsf, + sc.clutch_pct, + sc.clutch_crsf, + if sc.last_button.is_empty() { + "Aucun".to_string() + } else { + sc.last_button.clone() + }, + detail, + ) + } else { + let g = &self.latest_g29; + let live = g.is_live(); + let detail = if live { + format!( + "FFB Ressort : {}% • Vibrations : {}% • Paquets UDP reçus : {}", + g.ffb_pct, g.vib_pct, g.packets_received + ) + } else { + format!( + "En attente du pont G29 sur UDP :{} (Paquets reçus : {})", + self.cfg.g29_udp_port, g.packets_received + ) + }; + ( + live, + !live || g.failsafe, + g.armed, + g.gear, + g.gear_label(), + g.steering_pct as f32, + g.steer_crsf, + g.throttle_pct, + g.throttle_crsf, + g.brake_pct, + g.brake_crsf, + g.clutch_pct, + g.clutch_crsf, + if g.last_button.is_empty() { + "Aucun".to_string() + } else { + g.last_button.clone() + }, + detail, + ) + }; + + let gear_color = match gear { + -1 => COLOR_RED, + 0 => COLOR_ORANGE, + 1 => COLOR_CYAN, + 2 => COLOR_GREEN, + 3 => 0xc084fc, + _ => COLOR_TEXT_MUTED, + }; + let arm_color = if is_failsafe { + COLOR_RED + } else if is_armed { + COLOR_GREEN + } else { + COLOR_ORANGE + }; + let arm_text = if is_failsafe { + "FAILSAFE (CH5: 172)" + } else if is_armed { + "ARMED (CH5: 1811)" + } else { + "SAFE (CH5: 172)" + }; + let throttle_color = if gear == -1 { + COLOR_RED + } else if is_armed && gear > 0 { + COLOR_GREEN + } else { + COLOR_ORANGE + }; + + let rp2350_sync_text = if self.latest_g29.is_live() { + format!("RP2350 : {}", self.latest_g29.remote_source.wire_tag()) + } else { + "RP2350 : hors ligne".to_string() + }; + let rp2350_sync_color = if !self.latest_g29.is_live() { + COLOR_TEXT_MUTED + } else if self.latest_g29.remote_source == self.active_control_source { + COLOR_GREEN + } else { + COLOR_ORANGE + }; + + div() + .flex() + .flex_col() + .gap_2() + .p_3() + .rounded_lg() + .bg(rgb(COLOR_BG_DARK)) + .border_1() + .border_color(if is_online { + rgb(COLOR_BORDER_ACTIVE) + } else { + rgb(COLOR_BORDER_SUBTLE) + }) + // En-tête du moniteur + sélecteur de vue (Auto / G29 / Steam) + .child( + div() + .flex() + .flex_wrap() + .justify_between() + .items_center() + .gap_2() + .child( + div() + .text_xs() + .font_weight(gpui::FontWeight::BOLD) + .text_color(rgb(COLOR_CYAN)) + .child(format!( + "📊 Moniteur Temps Réel — {}", + viewed_source.label() + )), + ) + .child( + div() + .flex() + .gap_1() + .child(self.render_selectable_pill( + "pill_mon_auto", + "Auto (actif)", + self.monitor_source_override.is_none(), + cx.listener(|this, _, _, cx| { + this.monitor_source_override = None; + cx.notify(); + }), + )) + .child(self.render_selectable_pill( + "pill_mon_g29", + "🏎️ G29", + self.monitor_source_override == Some(ControlSource::G29), + cx.listener(|this, _, _, cx| { + this.monitor_source_override = Some(ControlSource::G29); + cx.notify(); + }), + )) + .child(self.render_selectable_pill( + "pill_mon_steam", + "🎮 Steam", + self.monitor_source_override + == Some(ControlSource::SteamController), + cx.listener(|this, _, _, cx| { + this.monitor_source_override = + Some(ControlSource::SteamController); + cx.notify(); + }), + )), + ), + ) + // Badges d'état (En ligne, Rapport, ARM/CH5, Sync RP2350) + .child( + div() + .flex() + .flex_wrap() + .items_center() + .gap_2() + .child( + div() + .px_2() + .py_0p5() + .rounded_md() + .bg(rgb(COLOR_SURFACE_ELEVATED)) + .border_1() + .border_color(if is_online { + rgb(COLOR_GREEN) + } else { + rgb(COLOR_BORDER_SUBTLE) + }) + .text_xs() + .font_weight(gpui::FontWeight::BOLD) + .text_color(if is_online { + rgb(COLOR_GREEN) + } else { + rgb(COLOR_TEXT_MUTED) + }) + .child(if is_online { + "● CONNECTÉ" + } else { + "○ HORS LIGNE" + }), + ) + .child( + div() + .px_2() + .py_0p5() + .rounded_md() + .bg(rgb(COLOR_SURFACE_ELEVATED)) + .border_1() + .border_color(rgb(gear_color)) + .text_xs() + .font_weight(gpui::FontWeight::BOLD) + .text_color(rgb(gear_color)) + .child(format!("BOÎTE [{}]", gear_lbl)), + ) + .child( + div() + .px_2() + .py_0p5() + .rounded_md() + .bg(rgb(COLOR_SURFACE_ELEVATED)) + .border_1() + .border_color(rgb(arm_color)) + .text_xs() + .font_weight(gpui::FontWeight::BOLD) + .text_color(rgb(arm_color)) + .child(arm_text), + ) + .child( + div() + .px_2() + .py_0p5() + .rounded_md() + .bg(rgb(COLOR_SURFACE_ELEVATED)) + .border_1() + .border_color(rgb(rp2350_sync_color)) + .text_xs() + .text_color(rgb(rp2350_sync_color)) + .child(rp2350_sync_text), + ), + ) + // Jauges axes temps réel (CH1..CH4) + .child(self.render_monitor_bipolar_bar( + "Direction (CH1)", + steer_pct, + steer_crsf, + COLOR_CYAN, + )) + .child(self.render_monitor_progress_bar( + "Accélérateur (CH2)", + throttle_pct, + throttle_crsf, + throttle_color, + )) + .child(self.render_monitor_progress_bar( + "Frein (CH3)", + brake_pct, + brake_crsf, + COLOR_RED, + )) + .child(self.render_monitor_progress_bar( + "Embrayage (CH4)", + clutch_pct, + clutch_crsf, + COLOR_CYAN, + )) + // Pied du moniteur : dernier bouton & détails liaison + .child( + div() + .flex() + .flex_wrap() + .justify_between() + .items_center() + .gap_2() + .pt_1() + .border_t_1() + .border_color(rgb(COLOR_BORDER_SUBTLE)) + .text_xs() + .child( + div() + .text_color(rgb(COLOR_ORANGE)) + .child(format!("🔘 Dernier bouton : {}", last_btn)), + ) + .child(div().text_color(rgb(COLOR_TEXT_MUTED)).child(detail_line)), + ) + } + fn render_crsf_gauge(&self, label: &str, deg: f32, crsf: u16, max_deg: f32) -> impl IntoElement { let norm = ((deg / max_deg) * 0.5 + 0.5).clamp(0.0, 1.0);