From bf3588e516015d98460804e963aafc3e2cfe10e0 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 11 Jun 2020 20:09:18 +1200 Subject: [PATCH] Move led_writer to a main loop due to strange mpsc behaviour --- rog-client/examples/ball.rs | 2 +- rog-client/examples/comet.rs | 2 +- rog-client/src/anime_matrix.rs | 11 +++-- rog-client/src/lib.rs | 8 ++-- rog-core/src/animatrix_control.rs | 12 +++--- rog-core/src/config.rs | 6 +-- rog-core/src/daemon.rs | 72 +++++++++++++------------------ rog-core/src/laptops.rs | 4 +- rog-core/src/rog_dbus.rs | 5 ++- rog-core/src/virt_device.rs | 6 +++ 10 files changed, 65 insertions(+), 63 deletions(-) diff --git a/rog-client/examples/ball.rs b/rog-client/examples/ball.rs index fa08d3ad..b2fea301 100644 --- a/rog-client/examples/ball.rs +++ b/rog-client/examples/ball.rs @@ -89,6 +89,6 @@ fn main() -> Result<(), Box> { writer.write_colour_block(&colours)?; // can change 100 times per second, so need to slow it down - std::thread::sleep(std::time::Duration::from_millis(60)); + std::thread::sleep(std::time::Duration::from_millis(30)); } } diff --git a/rog-client/examples/comet.rs b/rog-client/examples/comet.rs index 691d013f..0aad01d9 100644 --- a/rog-client/examples/comet.rs +++ b/rog-client/examples/comet.rs @@ -23,6 +23,6 @@ fn main() -> Result<(), Box> { } writer.write_colour_block(&key_colours)?; - std::thread::sleep(std::time::Duration::from_millis(250)); + std::thread::sleep(std::time::Duration::from_millis(30)); } } diff --git a/rog-client/src/anime_matrix.rs b/rog-client/src/anime_matrix.rs index 35d10193..2720dfc5 100644 --- a/rog-client/src/anime_matrix.rs +++ b/rog-client/src/anime_matrix.rs @@ -11,6 +11,12 @@ use yansi_term::Colour::RGB; /// See the examples for ways to write an image to `AniMeMatrix` format. pub struct AniMeMatrix(AniMeBufferType); +impl Default for AniMeMatrix { + fn default() -> Self { + Self::new() + } +} + impl AniMeMatrix { pub fn new() -> Self { AniMeMatrix([[0u8; WIDTH]; HEIGHT]) @@ -127,7 +133,7 @@ impl From for AniMePacketType { } let index = row.len() - prog_row_len; - for n in index..row.len() { + for n in row.iter().skip(index) { // Require a special case to catch the correct end-of-packet which is // 6 bytes from the end if write_index == BLOCK_END && !block1_done { @@ -136,8 +142,7 @@ impl From for AniMePacketType { write_index = BLOCK_START; } - //println!("{:?}", write_block.to_vec()); - write_block[write_index] = row[n]; + write_block[write_index] = *n; write_index += 1; } } diff --git a/rog-client/src/lib.rs b/rog-client/src/lib.rs index c6c3c96f..fe3127af 100644 --- a/rog-client/src/lib.rs +++ b/rog-client/src/lib.rs @@ -211,10 +211,10 @@ impl From for [[u8; LED_MSG_LEN]; 4] { #[inline] fn from(mode: SetAuraBuiltin) -> Self { let mut msg = [[0u8; LED_MSG_LEN]; 4]; - for i in 0..4 { - msg[i][0] = 0x5d; - msg[i][1] = 0xb3; - msg[i][2] = i as u8 + 1; + for (i, row) in msg.iter_mut().enumerate() { + row[0] = 0x5d; + row[1] = 0xb3; + row[2] = i as u8 + 1; } match mode { diff --git a/rog-core/src/animatrix_control.rs b/rog-core/src/animatrix_control.rs index 9d37d97a..57d77b3e 100644 --- a/rog-core/src/animatrix_control.rs +++ b/rog-core/src/animatrix_control.rs @@ -148,14 +148,14 @@ impl AniMeWriter { init[idx + 1] = *byte } self.write_bytes(&init).await?; + // clear the init array and write other init message - for idx in 0..INIT_STR.len() { - match idx { - 0 => init[idx] = DEV_PAGE, // write it to be sure? - 1 => init[idx] = INIT, - _ => init[idx] = 0, - } + for ch in init.iter_mut() { + *ch = 0; } + init[0] = DEV_PAGE; // write it to be sure? + init[1] = INIT; + self.write_bytes(&init).await?; self.initialised = true; Ok(()) diff --git a/rog-core/src/config.rs b/rog-core/src/config.rs index ef3db6ac..7d98dad4 100644 --- a/rog-core/src/config.rs +++ b/rog-core/src/config.rs @@ -34,7 +34,7 @@ impl Config { // Should be okay to unwrap this as is since it is a Default let toml = toml::to_string(&c).unwrap(); file.write_all(toml.as_bytes()) - .expect("Writing default config failed"); + .unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH)); self = c; } else { self = @@ -54,8 +54,8 @@ impl Config { if l == 0 { panic!("Missing {}", CONFIG_PATH); } else { - let x: Config = - toml::from_str(&buf).expect(&format!("Could not deserialise {}", CONFIG_PATH)); + let x: Config = toml::from_str(&buf) + .unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH)); *self = x; } } diff --git a/rog-core/src/daemon.rs b/rog-core/src/daemon.rs index d0be5622..43776ccf 100644 --- a/rog-core/src/daemon.rs +++ b/rog-core/src/daemon.rs @@ -14,12 +14,9 @@ use log::{error, info, warn}; use rog_client::{DBUS_IFACE, DBUS_NAME, DBUS_PATH}; use std::error::Error; use std::sync::Arc; -use std::time::{Duration, Instant}; -use tokio::sync::{mpsc, Mutex}; +use tokio::sync::Mutex; pub(super) type FanModeType = Arc>>; -pub(super) type LedMsgType = Arc>>>; -pub(super) type NestedVecType = Arc>>>>; // Timing is such that: // - interrupt write is minimum 1ms (sometimes lower) @@ -108,7 +105,7 @@ pub async fn start_daemon() -> Result<(), Box> { let config1 = config.clone(); // start the keyboard reader and laptop-action loop - let key_read_handle = tokio::spawn(async move { + tokio::spawn(async move { loop { // Fan mode if let Ok(mut lock) = fan_mode.try_lock() { @@ -129,39 +126,8 @@ pub async fn start_daemon() -> Result<(), Box> { } }); - // start the LED writer loop - let led_write_handle = tokio::spawn(async move { - loop { - //connection.process_all(); - - // Check if a key press issued a command - while let Some(command) = aura_command_recv.recv().await { - let mut config = config.lock().await; - match command { - AuraCommand::WriteEffect(_) | AuraCommand::WriteMultizone(_) => led_writer - .do_command(command, &mut config) - .await - .unwrap_or_else(|err| warn!("{:?}", err)), - _ => { - led_writer - .do_command(command, &mut config) - .await - .unwrap_or_else(|err| warn!("{:?}", err)); - connection - .send( - effect_cancel_signal - .msg(&DBUS_PATH.into(), &DBUS_IFACE.into()) - .append1(true), - ) - .unwrap_or_else(|_| 0); - } - } - } - } - }); - // If animatrix is supported, try doing a write - let animatrix_write_handle = tokio::spawn(async move { + tokio::spawn(async move { if let Some(writer) = animatrix_writer.as_mut() { while let Some(image) = animatrix_recv.recv().await { writer @@ -172,8 +138,32 @@ pub async fn start_daemon() -> Result<(), Box> { } }); - animatrix_write_handle.await?; - led_write_handle.await?; - key_read_handle.await?; - Ok(()) + // start the main loop + loop { + connection.process_all(); + + // Check if a key press issued a command + while let Some(command) = aura_command_recv.recv().await { + let mut config = config.lock().await; + match command { + AuraCommand::WriteEffect(_) | AuraCommand::WriteMultizone(_) => led_writer + .do_command(command, &mut config) + .await + .unwrap_or_else(|err| warn!("{:?}", err)), + _ => { + led_writer + .do_command(command, &mut config) + .await + .unwrap_or_else(|err| warn!("{:?}", err)); + connection + .send( + effect_cancel_signal + .msg(&DBUS_PATH.into(), &DBUS_IFACE.into()) + .append1(true), + ) + .unwrap_or_else(|_| 0); + } + } + } + } } diff --git a/rog-core/src/laptops.rs b/rog-core/src/laptops.rs index 09c0cae7..99ad8019 100644 --- a/rog-core/src/laptops.rs +++ b/rog-core/src/laptops.rs @@ -174,13 +174,13 @@ impl LaptopBase { aura_command .send(AuraCommand::BrightInc) .await - .unwrap_or_else(|_| {}); + .unwrap_or_else(|err| warn!("LedBrightUp: {}", err)); } GX502Keys::LedBrightDown => { aura_command .send(AuraCommand::BrightDec) .await - .unwrap_or_else(|_| {}); + .unwrap_or_else(|err| warn!("LedBrightDown: {}", err)); } GX502Keys::AuraNext => { aura_command diff --git a/rog-core/src/rog_dbus.rs b/rog-core/src/rog_dbus.rs index 5a447726..731f2012 100644 --- a/rog-core/src/rog_dbus.rs +++ b/rog-core/src/rog_dbus.rs @@ -1,8 +1,8 @@ -use crate::daemon::{FanModeType, LedMsgType, NestedVecType}; +use crate::daemon::FanModeType; use crate::led_control::AuraCommand; use crate::rogcore::FanLevel; use dbus::tree::{Factory, MTSync, Method, MethodErr, Signal, Tree}; -use log::{error, info, warn}; +use log::warn; use rog_client::{DBUS_IFACE, DBUS_PATH}; use std::sync::Arc; use tokio::sync::{ @@ -162,6 +162,7 @@ pub(super) fn dbus_create_fan_mode_method(fan_mode: FanModeType) -> Method("byte") } +#[allow(clippy::type_complexity)] pub(super) fn dbus_create_tree() -> ( Tree, Sender, diff --git a/rog-core/src/virt_device.rs b/rog-core/src/virt_device.rs index 36a36e91..d290cf63 100644 --- a/rog-core/src/virt_device.rs +++ b/rog-core/src/virt_device.rs @@ -29,6 +29,12 @@ pub struct VirtKeys { device: UHIDDevice, } +impl Default for VirtKeys { + fn default() -> Self { + Self::new() + } +} + impl VirtKeys { pub fn new() -> Self { VirtKeys {