Anime: add base brightness control (dbus, cli)

This commit is contained in:
Luke D. Jones
2023-06-16 11:50:38 +12:00
parent e523e4e9a2
commit 68ee62fef1
5 changed files with 61 additions and 12 deletions

View File

@@ -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<bool>,
#[options(meta = "", help = "set global AniMe brightness value")]
pub brightness: Option<f32>,
#[options(
meta = "",
help = "set global base brightness value <Off, Low, Med, High>"
)]
pub brightness: Option<Brightness>,
#[options(meta = "", help = "set global (image) brightness value")]
pub image_brightness: Option<f32>,
#[options(help = "clear the display")]
pub clear: bool,
#[options(command)]

View File

@@ -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)?;
}

View File

@@ -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))?;

View File

@@ -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<Self, Self::Err> {
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
}

View File

@@ -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