push empty samples to compensate time lag

this can happen when the script freezes or the device goes to sleep
This commit is contained in:
Ondrej Novak 2026-06-30 22:44:11 +02:00
parent 81794de504
commit 9025e0e461
Signed by: handy
SSH Key Fingerprint: SHA256:jlNm8ijkaMl4X7+nn3zBFTJufdQgT4unKJoZVH6kYTM
3 changed files with 32 additions and 2 deletions

View File

@ -27,7 +27,7 @@ fn main() {
.iter()
.map(|g| Graph::new(g, &mut proto))
.collect();
let mut runner = Runner::new(graphs, proto);
let mut runner = Runner::new(graphs, proto, config.global.period);
loop {
let r = runner.tick();

View File

@ -85,4 +85,8 @@ impl ObservableCollection {
pub fn sample(&self, proto: &mut Protocol) -> Result<Vec<f32>, ProtocolError> {
self.observables.iter().map(|o| o.sample(proto)).collect()
}
pub fn get_num_observables(&self) -> usize {
self.observables.len()
}
}

View File

@ -1,6 +1,8 @@
use core::fmt;
use std::time::SystemTime;
use draw::{StackedConfig, StackedSeries, StackedSeriesConfig, stacked};
use imageproc::image::codecs::pnm::SampleEncoding;
use protocol::Protocol;
use crate::{
@ -68,17 +70,41 @@ impl fmt::Display for RunnerError {
pub struct Runner {
graphs: Vec<Graph>,
proto: Protocol,
last_sample: SystemTime,
sampling_period: f32,
}
impl Runner {
pub fn new(graphs: Vec<Graph>, proto: Protocol) -> Runner {
pub fn new(graphs: Vec<Graph>, proto: Protocol, sampling_period: f32) -> Runner {
Runner {
graphs: graphs,
proto: proto,
last_sample: SystemTime::UNIX_EPOCH,
sampling_period: sampling_period,
}
}
pub fn tick(&mut self) -> Result<(), RunnerError> {
let now = SystemTime::now();
let dt = if self.last_sample == SystemTime::UNIX_EPOCH {
self.sampling_period
} else {
now.duration_since(self.last_sample)
.expect("time jumped back")
.as_secs_f32()
};
self.last_sample = now;
for g in &mut self.graphs {
// fill in empty samples for missing data
if dt >= 2. {
let dt_round = dt.round() as u32;
for _ in 0..dt_round {
let dummy = vec![0.; g.observable.get_num_observables()];
g.series.push(dummy)
}
}
// sample data
let sample = g
.observable
.sample(&mut self.proto)