use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};
use liveplot::{run_liveplot, LivePlotConfig};
use liveplot::PlotPoint;
use liveplot::PlotCommand;
fn make_wave(is_sine: bool, t0: f64, n: usize, dt: f64) -> Vec<PlotPoint> {
let mut v = Vec::with_capacity(n);
for i in 0..n {
let t = t0 + (i as f64) * dt;
let phase = 2.0 * std::f64::consts::PI * 1.0 * t; let y = if is_sine { phase.sin() } else { phase.cos() };
v.push(PlotPoint { x: t, y });
}
v
}
fn main() {
let (tx, rx) = mpsc::channel::<PlotCommand>();
let producer = thread::spawn(move || {
let _ = tx.send(PlotCommand::RegisterTrace {
id: 1,
name: "toggle".into(),
info: Some("sine/cosine toggle".into()),
});
let _start = Instant::now();
let mut t0 = 0.0_f64;
let n = 200usize;
let dt = 0.01_f64;
let mut is_sine = true;
loop {
let pts = make_wave(is_sine, t0, n, dt);
if mpsc::Sender::send(&tx, PlotCommand::SetData { trace_id: 1, points: pts }).is_err() {
break; }
thread::sleep(Duration::from_secs(2));
t0 += 2.0;
is_sine = !is_sine;
}
});
if let Err(e) = run_liveplot(rx, LivePlotConfig::default()) {
eprintln!("UI error: {e}");
}
let _ = producer.join();
}