From 36ce024665d5b69bc4bd15ce62f671496b8343d7 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 5 Dec 2022 19:47:32 -0500 Subject: [PATCH] Improve the thing --- backend/src/settings/driver.rs | 12 +----------- backend/src/settings/steam_deck/mod.rs | 2 +- backend/src/settings/steam_deck/util.rs | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/backend/src/settings/driver.rs b/backend/src/settings/driver.rs index 7a2e498..b637da4 100644 --- a/backend/src/settings/driver.rs +++ b/backend/src/settings/driver.rs @@ -80,17 +80,7 @@ impl Driver { pub fn maybe_do_button() { match super::auto_detect_provider() { DriverJson::SteamDeck | DriverJson::SteamDeckAdvance => { - let period = std::time::Duration::from_millis(500); - for _ in 0..10 { - if let Err(e) = crate::settings::steam_deck::set_led(false, true, false) { - log::error!("Thing err: {}", e); - } - std::thread::sleep(period); - if let Err(e) = crate::settings::steam_deck::set_led(false, false, false) { - log::error!("Thing err: {}", e); - }; - std::thread::sleep(period); - } + crate::settings::steam_deck::flash_led(); }, DriverJson::Generic => log::warn!("You need to come up with something fun on generic"), DriverJson::Unknown => log::warn!("Can't do button activities on unknown platform"), diff --git a/backend/src/settings/steam_deck/mod.rs b/backend/src/settings/steam_deck/mod.rs index a49088e..fb836a1 100644 --- a/backend/src/settings/steam_deck/mod.rs +++ b/backend/src/settings/steam_deck/mod.rs @@ -7,4 +7,4 @@ pub use battery::Battery; pub use cpu::{Cpu, Cpus}; pub use gpu::Gpu; -pub use util::set_led; +pub use util::flash_led; diff --git a/backend/src/settings/steam_deck/util.rs b/backend/src/settings/steam_deck/util.rs index a8cd99e..1d89d1c 100644 --- a/backend/src/settings/steam_deck/util.rs +++ b/backend/src/settings/steam_deck/util.rs @@ -60,11 +60,34 @@ fn wait_ready_for_read() -> Result<(), Error> { } pub fn set_led(red_unused: bool, green_aka_white: bool, blue_unused: bool) -> Result { - let payload: u8 = 0x80 + (red_unused as u8 & 1) + ((green_aka_white as u8 & 1) << 1) + ((blue_unused as u8 & 1) << 2); + let payload: u8 = 0x80 | (red_unused as u8 & 1) | ((green_aka_white as u8 & 1) << 1) | ((blue_unused as u8 & 1) << 2); //log::info!("Payload: {:b}", payload); write2(Setting::LEDStatus as _, payload) } +const THINGS: &[u8] = &[ + 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, +]; + +const TIME_UNIT: std::time::Duration = std::time::Duration::from_millis(200); + +pub fn flash_led() { + let old_led_state = write_read(Setting::LEDStatus as _).map_err(|e| log::error!("Failed to read LED status: {}", e)); + for &code in THINGS { + let on = code != 0; + if let Err(e) = set_led(on, on, false) { + log::error!("Thing err: {}", e); + } + std::thread::sleep(TIME_UNIT); + } + if let Ok(old_led_state) = old_led_state { + log::debug!("Restoring LED state to {:#02b}", old_led_state); + write2(Setting::LEDStatus as _, old_led_state).map_err(|e| log::error!("Failed to restore LED status: {}", e)).unwrap(); + } +} + pub fn set(setting: Setting, mode: u8) -> Result { write2(setting as u8, mode) }