diff --git a/rp2040/src/hardware.rs b/rp2040/src/hardware.rs index 713a440..6451938 100644 --- a/rp2040/src/hardware.rs +++ b/rp2040/src/hardware.rs @@ -85,7 +85,7 @@ pub mod i2c { /// Cadences for periodic firmware tasks. pub mod timers { /// Status LED update interval (ms). - pub const STATUS_LED_INTERVAL_MS: u32 = 250; + pub const STATUS_LED_INTERVAL_MS: u32 = 40; /// Button matrix scan interval (µs). pub const SCAN_INTERVAL_US: u32 = 200; @@ -97,7 +97,7 @@ pub mod timers { pub const USB_UPDATE_INTERVAL_MS: u32 = 10; /// USB activity timeout (ms) - stop sending reports after this period of inactivity. - pub const USB_ACTIVITY_TIMEOUT_MS: u32 = 10_000; // 10 seconds + pub const USB_ACTIVITY_TIMEOUT_MS: u32 = 5_000; // 5 seconds } // ==================== USB DEVICE CONFIGURATION ==================== diff --git a/rp2040/src/main.rs b/rp2040/src/main.rs index b523ac6..4d81399 100644 --- a/rp2040/src/main.rs +++ b/rp2040/src/main.rs @@ -232,6 +232,7 @@ fn main() -> ! { let mut usb_activity: bool = false; let mut usb_active: bool = false; + let mut usb_initialized: bool = false; let mut vt_enable: bool = false; let mut idle_mode: bool = false; let mut usb_activity_timeout_count: u32 = 0; @@ -287,6 +288,9 @@ fn main() -> ! { // Handle USB device polling and maintain connection state if usb_dev.poll(&mut [&mut usb_hid_joystick]) { + if !usb_initialized { + usb_initialized = true; + } if !usb_active { usb_activity = true; // Force initial report idle_mode = false; @@ -336,6 +340,7 @@ fn main() -> ! { let system_state = SystemState { usb_active, + usb_initialized, idle_mode, calibration_active: calibration_manager.is_active(), throttle_hold_enable: axis_manager.throttle_hold_enable, @@ -494,8 +499,6 @@ fn main() -> ! { } } else if usb_tick && usb_active { idle_mode = true; - } else if usb_tick { - idle_mode = false; } } } diff --git a/rp2040/src/status.rs b/rp2040/src/status.rs index ae81bb3..d9e675d 100644 --- a/rp2040/src/status.rs +++ b/rp2040/src/status.rs @@ -11,6 +11,13 @@ use rp2040_hal::{ use smart_leds::{SmartLedsWrite, RGB8}; use ws2812_pio::Ws2812Direct; +const COLOR_OFF: RGB8 = RGB8 { r: 0, g: 0, b: 0 }; +const COLOR_GREEN: RGB8 = RGB8 { r: 10, g: 7, b: 0 }; +const COLOR_BLUE: RGB8 = RGB8 { r: 10, g: 4, b: 10 }; +const COLOR_ORANGE: RGB8 = RGB8 { r: 5, g: 10, b: 0 }; +const COLOR_RED: RGB8 = RGB8 { r: 20, g: 0, b: 0 }; +const COLOR_PURPLE: RGB8 = RGB8 { r: 0, g: 10, b: 10 }; + /// Status LED modes with clear semantics. #[allow(dead_code)] #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -26,48 +33,185 @@ pub enum StatusMode { Warning = 8, Error = 9, Bootloader = 10, + Power = 11, } /// Aggregate system state for LED status indication. #[derive(Clone, Copy)] pub struct SystemState { pub usb_active: bool, + pub usb_initialized: bool, pub idle_mode: bool, pub calibration_active: bool, pub throttle_hold_enable: bool, pub vt_enable: bool, } -/// Color definitions for different status modes. -const LED_COLORS: [RGB8; 11] = [ - RGB8 { r: 0, g: 0, b: 0 }, // Off - RGB8 { r: 10, g: 7, b: 0 }, // Normal (Green) - RGB8 { r: 10, g: 7, b: 0 }, // NormalFlash (Green) - RGB8 { r: 10, g: 4, b: 10 }, // Activity (Blue) - RGB8 { r: 10, g: 4, b: 10 }, // ActivityFlash (Blue) - RGB8 { r: 10, g: 7, b: 0 }, // Idle (Green flash) - RGB8 { r: 5, g: 10, b: 0 }, // Other (Orange) - RGB8 { r: 5, g: 10, b: 0 }, // OtherFlash (Orange) - RGB8 { r: 2, g: 20, b: 0 }, // Warning (Red) - RGB8 { r: 2, g: 20, b: 0 }, // Error (Red) - RGB8 { r: 0, g: 10, b: 10 }, // Bootloader (Purple) -]; +#[derive(Copy, Clone)] +enum LedEffect { + Solid, + Blink { period_ms: u32 }, + Heartbeat { period_ms: u32 }, +} -fn determine_mode(system_state: SystemState) -> StatusMode { +#[derive(Copy, Clone)] +struct ModeDescriptor { + color: RGB8, + effect: LedEffect, +} + +const HEARTBEAT_POWER_MS: u32 = 800; +const HEARTBEAT_IDLE_MS: u32 = 3200; + +impl LedEffect { + fn update_interval_ms(self) -> u32 { + match self { + LedEffect::Solid => 0, + LedEffect::Blink { period_ms } => period_ms / 2, + LedEffect::Heartbeat { .. } => 40, + } + } + + fn color_for(self, base: RGB8, elapsed_ms: u32) -> RGB8 { + match self { + LedEffect::Solid => base, + LedEffect::Blink { period_ms } => { + if period_ms == 0 { + return base; + } + let half = (period_ms / 2).max(1); + if elapsed_ms % period_ms < half { + base + } else { + RGB8 { r: 0, g: 0, b: 0 } + } + } + LedEffect::Heartbeat { period_ms } => { + let period = period_ms.max(1); + let phase = elapsed_ms % period; + let half = (period / 2).max(1); + let ramp = if phase < half { + ((phase * 255) / half) as u8 + } else { + (((period - phase) * 255) / half) as u8 + }; + scale_color(base, ramp) + } + } + } +} + +const fn descriptor_for(mode: StatusMode, base_mode: StatusMode) -> ModeDescriptor { + match mode { + StatusMode::Off => ModeDescriptor { + color: COLOR_OFF, + effect: LedEffect::Solid, + }, + StatusMode::Normal => ModeDescriptor { + color: COLOR_GREEN, + effect: LedEffect::Solid, + }, + StatusMode::NormalFlash => ModeDescriptor { + color: COLOR_GREEN, + effect: LedEffect::Blink { period_ms: 1000 }, + }, + StatusMode::Activity => ModeDescriptor { + color: COLOR_BLUE, + effect: LedEffect::Solid, + }, + StatusMode::ActivityFlash => ModeDescriptor { + color: COLOR_BLUE, + effect: LedEffect::Blink { period_ms: 600 }, + }, + StatusMode::Idle => match base_mode { + StatusMode::Activity | StatusMode::ActivityFlash => ModeDescriptor { + color: COLOR_BLUE, + effect: LedEffect::Heartbeat { + period_ms: HEARTBEAT_IDLE_MS, + }, + }, + StatusMode::Other | StatusMode::OtherFlash => ModeDescriptor { + color: COLOR_ORANGE, + effect: LedEffect::Heartbeat { + period_ms: HEARTBEAT_IDLE_MS, + }, + }, + StatusMode::NormalFlash | StatusMode::Normal => ModeDescriptor { + color: COLOR_GREEN, + effect: LedEffect::Heartbeat { + period_ms: HEARTBEAT_IDLE_MS, + }, + }, + StatusMode::Warning => ModeDescriptor { + color: COLOR_RED, + effect: LedEffect::Heartbeat { + period_ms: HEARTBEAT_IDLE_MS, + }, + }, + StatusMode::Error => ModeDescriptor { + color: COLOR_RED, + effect: LedEffect::Heartbeat { + period_ms: HEARTBEAT_IDLE_MS, + }, + }, + _ => ModeDescriptor { + color: COLOR_GREEN, + effect: LedEffect::Heartbeat { + period_ms: HEARTBEAT_IDLE_MS, + }, + }, + }, + StatusMode::Other => ModeDescriptor { + color: COLOR_ORANGE, + effect: LedEffect::Solid, + }, + StatusMode::OtherFlash => ModeDescriptor { + color: COLOR_ORANGE, + effect: LedEffect::Blink { period_ms: 600 }, + }, + StatusMode::Warning => ModeDescriptor { + color: COLOR_RED, + effect: LedEffect::Blink { period_ms: 500 }, + }, + StatusMode::Error => ModeDescriptor { + color: COLOR_RED, + effect: LedEffect::Solid, + }, + StatusMode::Bootloader => ModeDescriptor { + color: COLOR_PURPLE, + effect: LedEffect::Solid, + }, + StatusMode::Power => ModeDescriptor { + color: COLOR_GREEN, + effect: LedEffect::Heartbeat { + period_ms: HEARTBEAT_POWER_MS, + }, + }, + } +} + +fn scale_color(base: RGB8, brightness: u8) -> RGB8 { + let scale = brightness as u16; + RGB8 { + r: ((base.r as u16 * scale) / 255) as u8, + g: ((base.g as u16 * scale) / 255) as u8, + b: ((base.b as u16 * scale) / 255) as u8, + } +} + +fn determine_base_mode(system_state: SystemState) -> StatusMode { if system_state.calibration_active { StatusMode::ActivityFlash - } else if system_state.idle_mode { - StatusMode::Idle + } else if !system_state.usb_initialized { + StatusMode::Power } else if !system_state.usb_active { StatusMode::NormalFlash } else if system_state.vt_enable { StatusMode::Activity } else if system_state.throttle_hold_enable { StatusMode::Other - } else if system_state.usb_active { - StatusMode::Normal } else { - StatusMode::Off + StatusMode::Normal } } @@ -80,7 +224,7 @@ where { ws2812_direct: Ws2812Direct
,
current_mode: StatusMode,
- flash_state: bool,
+ mode_started_at: Option