Rebuild of LED power control

This commit is contained in:
Luke D. Jones
2022-07-14 21:18:53 +12:00
parent 51656dc13f
commit 535e104ccc
9 changed files with 393 additions and 316 deletions

View File

@@ -1,6 +1,7 @@
use crate::laptops::LaptopLedData;
use log::{error, warn};
use rog_aura::{AuraEffect, AuraModeNum, AuraZone, LedBrightness, LedPowerStates};
use rog_aura::usb::AuraControl;
use rog_aura::{AuraEffect, AuraModeNum, AuraZone, LedBrightness};
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs::{File, OpenOptions};
@@ -15,7 +16,7 @@ pub struct AuraConfig {
pub current_mode: AuraModeNum,
pub builtins: BTreeMap<AuraModeNum, AuraEffect>,
pub multizone: Option<BTreeMap<AuraModeNum, Vec<AuraEffect>>>,
pub power_states: LedPowerStates,
pub enabled: Vec<AuraControl>,
}
impl Default for AuraConfig {
@@ -25,13 +26,20 @@ impl Default for AuraConfig {
current_mode: AuraModeNum::Static,
builtins: BTreeMap::new(),
multizone: None,
power_states: LedPowerStates {
boot_anim: true,
sleep_anim: true,
all_leds: true,
keys_leds: true,
side_leds: true,
},
enabled: vec![
AuraControl::BootLogo,
AuraControl::BootKeyb,
AuraControl::SleepLogo,
AuraControl::SleepKeyb,
AuraControl::AwakeLogo,
AuraControl::AwakeKeyb,
AuraControl::ShutdownLogo,
AuraControl::ShutdownKeyb,
AuraControl::AwakeBar,
AuraControl::BootBar,
AuraControl::SleepBar,
AuraControl::ShutdownBar,
],
}
}
}

View File

@@ -9,7 +9,7 @@ use crate::{
use async_trait::async_trait;
use log::{error, info, warn};
use logind_zbus::manager::ManagerProxy;
use rog_aura::usb::leds_message;
use rog_aura::usb::AuraControl;
use rog_aura::{
usb::{LED_APPLY, LED_SET},
AuraEffect, LedBrightness, LED_MSG_LEN,
@@ -298,17 +298,11 @@ impl CtrlKbdLed {
/// Set combination state for boot animation/sleep animation/all leds/keys leds/side leds LED active
pub(super) fn set_power_states(&self, config: &AuraConfig) -> Result<(), RogError> {
let bytes = leds_message(
config.power_states.boot_anim,
config.power_states.sleep_anim,
config.power_states.all_leds,
config.power_states.keys_leds,
config.power_states.side_leds,
);
let bytes = AuraControl::to_bytes(&config.enabled);
// Quite ugly, must be a more idiomatic way to do
let message = [
0x5d, 0xbd, 0x01, bytes[0], bytes[1], bytes[2], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x5d, 0xbd, 0x01, bytes[0], bytes[1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
self.write_bytes(&message)?;

View File

@@ -1,6 +1,6 @@
use async_trait::async_trait;
use log::warn;
use rog_aura::{AuraEffect, LedBrightness, LedPowerStates};
use rog_aura::{usb::AuraControl, AuraEffect, LedBrightness};
use zbus::{dbus_interface, Connection, SignalContext};
use super::controller::CtrlKbdLedZbus;
@@ -26,22 +26,26 @@ impl CtrlKbdLedZbus {
}
}
/// Set the keyboard LED to enabled while the device is awake
async fn set_boot_enabled(
/// Set a variety of states
async fn set_leds_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
enabled: Vec<AuraControl>,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.config.power_states.boot_anim = enabled;
for s in enabled {
if !ctrl.config.enabled.contains(&s) {
ctrl.config.enabled.push(s);
}
}
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(ctrl.config.power_states);
states = Some(ctrl.config.enabled.clone());
}
// Need to pull state out like this due to MutexGuard
if let Some(states) = states {
@@ -51,98 +55,27 @@ impl CtrlKbdLedZbus {
}
}
/// Set the keyboard LED suspend animation to enabled while the device is suspended
async fn set_sleep_enabled(
async fn set_leds_disabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
disabled: Vec<AuraControl>,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.config.power_states.sleep_anim = enabled;
for s in disabled {
if ctrl.config.enabled.contains(&s) {
if let Ok(idx) = ctrl.config.enabled.binary_search(&s) {
ctrl.config.enabled.remove(idx);
}
}
}
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(ctrl.config.power_states);
}
if let Some(states) = states {
Self::notify_power_states(&ctxt, &states)
.await
.unwrap_or_else(|err| warn!("{}", err));
}
}
/// Set all the keyboard LEDs (keys and side) to enabled
async fn set_all_leds_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.config.power_states.all_leds = enabled;
ctrl.config.power_states.keys_leds = enabled;
ctrl.config.power_states.side_leds = enabled;
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(ctrl.config.power_states);
}
// Need to pull state out like this due to MutexGuard
if let Some(states) = states {
Self::notify_power_states(&ctxt, &states)
.await
.unwrap_or_else(|err| warn!("{}", err));
}
}
/// Set the keyboard keys LEDs to enabled
async fn set_keys_leds_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.config.power_states.keys_leds = enabled;
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(ctrl.config.power_states);
}
// Need to pull state out like this due to MutexGuard
if let Some(states) = states {
Self::notify_power_states(&ctxt, &states)
.await
.unwrap_or_else(|err| warn!("{}", err));
}
}
/// Set the keyboard side LEDs to enabled
async fn set_side_leds_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.config.power_states.side_leds = enabled;
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(ctrl.config.power_states);
states = Some(ctrl.config.enabled.clone());
}
// Need to pull state out like this due to MutexGuard
if let Some(states) = states {
@@ -226,43 +159,11 @@ impl CtrlKbdLedZbus {
}
#[dbus_interface(property)]
async fn boot_enabled(&self) -> bool {
async fn leds_enabled(&self) -> Vec<u8> {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.power_states.boot_anim;
return AuraControl::to_bytes(&ctrl.config.enabled).to_vec();
}
true
}
#[dbus_interface(property)]
async fn sleep_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.power_states.sleep_anim;
}
true
}
#[dbus_interface(property)]
async fn all_leds_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.power_states.all_leds;
}
true
}
#[dbus_interface(property)]
async fn keys_leds_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.power_states.keys_leds;
}
true
}
#[dbus_interface(property)]
fn side_leds_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.power_states.side_leds;
}
true
vec![0, 0]
}
/// Return the current mode data
@@ -307,6 +208,6 @@ impl CtrlKbdLedZbus {
#[dbus_interface(signal)]
async fn notify_power_states(
signal_ctxt: &SignalContext<'_>,
data: &LedPowerStates,
data: &[AuraControl],
) -> zbus::Result<()>;
}