improve time management

This commit is contained in:
Tobias Ollive 2022-03-18 14:41:16 +01:00
parent b332d75c7f
commit 66c431f20c
2 changed files with 8 additions and 5 deletions

View File

@ -27,9 +27,9 @@ impl<const N: usize> Pattern for StarsRandom<N> {
fn init(&mut self) -> Option<Self::Strip> { fn init(&mut self) -> Option<Self::Strip> {
if let Some(color) = self.background_color { if let Some(color) = self.background_color {
self.strip = Strip::<N>::new(color); self.strip = Strip::<N>::new(color);
let red = (self.color.red as i16 - color.red as i16); let red = self.color.red as i16 - color.red as i16;
let green = (color.green as i16 - self.color.green as i16); let green = color.green as i16 - self.color.green as i16;
let blue = (self.color.blue as i16 - color.blue as i16); let blue = self.color.blue as i16 - color.blue as i16;
self.step = ( self.step = (
-(red / self.steps as i16) as i8, -(red / self.steps as i16) as i8,
-(green / self.steps as i16) as i8, -(green / self.steps as i16) as i8,

View File

@ -9,6 +9,7 @@ use std::future::Future;
use std::str::FromStr; use std::str::FromStr;
use std::time::Duration; use std::time::Duration;
use tokio::sync::watch::{Receiver, Sender}; use tokio::sync::watch::{Receiver, Sender};
use tokio::time::Instant;
pub(crate) mod period { pub(crate) mod period {
use serde::{de::Error as _, Deserialize, Deserializer}; use serde::{de::Error as _, Deserialize, Deserializer};
@ -56,17 +57,19 @@ async fn runner_loop<const N: usize>(runner: DeviceSequence<N>, tx: Sender<Strip
for mut context in runner { for mut context in runner {
let mut strip = context.pattern.init().unwrap_or(background); let mut strip = context.pattern.init().unwrap_or(background);
let delay = context.period; let delay = context.period;
let mut next_instant = Instant::now() + delay;
while tx.send(strip).is_ok() { while tx.send(strip).is_ok() {
if let Some(value) = context.pattern.next() { if let Some(value) = context.pattern.next() {
strip = apply_mask(background, value); strip = apply_mask(background, value);
} else { } else {
break; break;
} }
tokio::time::sleep(delay).await; tokio::time::sleep_until(next_instant).await;
next_instant += delay;
} }
background = strip; background = strip;
println!("{:?}", background);
} }
} }
fn apply_mask<const N: usize>(previous_strip: Strip<N>, strip: Strip<{ N }>) -> Strip<N> { fn apply_mask<const N: usize>(previous_strip: Strip<N>, strip: Strip<{ N }>) -> Strip<N> {