use liveplot::{channel_plot, run_liveplot, LivePlotConfig, PlotPoint, TracesController};
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; let dt = Duration::from_millis(1);
let mut n: u64 = 0;
loop {
let t = n 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 = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0);
let _ = sink.send_point(&tr_sine, PlotPoint { x: t_s, y: s_val });
let _ = sink.send_point(&tr_cos, PlotPoint { x: t_s, y: c_val });
n = n.wrapping_add(1);
std::thread::sleep(dt);
}
});
let traces_ctrl = TracesController::new();
{
let ctrl = traces_ctrl.clone();
let rx_info = ctrl.subscribe();
std::thread::spawn(move || {
while let Ok(info) = rx_info.recv() {
for tr in info.traces {
match tr.name.as_str() {
"sine" => ctrl.request_set_color("sine", [0xFF, 0x66, 0x00]), "cosine" => ctrl.request_set_color("cosine", [0x00, 0x99, 0xFF]), _ => {}
}
}
}
});
}
let mut cfg = LivePlotConfig::default();
cfg.title = "LivePlot (custom colors)".into();
cfg.traces_controller = Some(traces_ctrl);
run_liveplot(rx, cfg)
}