comchan 0.3.3

A Blazingly Fast and Minimal Serial Monitor for Embedded Applications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use crate::config::MergedConfig;
use crate::parser::{SensorData, get_color_for_index, parse_sensor_data};
use crate::serial::{
    get_timestamp, parse_data_bits, parse_flow_control, parse_parity, parse_stop_bits,
};
use crossterm::{
    event::{self, KeyCode, KeyModifiers},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use inline_colorization::*;
use ratatui::{
    Terminal,
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout},
    prelude::*,
    style::{Color, Modifier, Style},
    symbols,
    text::{Line, Span},
    widgets::*,
};
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io::{self, BufWriter, Read, Write};
use std::time::{Duration, Instant};

// ── Plotter state ─────────────────────────────────────────────────────────────

struct PlotterState {
    sensors: HashMap<String, SensorData>,
    /// Ordered list of sensor names (insertion order)
    sensor_order: Vec<String>,
    x: f64,
    global_y_min: f64,
    global_y_max: f64,
    paused: bool,
    /// Total samples received (never paused)
    total_samples: u64,
    samples_this_second: u32,
    sample_rate: u32,
    last_rate_update: Instant,
    start_time: Instant,
    lines_discarded: usize,
    /// Buffer for incomplete serial lines
    receive_buf: String,
    /// Errors reported to the status bar
    last_error: Option<String>,
}

const DISCARD_FIRST_LINES: usize = 3;

impl PlotterState {
    fn new() -> Self {
        let now = Instant::now();
        PlotterState {
            sensors: HashMap::new(),
            sensor_order: Vec::new(),
            x: 0.0,
            global_y_min: f64::INFINITY,
            global_y_max: f64::NEG_INFINITY,
            paused: false,
            total_samples: 0,
            samples_this_second: 0,
            sample_rate: 0,
            last_rate_update: now,
            start_time: now,
            lines_discarded: 0,
            receive_buf: String::new(),
            last_error: None,
        }
    }

    fn get_or_create_sensor(&mut self, name: &str) -> &mut SensorData {
        if !self.sensors.contains_key(name) {
            let color = get_color_for_index(self.sensor_order.len());
            self.sensors
                .insert(name.to_string(), SensorData::new(name.to_string(), color));
            self.sensor_order.push(name.to_string());
        }
        self.sensors.get_mut(name).unwrap()
    }

    fn ingest_line(&mut self, line: &str, max_points: usize) {
        let clean = line.trim();

        if self.lines_discarded < DISCARD_FIRST_LINES {
            self.lines_discarded += 1;
            return;
        }

        if self.paused {
            return;
        }

        let readings = parse_sensor_data(clean);
        if readings.is_empty() {
            return;
        }

        for (name, value) in readings {
            let x = self.x;
            let sensor = self.get_or_create_sensor(name.as_ref());
            sensor.add_point(x, value, max_points);

            if value < self.global_y_min {
                self.global_y_min = value;
            }
            if value > self.global_y_max {
                self.global_y_max = value;
            }
        }

        self.x += 1.0;
        self.total_samples += 1;
        self.samples_this_second += 1;

        // Update sample rate every second
        let elapsed = self.last_rate_update.elapsed();
        if elapsed >= Duration::from_secs(1) {
            self.sample_rate = self.samples_this_second;
            self.samples_this_second = 0;
            self.last_rate_update = Instant::now();
        }
    }

    fn x_bounds(&self) -> [f64; 2] {
        let mut min_x = f64::INFINITY;
        let mut max_x = f64::NEG_INFINITY;

        for sensor in self.sensors.values() {
            if let (Some(first), Some(last)) = (sensor.data.first(), sensor.data.last()) {
                if first.0 < min_x {
                    min_x = first.0;
                }
                if last.0 > max_x {
                    max_x = last.0;
                }
            }
        }

        if min_x.is_finite() && max_x.is_finite() {
            [min_x, max_x]
        } else {
            [0.0, 10.0]
        }
    }

    fn y_bounds(&self) -> [f64; 2] {
        if self.global_y_min.is_finite() && self.global_y_max.is_finite() {
            if self.global_y_min == self.global_y_max {
                [self.global_y_min - 1.0, self.global_y_max + 1.0]
            } else {
                let padding = (self.global_y_max - self.global_y_min) * 0.1;
                [self.global_y_min - padding, self.global_y_max + padding]
            }
        } else {
            [-1.0, 1.0]
        }
    }

    fn uptime_str(&self) -> String {
        let secs = self.start_time.elapsed().as_secs();
        format!(
            "{:02}:{:02}:{:02}",
            secs / 3600,
            (secs % 3600) / 60,
            secs % 60
        )
    }
}

// ── Main entry point ──────────────────────────────────────────────────────────

pub fn run_plotter_mode(
    config: MergedConfig,
    port_name: String,
) -> Result<(), Box<dyn std::error::Error>> {
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let data_bits = parse_data_bits(config.data_bits)?;
    let stop_bits = parse_stop_bits(config.stop_bits)?;
    let parity = parse_parity(&config.parity)?;
    let flow_control = parse_flow_control(&config.flow_control)?;

    let mut port = serialport::new(&port_name, config.baud)
        .timeout(Duration::from_millis(config.timeout_ms))
        .data_bits(data_bits)
        .stop_bits(stop_bits)
        .parity(parity)
        .flow_control(flow_control)
        .open()?;

    let mut log_writer: Option<BufWriter<std::fs::File>> =
        if let Some(ref log_path) = config.log_file {
            let file = OpenOptions::new()
                .create(true)
                .append(true)
                .open(log_path)?;
            Some(BufWriter::new(file))
        } else {
            None
        };

    std::thread::sleep(Duration::from_millis(config.reset_delay_ms));

    let mut state = PlotterState::new();
    let mut serial_buf = [0u8; 1024];

    loop {
        // ── Input handling ────────────────────────────────────────────────────
        if event::poll(Duration::from_millis(5))?
            && let event::Event::Key(key) = event::read()?
        {
            match key.code {
                KeyCode::Char('q') | KeyCode::Esc => break,
                KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => break,

                // Space: pause / resume
                KeyCode::Char(' ') => {
                    state.paused = !state.paused;
                }

                // 'c': clear all data
                KeyCode::Char('c') => {
                    state.sensors.clear();
                    state.sensor_order.clear();
                    state.x = 0.0;
                    state.global_y_min = f64::INFINITY;
                    state.global_y_max = f64::NEG_INFINITY;
                    state.total_samples = 0;
                }

                _ => {}
            }
        }

        // ── Serial read ───────────────────────────────────────────────────────
        match port.read(&mut serial_buf) {
            Ok(n) if n > 0 => {
                let chunk = String::from_utf8_lossy(&serial_buf[..n]);
                state.receive_buf.push_str(&chunk);

                while let Some(pos) = state.receive_buf.find('\n') {
                    let line = state.receive_buf.drain(..=pos).collect::<String>();

                    if let Some(ref mut writer) = log_writer {
                        let _ = writeln!(writer, "RX [{}]: {}", get_timestamp(), line.trim_end());
                        let _ = writer.flush();
                    }

                    state.ingest_line(&line, config.plot_points);
                }
            }
            Ok(_) => {}
            Err(ref e) if e.kind() == io::ErrorKind::TimedOut => {}
            Err(e) => {
                state.last_error = Some(format!("Read error: {}", e));
            }
        }

        // ── Render ────────────────────────────────────────────────────────────
        let x_bounds = state.x_bounds();
        let y_bounds = state.y_bounds();

        // Pre-compute labels before the closure borrows state
        let x_labels = [
            format!("{:.0}", x_bounds[0]),
            format!("{:.0}", (x_bounds[0] + x_bounds[1]) / 2.0),
            format!("{:.0}", x_bounds[1]),
        ];
        let y_labels = [
            format!("{:.2}", y_bounds[0]),
            format!("{:.2}", (y_bounds[0] + y_bounds[1]) / 2.0),
            format!("{:.2}", y_bounds[1]),
        ];

        let pause_indicator = if state.paused { " ⏸ PAUSED" } else { "" };
        let uptime = state.uptime_str();
        let sample_rate = state.sample_rate;
        let total_samples = state.total_samples;
        let sensor_count = state.sensors.len();
        let last_error = state.last_error.clone();
        let baud = config.baud;
        let port_name_disp = port_name.clone();

        // Build sidebar rows before the draw closure
        let sidebar_rows: Vec<(String, Color, f64, f64, f64)> = state
            .sensor_order
            .iter()
            .filter_map(|name| {
                state.sensors.get(name).map(|s| {
                    (
                        s.name.clone(),
                        s.color,
                        s.current_value,
                        s.min_value,
                        s.max_value,
                    )
                })
            })
            .collect();

        // Build datasets
        let datasets: Vec<Dataset> = state
            .sensor_order
            .iter()
            .filter_map(|name| state.sensors.get(name))
            .filter(|s| s.has_data())
            .map(|sensor| {
                Dataset::default()
                    .name(format!("{} ({:.2})", sensor.name, sensor.current_value))
                    .marker(symbols::Marker::Braille)
                    .graph_type(GraphType::Line)
                    .style(Style::default().fg(sensor.color))
                    .data(&sensor.data)
            })
            .collect();

        terminal.draw(|f| {
            // ── Layout ────────────────────────────────────────────────────────
            //
            //  ┌───────────────── chart ──────────────┬── sidebar ──┐
            //  │                                       │             │
            //  │                                       │  sensor     │
            //  │                                       │  stats      │
            //  │                                       │             │
            //  └───────────────────────────────────────┴─────────────┘
            //  ┌─────────────────── status bar ───────────────────────┐

            let outer = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(3), Constraint::Length(3)])
                .split(f.area());

            let main_row = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Min(10), Constraint::Length(28)])
                .split(outer[0]);

            // ── Chart ─────────────────────────────────────────────────────────
            let chart_title = format!(
                " 󰕾 ComChan Plotter  {}  {}  {} sensors{}",
                port_name_disp, baud, sensor_count, pause_indicator
            );

            let chart = Chart::new(datasets)
                .block(
                    Block::default()
                        .title(Span::styled(
                            chart_title,
                            Style::default()
                                .fg(Color::White)
                                .add_modifier(Modifier::BOLD),
                        ))
                        .borders(Borders::ALL)
                        .border_style(Style::default().fg(Color::DarkGray)),
                )
                .x_axis(
                    Axis::default()
                        .title(Span::styled("Sample", Style::default().fg(Color::Gray)))
                        .style(Style::default().fg(Color::DarkGray))
                        .bounds(x_bounds)
                        .labels(vec![
                            x_labels[0].as_str(),
                            x_labels[1].as_str(),
                            x_labels[2].as_str(),
                        ]),
                )
                .y_axis(
                    Axis::default()
                        .title(Span::styled("Value", Style::default().fg(Color::Gray)))
                        .style(Style::default().fg(Color::DarkGray))
                        .bounds(y_bounds)
                        .labels(vec![
                            y_labels[0].as_str(),
                            y_labels[1].as_str(),
                            y_labels[2].as_str(),
                        ]),
                )
                .legend_position(Some(LegendPosition::TopLeft));

            f.render_widget(chart, main_row[0]);

            // ── Sidebar (sensor stats) ────────────────────────────────────────
            let sidebar_block = Block::default()
                .title(Span::styled(
                    " Sensors ",
                    Style::default()
                        .fg(Color::Cyan)
                        .add_modifier(Modifier::BOLD),
                ))
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::DarkGray));

            let inner = sidebar_block.inner(main_row[1]);
            f.render_widget(sidebar_block, main_row[1]);

            if sidebar_rows.is_empty() {
                let waiting = Paragraph::new("Waiting for\ndata…")
                    .style(Style::default().fg(Color::DarkGray))
                    .alignment(Alignment::Center);
                f.render_widget(waiting, inner);
            } else {
                // Each sensor gets 4 lines: name + cur/min/max + separator
                let mut lines: Vec<Line> = Vec::new();
                for (name, color, cur, min, max) in &sidebar_rows {
                    lines.push(Line::from(vec![Span::styled(
                        format!("{}", name),
                        Style::default().fg(*color).add_modifier(Modifier::BOLD),
                    )]));
                    lines.push(Line::from(vec![Span::styled(
                        format!("  now: {:.3}", cur),
                        Style::default().fg(Color::White),
                    )]));
                    lines.push(Line::from(vec![Span::styled(
                        format!("  min: {:.3}", min),
                        Style::default().fg(Color::Blue),
                    )]));
                    lines.push(Line::from(vec![Span::styled(
                        format!("  max: {:.3}", max),
                        Style::default().fg(Color::Red),
                    )]));
                    lines.push(Line::from(vec![Span::styled(
                        "  ─────────────",
                        Style::default().fg(Color::DarkGray),
                    )]));
                }
                let para = Paragraph::new(lines).wrap(Wrap { trim: false });
                f.render_widget(para, inner);
            }

            // ── Status bar ────────────────────────────────────────────────────
            let status_bg = if state.paused {
                Color::DarkGray
            } else {
                Color::Reset
            };

            let error_span = if let Some(ref err) = last_error {
                Span::styled(format!("{} ", err), Style::default().fg(Color::Red))
            } else {
                Span::raw("")
            };

            let status_line = Line::from(vec![
                Span::styled(format!("{}", uptime), Style::default().fg(Color::Green)),
                Span::raw("  "),
                Span::styled(
                    format!("󰩙 {} sps", sample_rate),
                    Style::default().fg(Color::Cyan),
                ),
                Span::raw("  "),
                Span::styled(
                    format!("󰆼 {} total", total_samples),
                    Style::default().fg(Color::Yellow),
                ),
                Span::raw("  "),
                error_span,
                Span::styled(
                    "  [Space] pause  [c] clear  [q/Esc] quit",
                    Style::default().fg(Color::DarkGray),
                ),
            ]);

            let status_bar = Paragraph::new(status_line)
                .block(
                    Block::default()
                        .borders(Borders::ALL)
                        .border_style(Style::default().fg(Color::DarkGray)),
                )
                .style(Style::default().bg(status_bg));

            f.render_widget(status_bar, outer[1]);
        })?;
    }

    // ── Cleanup ───────────────────────────────────────────────────────────────
    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;

    if !state.sensors.is_empty() {
        println!("\n{color_green}󰄨 Plotting Summary:{color_reset}");
        for name in &state.sensor_order {
            if let Some(s) = state.sensors.get(name) {
                println!(
                    "   {}: {} pts  min={:.3}  max={:.3}  last={:.3}",
                    s.name,
                    s.data.len(),
                    s.min_value,
                    s.max_value,
                    s.current_value,
                );
            }
        }
        println!(
            "   Total samples: {}  Uptime: {}",
            state.total_samples,
            state.uptime_str()
        );
    }

    Ok(())
}