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

@@ -89,6 +89,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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));
}
}

View File

@@ -23,6 +23,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
writer.write_colour_block(&key_colours)?;
std::thread::sleep(std::time::Duration::from_millis(250));
std::thread::sleep(std::time::Duration::from_millis(30));
}
}

View File

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

View File

@@ -211,10 +211,10 @@ impl From<SetAuraBuiltin> 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 {

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 {