From 68ee62fef1c02ceab0fad42899b8fd3ff1fa4161 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Fri, 16 Jun 2023 11:50:38 +1200 Subject: [PATCH] Anime: add base brightness control (dbus, cli) --- asusctl/src/anime_cli.rs | 10 ++++++-- asusctl/src/main.rs | 4 ++++ daemon/src/ctrl_anime/trait_impls.rs | 19 ++++++++++------ rog-anime/src/usb.rs | 34 ++++++++++++++++++++++++++-- rog-dbus/src/zbus_anime.rs | 6 ++++- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/asusctl/src/anime_cli.rs b/asusctl/src/anime_cli.rs index 63968052..3b2cf5c6 100644 --- a/asusctl/src/anime_cli.rs +++ b/asusctl/src/anime_cli.rs @@ -1,4 +1,5 @@ use gumdrop::Options; +use rog_anime::usb::Brightness; #[derive(Options)] pub struct AnimeCommand { @@ -14,8 +15,13 @@ pub struct AnimeCommand { help = "enable/disable system animations (boot/sleep/shutdown)" )] pub boot_enable: Option, - #[options(meta = "", help = "set global AniMe brightness value")] - pub brightness: Option, + #[options( + meta = "", + help = "set global base brightness value " + )] + pub brightness: Option, + #[options(meta = "", help = "set global (image) brightness value")] + pub image_brightness: Option, #[options(help = "clear the display")] pub clear: bool, #[options(command)] diff --git a/asusctl/src/main.rs b/asusctl/src/main.rs index 4dd3ed26..cd91ebb6 100644 --- a/asusctl/src/main.rs +++ b/asusctl/src/main.rs @@ -226,6 +226,7 @@ fn handle_anime( && cmd.enable.is_none() && cmd.boot_enable.is_none() && cmd.brightness.is_none() + && cmd.image_brightness.is_none() && !cmd.clear) || cmd.help { @@ -241,6 +242,9 @@ fn handle_anime( dbus.proxies().anime().set_animation_enabled(anime_boot)?; } if let Some(bright) = cmd.brightness { + dbus.proxies().anime().set_brightness(bright)?; + } + if let Some(bright) = cmd.image_brightness { verify_brightness(bright); dbus.proxies().anime().set_image_brightness(bright)?; } diff --git a/daemon/src/ctrl_anime/trait_impls.rs b/daemon/src/ctrl_anime/trait_impls.rs index df9dd0f3..d11f69ce 100644 --- a/daemon/src/ctrl_anime/trait_impls.rs +++ b/daemon/src/ctrl_anime/trait_impls.rs @@ -6,6 +6,7 @@ use config_traits::StdConfig; use log::warn; use rog_anime::usb::{ pkt_for_enable_animation, pkt_for_set_awake_enabled, pkt_for_set_boot, pkt_for_set_brightness, + Brightness, }; use rog_anime::{AnimeDataBuffer, AnimePowerStates}; use zbus::export::futures_util::lock::Mutex; @@ -59,16 +60,19 @@ impl CtrlAnimeZbus { /// Set base brightness level // TODO: enum for brightness - async fn set_brightness(&self, #[zbus(signal_context)] ctxt: SignalContext<'_>, status: bool) { - let mut lock = self.0.lock().await; + async fn set_brightness( + &self, + #[zbus(signal_context)] ctxt: SignalContext<'_>, + brightness: Brightness, + ) { + let lock = self.0.lock().await; lock.node - .write_bytes(&pkt_for_set_brightness(status)) + .write_bytes(&pkt_for_set_brightness(brightness)) .map_err(|err| { warn!("rog_anime::run_animation:callback {}", err); }) .ok(); - lock.config.awake_enabled = status; - lock.config.write(); + // lock.config.write(); Self::notify_power_states( &ctxt, @@ -232,8 +236,9 @@ impl crate::CtrlTask for CtrlAnimeZbus { impl crate::Reloadable for CtrlAnimeZbus { async fn reload(&mut self) -> Result<(), RogError> { if let Some(lock) = self.0.try_lock() { - lock.node - .write_bytes(&pkt_for_set_brightness(lock.config.awake_enabled))?; + // TODO: restore new settings + // lock.node + // .write_bytes(&pkt_for_set_brightness(lock.config.awake_enabled))?; lock.node .write_bytes(&pkt_for_set_boot(lock.config.boot_anim_enabled))?; diff --git a/rog-anime/src/usb.rs b/rog-anime/src/usb.rs index d611916b..57a26763 100644 --- a/rog-anime/src/usb.rs +++ b/rog-anime/src/usb.rs @@ -8,6 +8,12 @@ //! //! Step 1 need to applied only on fresh system boot. +use std::str::FromStr; + +use serde_derive::{Deserialize, Serialize}; +#[cfg(feature = "dbus")] +use zbus::zvariant::Type; + use crate::error::AnimeError; use crate::AnimeType; @@ -16,6 +22,30 @@ const DEV_PAGE: u8 = 0x5e; pub const VENDOR_ID: u16 = 0x0b05; pub const PROD_ID: u16 = 0x193b; +#[cfg_attr(feature = "dbus", derive(Type))] +#[derive(Debug, PartialEq, Eq, Copy, Clone, Deserialize, Serialize)] +/// Base LED brightness of the display +pub enum Brightness { + Off, + Low, + Med, + High, +} + +impl FromStr for Brightness { + type Err = AnimeError; + + fn from_str(s: &str) -> Result { + Ok(match s { + "Off" | "off" => Brightness::Off, + "Low" | "low" => Brightness::Low, + "Med" | "med" => Brightness::Med, + "High" | "high" => Brightness::High, + _ => Brightness::Med, + }) + } +} + /// `get_anime_type` is very broad, matching on part of the laptop board name /// only. For this reason `find_node()` must be used also to verify if the USB /// device is available. @@ -83,12 +113,12 @@ pub const fn pkt_for_set_boot(status: bool) -> [u8; PACKET_SIZE] { /// `pkt_for_apply()` to be written after. // TODO: change the users of this method #[inline] -pub const fn pkt_for_set_brightness(on: bool) -> [u8; PACKET_SIZE] { +pub const fn pkt_for_set_brightness(brightness: Brightness) -> [u8; PACKET_SIZE] { let mut pkt = [0; PACKET_SIZE]; pkt[0] = DEV_PAGE; pkt[1] = 0xc0; pkt[2] = 0x04; - pkt[3] = if on { 0x03 } else { 0x00 }; + pkt[3] = brightness as u8; pkt } diff --git a/rog-dbus/src/zbus_anime.rs b/rog-dbus/src/zbus_anime.rs index b12da9d9..43eadcd2 100644 --- a/rog-dbus/src/zbus_anime.rs +++ b/rog-dbus/src/zbus_anime.rs @@ -1,3 +1,4 @@ +use rog_anime::usb::Brightness; use rog_anime::{AnimeDataBuffer, AnimePowerStates}; use zbus::dbus_proxy; @@ -9,7 +10,10 @@ trait Anime { /// Set whether the AniMe will show boot, suspend, or off animations fn set_animation_enabled(&self, status: bool) -> zbus::Result<()>; - /// Set the global AniMe brightness + /// Set the global base brightness + fn set_brightness(&self, bright: Brightness) -> zbus::Result<()>; + + /// Set the global (image) brightness fn set_image_brightness(&self, bright: f32) -> zbus::Result<()>; /// Set whether the AniMe is displaying images/data