use liveplot::{channel_plot, run_liveplot, LivePlotConfig, PlotPoint};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
fn main() -> eframe::Result<()> {
let (sink, rx) = channel_plot();
let tr_sine = sink.create_trace("sine", None);
let tr_cos = sink.create_trace("cosine", None);
std::thread::spawn(move || {
const FS_HZ: f64 = 1000.0; const F_HZ: f64 = 3.0; const CHUNK: usize = 200;
let dt = Duration::from_millis((CHUNK as f64 / FS_HZ * 1000.0) as u64);
let mut n: u64 = 0;
loop {
let base_t = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0);
let mut pts_sine: Vec<PlotPoint> = Vec::with_capacity(CHUNK);
let mut pts_cos: Vec<PlotPoint> = Vec::with_capacity(CHUNK);
for i in 0..CHUNK {
let t = (n as f64 + i as f64) / FS_HZ; let s_val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
let c_val = (2.0 * std::f64::consts::PI * F_HZ * t).cos();
let t_s = base_t + (i as f64) / FS_HZ;
pts_sine.push(PlotPoint { x: t_s, y: s_val });
pts_cos.push(PlotPoint { x: t_s, y: c_val });
}
let _ = sink.send_points(&tr_sine, pts_sine);
let _ = sink.send_points(&tr_cos, pts_cos);
n = n.wrapping_add(CHUNK as u64);
std::thread::sleep(dt);
}
});
let mut cfg = LivePlotConfig::default();
cfg.headline = Some("Sine/cosine example".to_string());
cfg.subheadline = Some("(with chunks)".to_string());
run_liveplot(rx, cfg)
}