Move led_writer to a main loop due to strange mpsc behaviour

This commit is contained in:
Luke
2020-06-11 20:09:18 +12:00
parent 93c6b28409
commit bf3588e516
10 changed files with 65 additions and 63 deletions

View File

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

View File

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

View File

@@ -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<Mutex<Option<u8>>>;
pub(super) type LedMsgType = Arc<Mutex<Option<Vec<u8>>>>;
pub(super) type NestedVecType = Arc<Mutex<Option<Vec<Vec<u8>>>>>;
// Timing is such that:
// - interrupt write is minimum 1ms (sometimes lower)
@@ -108,7 +105,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
}
});
// 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<dyn Error>> {
}
});
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);
}
}
}
}
}

View File

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

View File

@@ -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<MTSyn
.inarg::<u8, _>("byte")
}
#[allow(clippy::type_complexity)]
pub(super) fn dbus_create_tree() -> (
Tree<MTSync, ()>,
Sender<AuraCommand>,

View File

@@ -29,6 +29,12 @@ pub struct VirtKeys {
device: UHIDDevice<std::fs::File>,
}
impl Default for VirtKeys {
fn default() -> Self {
Self::new()
}
}
impl VirtKeys {
pub fn new() -> Self {
VirtKeys {