From 3bdc11c994cfe55201b3f98c07a034e935f8bbd0 Mon Sep 17 00:00:00 2001 From: Asere Date: Tue, 27 Oct 2020 13:50:10 +0100 Subject: [PATCH] Better anime options & gumdrop error Display --- asus-nb-ctrl/src/ctrl_anime.rs | 47 ++++++++++++++++++++++------------ asus-nb-ctrl/src/main.rs | 29 +++++++++++++-------- asus-nb/src/anime_dbus.rs | 24 +++++++++-------- asus-nb/src/cli_options.rs | 32 ++++++++++++++++++++++- asus-nb/src/dbus_anime.rs | 5 ++++ 5 files changed, 99 insertions(+), 38 deletions(-) diff --git a/asus-nb-ctrl/src/ctrl_anime.rs b/asus-nb-ctrl/src/ctrl_anime.rs index 9aa36d02..f9c520d2 100644 --- a/asus-nb-ctrl/src/ctrl_anime.rs +++ b/asus-nb-ctrl/src/ctrl_anime.rs @@ -6,8 +6,8 @@ const DEV_PAGE: u8 = 0x5e; // These bytes are in [1] position of the array const WRITE: u8 = 0xc0; const INIT: u8 = 0xc2; -const APPLY: u8 = 0xc3; -const SET: u8 = 0xc4; +const SET: u8 = 0xc3; +const APPLY: u8 = 0xc4; // Used to turn the panel on and off // The next byte can be 0x03 for "on" and 0x00 for "off" @@ -25,7 +25,7 @@ use zbus::dbus_interface; #[derive(Debug)] pub enum AnimatrixCommand { Apply, - Set, + SetBoot(bool), Write(Vec), WriteImage(Vec>), //ReloadLast, @@ -41,6 +41,8 @@ pub trait Dbus { fn set_anime(&mut self, input: Vec>); fn set_on_off(&mut self, status: bool); + + fn set_boot_on_off(&mut self, status: bool); } impl crate::ZbusAdd for CtrlAnimeDisplay { @@ -58,28 +60,40 @@ impl crate::ZbusAdd for CtrlAnimeDisplay { #[dbus_interface(name = "org.asuslinux.Daemon")] impl Dbus for CtrlAnimeDisplay { fn set_anime(&mut self, input: Vec>) { + self.do_command(AnimatrixCommand::WriteImage(input)) - .unwrap_or_else(|err| warn!("{}", err)); + .map_or_else(|err| warn!("{}", err), + |()| info!("Writing image to Anime")); } fn set_on_off(&mut self, status: bool) { - let mut activity : Vec = vec![0; PACKET_SIZE]; - activity[0] = DEV_PAGE; - activity[1] = WRITE; - activity[2] = ON_OFF; + let mut flush : Vec = vec![0; PACKET_SIZE]; + flush[0] = DEV_PAGE; + flush[1] = WRITE; + flush[2] = ON_OFF; let status_str; if status { - activity[3] = 0x03; + flush[3] = 0x03; status_str = "on"; } else { - activity[3] = 0x00; + flush[3] = 0x00; status_str = "off"; } - info!("Turning {} the AniMe", status_str); - self.do_command(AnimatrixCommand::Write(activity)) - .unwrap_or_else(|err| warn!("{}", err)); + self.do_command(AnimatrixCommand::Write(flush)) + .map_or_else(|err| warn!("{}", err), + |()| info!("Turning {} the AniMe", status_str)); + } + + fn set_boot_on_off(&mut self, status: bool) { + let status_str = if status { "on" } else { "off" }; + + self.do_command(AnimatrixCommand::SetBoot(status)) + .and_then(|()| self.do_command(AnimatrixCommand::Apply)) + .map_or_else(|err| warn!("{}", err), + |()| info!("Turning {} the AniMe at boot/shutdown", + status_str)); } } @@ -127,7 +141,8 @@ impl CtrlAnimeDisplay { match command { AnimatrixCommand::Apply => self.do_apply()?, - AnimatrixCommand::Set => self.do_set()?, + //AnimatrixCommand::Set => self.do_set_boot()?, + AnimatrixCommand::SetBoot(status) => self.do_set_boot(status)?, AnimatrixCommand::Write(bytes) => self.write_bytes(&bytes)?, AnimatrixCommand::WriteImage(effect) => self.write_image(effect)?, //AnimatrixCommand::ReloadLast => self.reload_last_builtin(&config).await?, @@ -212,12 +227,12 @@ impl CtrlAnimeDisplay { } #[inline] - fn do_set(&mut self) -> Result<(), AuraError> { + fn do_set_boot(&mut self, status: bool) -> Result<(), AuraError> { let mut flush = [0; PACKET_SIZE]; flush[0] = DEV_PAGE; flush[1] = SET; flush[2] = 0x01; - flush[3] = 0x80; + flush[3] = if status { 0x00 } else { 0x80 }; self.write_bytes(&flush)?; Ok(()) diff --git a/asus-nb-ctrl/src/main.rs b/asus-nb-ctrl/src/main.rs index 5ce75d59..dbee00d6 100644 --- a/asus-nb-ctrl/src/main.rs +++ b/asus-nb-ctrl/src/main.rs @@ -1,6 +1,11 @@ use asus_nb::{ anime_dbus::AniMeDbusWriter, - cli_options::{AniMeActions, LedBrightness, SetAuraBuiltin}, + cli_options::{ + AniMeActions, + AniMeStatusValue, + LedBrightness, + SetAuraBuiltin, + }, core_dbus::AuraDbusClient, profile::{ProfileCommand, ProfileEvent}, }; @@ -70,10 +75,10 @@ struct GraphicsCommand { struct AniMeCommand { #[options(help = "print help message")] help: bool, - #[options(help = "turn on the panel (and accept write requests)", no_short)] - on: bool, - #[options(help = "turn off the panel (and reject write requests)", no_short)] - off: bool, + #[options(help = "turn on/off the panel (accept/reject write requests)")] + turn: Option, + #[options(help = "turn on/off the panel at boot (with Asus effect)")] + boot: Option, #[options(command)] command: Option, } @@ -102,7 +107,7 @@ fn main() -> Result<(), Box> { }; } Err(err) => { - eprintln!("source {:?}", err); + eprintln!("source {}", err); std::process::exit(2); } } @@ -142,11 +147,13 @@ fn main() -> Result<(), Box> { } Some(CliCommand::Graphics(command)) => do_gfx(command, &writer)?, Some(CliCommand::AniMe(anime)) => { - if anime.on { - anime_writer.turn_on()?; - } else if anime.off { - anime_writer.turn_off()?; - } else if let Some(action) = anime.command { + if let Some(anime_turn) = anime.turn { + anime_writer.turn_on_off(anime_turn.into())? + } + if let Some(anime_boot) = anime.boot { + anime_writer.turn_boot_on_off(anime_boot.into())? + } + if let Some(action) = anime.command { match action { AniMeActions::Leds(anime_leds) => { let led_brightness = anime_leds.led_brightness(); diff --git a/asus-nb/src/anime_dbus.rs b/asus-nb/src/anime_dbus.rs index e1534404..1358c2f5 100644 --- a/asus-nb/src/anime_dbus.rs +++ b/asus-nb/src/anime_dbus.rs @@ -46,6 +46,10 @@ impl AniMeDbusWriter { ) } + fn thread_sleep(&self) { + thread::sleep(Duration::from_millis(self.block_time)); + } + pub fn write_image_to_buf(_buf: &mut AniMePacketType, _image_data: &[u8]) { unimplemented!("Image format is in progress of being worked out") } @@ -74,7 +78,7 @@ impl AniMeDbusWriter { image[1][..7].copy_from_slice(&ANIME_PANE2_PREFIX); proxy.set_anime(vec![image[0].to_vec(), image[1].to_vec()])?; - thread::sleep(Duration::from_millis(self.block_time)); + self.thread_sleep(); Ok(()) } @@ -90,24 +94,24 @@ impl AniMeDbusWriter { Ok(()) } - fn turn_on_off(&self, status : bool) -> Result<(), Box> { + #[inline] + pub fn turn_on_off(&self, status: bool) -> Result<(), Box> { let proxy = self.new_proxy(); proxy.set_on_off(status)?; - thread::sleep(Duration::from_millis(self.block_time)); + self.thread_sleep(); Ok(()) } #[inline] - pub fn turn_on(&self) -> Result<(), Box> { - self.turn_on_off(true)?; - Ok(()) - } + pub fn turn_boot_on_off(&self, status: bool) + -> Result<(), Box> { + let proxy = self.new_proxy(); + + proxy.set_boot_on_off(status)?; + self.thread_sleep(); - #[inline] - pub fn turn_off(&self) -> Result<(), Box> { - self.turn_on_off(false)?; Ok(()) } } diff --git a/asus-nb/src/cli_options.rs b/asus-nb/src/cli_options.rs index a458e298..9b7a4fdd 100644 --- a/asus-nb/src/cli_options.rs +++ b/asus-nb/src/cli_options.rs @@ -231,6 +231,37 @@ impl Default for SetAuraBuiltin { } } +#[derive(Copy, Clone, Debug)] +pub enum AniMeStatusValue { + On, + Off, +} +impl FromStr for AniMeStatusValue { + type Err = AuraError; + + fn from_str(s: &str) -> Result { + let s = s.to_lowercase(); + match s.as_str() { + "on" => Ok(AniMeStatusValue::On), + "off" => Ok(AniMeStatusValue::Off), + _ => { + print!("{}\n{}\n", + "Invalid argument, must be one of:", + "on, off"); + Err(AuraError::ParseAnime) + } + } + } +} +impl From for bool { + fn from(value: AniMeStatusValue) -> Self { + match value { + AniMeStatusValue::On => true, + AniMeStatusValue::Off => false, + } + } +} + #[derive(Options)] pub struct AniMeLeds { #[options(help = "print help message")] @@ -240,7 +271,6 @@ pub struct AniMeLeds { help = "set all leds brightness value")] led_brightness: u8, } - impl AniMeLeds { pub fn led_brightness(&self) -> u8 { self.led_brightness diff --git a/asus-nb/src/dbus_anime.rs b/asus-nb/src/dbus_anime.rs index 0fe3b015..0c200d48 100644 --- a/asus-nb/src/dbus_anime.rs +++ b/asus-nb/src/dbus_anime.rs @@ -7,6 +7,7 @@ use dbus::blocking; pub trait OrgAsuslinuxDaemon { fn set_anime(&self, input: Vec>) -> Result<(), dbus::Error>; fn set_on_off(&self, status: bool) -> Result<(), dbus::Error>; + fn set_boot_on_off(&self, status: bool) -> Result<(), dbus::Error>; } impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref> OrgAsuslinuxDaemon for blocking::Proxy<'a, C> { @@ -18,4 +19,8 @@ impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref> OrgAsuslin fn set_on_off(&self, status: bool) -> Result<(), dbus::Error> { self.method_call("org.asuslinux.Daemon", "SetOnOff", (status, )) } + + fn set_boot_on_off(&self, status: bool) -> Result<(), dbus::Error> { + self.method_call("org.asuslinux.Daemon", "SetBootOnOff", (status, )) + } }