sine/sine.rs
1//! Example: Continuous sine wave producer
2//!
3//! What it demonstrates
4//! - Streaming samples into the multi-trace UI using `channel_plot()` and `PlotSink`.
5//! - Creating a trace with `create_trace` and sending points at a fixed sample rate.
6//!
7//! How to run
8//! ```bash
9//! cargo run --example sine
10//! ```
11//! You should see a single trace named "signal" rendering a live sine wave.
12
13use liveplot::{channel_plot, run_liveplot, LivePlotConfig, PlotPoint};
14use std::time::{Duration, SystemTime, UNIX_EPOCH};
15
16fn main() -> eframe::Result<()> {
17 // Create multi-trace plot channel (we use a single trace labeled "signal")
18 let (sink, rx) = channel_plot();
19 let trace = sink.create_trace("signal", Some("Test Sine"));
20
21 // Producer: 1 kHz sample rate, 3 Hz sine
22 std::thread::spawn(move || {
23 const FS_HZ: f64 = 1000.0; // 1 kHz sampling rate
24 const F_HZ: f64 = 3.0; // 3 Hz sine wave
25 let dt = Duration::from_millis(1);
26 let mut n: u64 = 0;
27 loop {
28 let t = n as f64 / FS_HZ;
29 let val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
30 let t_s = SystemTime::now()
31 .duration_since(UNIX_EPOCH)
32 .map(|d| d.as_secs_f64())
33 .unwrap_or(0.0);
34 // Ignore error if the UI closed (receiver dropped)
35 let _ = sink.send_point(&trace, PlotPoint { x: t_s, y: val });
36 n = n.wrapping_add(1);
37 std::thread::sleep(dt);
38 }
39 });
40
41 // Run the UI until closed
42 run_liveplot(rx, LivePlotConfig::default())
43}