use crate::g29_bridge::G29State; use crate::nomad_elrs::{CRSF_CHANNEL_MAX, CRSF_CHANNEL_MID, CRSF_CHANNEL_MIN}; use std::time::{Duration, Instant}; // ========================================================================= // CONSTANTES EVDEV LINUX (identiques sur x86_64 et aarch64) // ========================================================================= pub(super) const EV_KEY: u16 = 0x01; pub(super) const EV_ABS: u16 = 0x03; pub(super) const ABS_X: u16 = 0x00; pub(super) const ABS_Z: u16 = 0x02; pub(super) const ABS_RZ: u16 = 0x05; pub(super) const ABS_HAT2X: u16 = 0x14; // 20 : Gâchette droite (R2) sur hid-steam (Steam Controller / Deck) pub(super) const ABS_HAT2Y: u16 = 0x15; // 21 : Gâchette gauche (L2) sur hid-steam (Steam Controller / Deck) pub(super) const BTN_SOUTH: u16 = 304; // A pub(super) const BTN_EAST: u16 = 305; // B pub(super) const BTN_NORTH: u16 = 307; // Y pub(super) const BTN_WEST: u16 = 308; // X pub(super) const BTN_TL: u16 = 310; // LB (Rétrograder) pub(super) const BTN_TR: u16 = 311; // RB (Monter un rapport) pub(super) const BTN_SELECT: u16 = 314; // View / Back pub(super) const BTN_START: u16 = 315; // Menu / Start (Armement / Désarmement) pub(super) const BTN_MODE: u16 = 316; // Bouton Steam pub(super) const BTN_THUMBR: u16 = 318; // R3 pub(super) const BTN_DPAD_UP: u16 = 544; pub(super) const BTN_DPAD_DOWN: u16 = 545; pub(super) const BTN_DPAD_LEFT: u16 = 546; pub(super) const BTN_DPAD_RIGHT: u16 = 547; pub(super) const BTN_GRIPL: u16 = 548; // Palette arrière gauche L4 (Rétrograder) pub(super) const BTN_GRIPR: u16 = 549; // Palette arrière droite R4 (Monter un rapport) pub(super) const BTN_GRIPL2: u16 = 550; // Palette arrière basse gauche L5 (Embrayage) pub(super) const INPUT_PROP_ACCELEROMETER: usize = 6; pub(super) const VALVE_VENDOR_ID: u16 = 0x28de; pub(super) const O_NONBLOCK_FLAG: i32 = 0x800; // O_NONBLOCK sur Linux x86_64 et aarch64 // Plages de gaz progressives identiques au firmware RP2350 (g29_crsf.ino) pub(super) const GAZ_AVANT_MIN: u16 = 1065; pub(super) const GAZ_AVANT_MAX_PAR_VITESSE: [u16; 3] = [ 1100, // 1ère vitesse (Manœuvre) 1150, // 2ème vitesse (Normale) 1220, // 3ème vitesse (Rapide) ]; pub(super) const GAZ_ARRIERE_MIN: u16 = 905; pub(super) 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, } } } pub(super) const TOAST_DURATION_MS: u64 = 1500; pub(super) const RC_SEND_INTERVAL_MS: u64 = 10; // 100 Hz vers RP2350 pub(super) const HAPTICS_INTERVAL_MS: u64 = 40; // 25 Hz (identique au G29) pub(super) const SRC_RESYNC_INTERVAL_MS: u64 = 500; pub(super) const GEARS_RESYNC_INTERVAL_MS: u64 = 2000; pub(super) const DEVICE_RESCAN_INTERVAL_MS: u64 = 1000; pub(super) const IBEX_HIDRAW_TIMEOUT_MS: u64 = 300; pub(super) const IBEX_STALE_RC_STOP_MS: u64 = 120; pub(super) const CONSECUTIVE_SEND_ERR_DISARM: u32 = 10; pub(super) const IBEX_REPORT_ID: u8 = 0x42; pub(super) const IBEX_REPORT_LEN: usize = 54; pub(super) const IBEX_RUMBLE_REPORT_ID: u8 = 0x80; extern "C" { pub(super) fn ioctl(fd: std::ffi::c_int, request: std::ffi::c_ulong, ...) -> std::ffi::c_int; } #[repr(C)] #[derive(Clone, Copy, Default)] pub(super) struct LinuxInputEvent { pub(super) tv_sec: std::ffi::c_long, pub(super) tv_usec: std::ffi::c_long, pub(super) ev_type: u16, pub(super) code: u16, pub(super) value: i32, } #[repr(C)] #[derive(Clone, Copy, Default, Debug)] pub(super) struct LinuxInputAbsInfo { pub(super) value: i32, pub(super) minimum: i32, pub(super) maximum: i32, pub(super) fuzz: i32, pub(super) flat: i32, pub(super) resolution: i32, } pub(super) fn eviocgabs(abs_code: u16) -> std::ffi::c_ulong { // _IOR('E', 0x40 + abs_code, struct input_absinfo) = 0x8018_4540 + abs_code (0x8018_4540u64 + abs_code as u64) as std::ffi::c_ulong } // ========================================================================= // TYPES PUBLICS : SOURCE DE CONTRÔLE & ÉTAT STEAM CONTROLLER // ========================================================================= #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] #[repr(u8)] pub enum ControlSource { #[default] G29 = 0, SteamController = 1, } impl ControlSource { pub fn from_str(s: &str) -> Self { let lower = s.trim().to_ascii_lowercase(); if lower == "steam" || lower == "steam_controller" || lower == "steam-controller" || lower == "controller" || lower == "gamepad" || lower == "ext" { Self::SteamController } else { Self::G29 } } pub fn from_u8(v: u8) -> Self { match v { 1 => Self::SteamController, _ => Self::G29, } } pub fn as_u8(self) -> u8 { self as u8 } pub fn as_str(self) -> &'static str { match self { Self::G29 => "g29", Self::SteamController => "steam", } } pub fn wire_tag(self) -> &'static str { match self { Self::G29 => "G29", Self::SteamController => "EXT", } } pub fn label(self) -> &'static str { match self { Self::G29 => "Volant G29", Self::SteamController => "Steam Controller", } } } #[derive(Debug, Clone)] pub struct SteamControllerConfig { pub device_override: Option, pub steering_deadzone: f32, pub steering_expo: f32, pub trigger_deadzone: f32, #[allow(dead_code)] pub invert_steering: bool, } impl Default for SteamControllerConfig { fn default() -> Self { Self { device_override: None, steering_deadzone: 0.08, steering_expo: 0.25, trigger_deadzone: 0.05, invert_steering: false, } } } #[allow(dead_code)] #[derive(Debug, Clone)] pub struct SteamControllerState { pub connected: bool, pub device_name: String, pub device_path: String, pub armed: bool, pub gear: i8, pub steering_norm: f32, 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 vibration_pct: u8, pub rumble_left_pct: u8, pub rumble_right_pct: u8, 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, } impl Default for SteamControllerState { fn default() -> Self { Self { connected: false, device_name: String::new(), device_path: String::new(), armed: false, gear: 0, steering_norm: 0.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, vibration_pct: 100, rumble_left_pct: 0, rumble_right_pct: 0, packets_sent: 0, send_errors: 0, last_button: String::new(), last_button_at: None, toast_text: String::new(), toast_until: None, } } } impl SteamControllerState { pub fn gear_label(&self) -> &'static str { match self.gear { -1 => "R", 0 => "N", 1 => "1", 2 => "2", 3 => "3", _ => "?", } } pub fn set_toast(&mut self, msg: impl Into) { self.toast_text = msg.into(); self.toast_until = Some(Instant::now() + Duration::from_millis(TOAST_DURATION_MS)); } #[allow(dead_code)] pub fn active_toast(&self) -> Option<&str> { match self.toast_until { Some(until) if Instant::now() < until && !self.toast_text.is_empty() => { Some(&self.toast_text) } _ => None, } } /// Construit un état compatible OSD pour afficher le HUD Steam Controller sans allocation supplémentaire. pub fn to_hud_g29_state(&self, hub_failsafe: bool) -> G29State { G29State { connected: self.connected, 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: self.rumble_left_pct.max(self.rumble_right_pct), remote_source: ControlSource::SteamController, packets_received: self.packets_sent.max(u64::from(self.connected)), last_packet: if self.connected { Some(Instant::now()) } 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), } }