2022-12-24 06:21:58 +01:00
|
|
|
|
2024-03-18 03:36:38 +01:00
|
|
|
use std::{io, ops::Range, time::{Duration, Instant}};
|
2023-09-18 00:56:51 +02:00
|
|
|
use ratatui::{
|
2023-09-17 07:10:08 +02:00
|
|
|
style::Color, widgets::{Table, Row, Cell}, symbols::Marker,
|
2022-12-28 03:53:37 +01:00
|
|
|
backend::Backend,
|
2023-09-17 07:10:08 +02:00
|
|
|
widgets::Chart,
|
|
|
|
Terminal, style::{Style, Modifier}, layout::{Rect, Constraint}
|
2022-12-28 03:53:37 +01:00
|
|
|
};
|
|
|
|
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
|
2022-12-24 06:21:58 +01:00
|
|
|
|
2024-03-18 03:36:38 +01:00
|
|
|
use crate::{display::{oscilloscope::Oscilloscope, spectroscope::Spectroscope, vectorscope::Vectorscope, Dimension, DisplayMode, GraphConfig}, input::{Matrix, DataSource}};
|
2022-12-24 06:21:58 +01:00
|
|
|
|
2023-09-17 07:10:08 +02:00
|
|
|
pub enum CurrentDisplayMode {
|
|
|
|
Oscilloscope,
|
|
|
|
Vectorscope,
|
|
|
|
Spectroscope,
|
|
|
|
}
|
2023-01-05 00:51:05 +01:00
|
|
|
|
2023-09-17 07:10:08 +02:00
|
|
|
pub struct App {
|
2024-03-18 03:36:38 +01:00
|
|
|
#[allow(unused)] channels: u8,
|
2023-09-17 07:10:08 +02:00
|
|
|
graph: GraphConfig,
|
|
|
|
oscilloscope: Oscilloscope,
|
|
|
|
vectorscope: Vectorscope,
|
|
|
|
spectroscope: Spectroscope,
|
|
|
|
mode: CurrentDisplayMode,
|
|
|
|
}
|
2023-06-29 16:09:36 +02:00
|
|
|
|
2023-09-18 04:36:52 +02:00
|
|
|
// TODO another way to build this that doesn't require getting cli args directly!!!
|
2024-03-18 06:12:34 +01:00
|
|
|
impl App {
|
|
|
|
pub fn new(ui: &crate::cfg::UiOptions, source: &crate::cfg::SourceOptions) -> Self {
|
2023-09-18 00:57:16 +02:00
|
|
|
let graph = GraphConfig {
|
|
|
|
axis_color: Color::DarkGray,
|
|
|
|
labels_color: Color::Cyan,
|
|
|
|
palette: vec![Color::Red, Color::Yellow, Color::Green, Color::Magenta],
|
2024-03-18 06:12:34 +01:00
|
|
|
scale: ui.scale as f64,
|
|
|
|
width: source.buffer, // TODO also make bit depth customizable
|
|
|
|
samples: source.buffer,
|
|
|
|
sampling_rate: source.sample_rate,
|
|
|
|
references: !ui.no_reference,
|
|
|
|
show_ui: !ui.no_ui,
|
|
|
|
scatter: ui.scatter,
|
2023-09-21 03:50:48 +02:00
|
|
|
pause: false,
|
2024-03-18 06:12:34 +01:00
|
|
|
marker_type: if ui.no_braille {
|
2023-09-18 00:57:16 +02:00
|
|
|
Marker::Dot
|
|
|
|
} else {
|
|
|
|
Marker::Braille
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-03-18 06:12:34 +01:00
|
|
|
let oscilloscope = Oscilloscope::from_args(source);
|
|
|
|
let vectorscope = Vectorscope::from_args(source);
|
|
|
|
let spectroscope = Spectroscope::from_args(source);
|
2023-09-18 00:57:16 +02:00
|
|
|
|
|
|
|
App {
|
|
|
|
graph, oscilloscope, vectorscope, spectroscope,
|
|
|
|
mode: CurrentDisplayMode::Oscilloscope,
|
2024-03-18 06:12:34 +01:00
|
|
|
channels: source.channels as u8,
|
2023-09-18 00:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 03:36:38 +01:00
|
|
|
pub fn run<T : Backend>(&mut self, mut source: Box<dyn DataSource<f64>>, terminal: &mut Terminal<T>) -> Result<(), io::Error> {
|
2023-09-17 07:10:08 +02:00
|
|
|
let mut fps = 0;
|
|
|
|
let mut framerate = 0;
|
|
|
|
let mut last_poll = Instant::now();
|
2024-03-18 03:36:38 +01:00
|
|
|
let mut channels = Matrix::default();
|
2023-09-17 07:10:08 +02:00
|
|
|
|
|
|
|
loop {
|
2023-09-18 04:36:52 +02:00
|
|
|
let data = source.recv()
|
|
|
|
.ok_or(io::Error::new(io::ErrorKind::BrokenPipe, "data source returned null"))?;
|
2023-09-17 07:10:08 +02:00
|
|
|
|
2023-09-21 03:50:48 +02:00
|
|
|
if !self.graph.pause {
|
2024-03-18 03:36:38 +01:00
|
|
|
channels = data;
|
2022-12-28 03:55:37 +01:00
|
|
|
}
|
2023-09-17 07:10:08 +02:00
|
|
|
|
|
|
|
fps += 1;
|
|
|
|
|
|
|
|
if last_poll.elapsed().as_secs() >= 1 {
|
|
|
|
framerate = fps;
|
|
|
|
fps = 0;
|
|
|
|
last_poll = Instant::now();
|
2022-12-28 03:55:37 +01:00
|
|
|
}
|
2023-09-17 07:10:08 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut datasets = Vec::new();
|
2023-09-18 00:57:16 +02:00
|
|
|
let graph = self.graph.clone(); // TODO cheap fix...
|
2023-09-17 07:10:08 +02:00
|
|
|
if self.graph.references {
|
2023-09-18 00:57:16 +02:00
|
|
|
datasets.append(&mut self.current_display_mut().references(&graph));
|
2022-12-28 03:55:37 +01:00
|
|
|
}
|
2023-09-18 00:57:16 +02:00
|
|
|
datasets.append(&mut self.current_display_mut().process(&graph, &channels));
|
2023-09-17 07:10:08 +02:00
|
|
|
terminal.draw(|f| {
|
|
|
|
let mut size = f.size();
|
|
|
|
if self.graph.show_ui {
|
|
|
|
f.render_widget(
|
2023-09-21 03:50:48 +02:00
|
|
|
make_header(&self.graph, &self.current_display().header(&self.graph), self.current_display().mode_str(), framerate, self.graph.pause),
|
2023-09-17 07:10:08 +02:00
|
|
|
Rect { x: size.x, y: size.y, width: size.width, height:1 } // a 1px line at the top
|
|
|
|
);
|
|
|
|
size.height -= 1;
|
|
|
|
size.y += 1;
|
|
|
|
}
|
|
|
|
let chart = Chart::new(datasets.iter().map(|x| x.into()).collect())
|
2023-09-18 00:57:16 +02:00
|
|
|
.x_axis(self.current_display().axis(&self.graph, Dimension::X)) // TODO allow to have axis sometimes?
|
|
|
|
.y_axis(self.current_display().axis(&self.graph, Dimension::Y));
|
2023-09-17 07:10:08 +02:00
|
|
|
f.render_widget(chart, size)
|
2024-03-18 03:36:38 +01:00
|
|
|
})?;
|
2022-12-28 03:55:37 +01:00
|
|
|
}
|
|
|
|
|
2023-09-17 07:10:08 +02:00
|
|
|
while event::poll(Duration::from_millis(0))? { // process all enqueued events
|
|
|
|
let event = event::read()?;
|
2022-12-28 03:55:37 +01:00
|
|
|
|
2023-09-17 07:10:08 +02:00
|
|
|
if self.process_events(event.clone())? { return Ok(()); }
|
2023-09-18 00:57:16 +02:00
|
|
|
self.current_display_mut().handle(event);
|
2023-06-29 16:09:36 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-17 07:10:08 +02:00
|
|
|
}
|
2023-06-29 16:09:36 +02:00
|
|
|
|
2023-09-18 00:57:16 +02:00
|
|
|
fn current_display_mut(&mut self) -> &mut dyn DisplayMode {
|
2023-09-17 07:10:08 +02:00
|
|
|
match self.mode {
|
|
|
|
CurrentDisplayMode::Oscilloscope => &mut self.oscilloscope as &mut dyn DisplayMode,
|
|
|
|
CurrentDisplayMode::Vectorscope => &mut self.vectorscope as &mut dyn DisplayMode,
|
|
|
|
CurrentDisplayMode::Spectroscope => &mut self.spectroscope as &mut dyn DisplayMode,
|
2022-12-28 03:55:37 +01:00
|
|
|
}
|
2023-09-17 07:10:08 +02:00
|
|
|
}
|
2022-12-28 03:55:37 +01:00
|
|
|
|
2023-09-18 00:57:16 +02:00
|
|
|
fn current_display(&self) -> &dyn DisplayMode {
|
|
|
|
match self.mode {
|
|
|
|
CurrentDisplayMode::Oscilloscope => &self.oscilloscope as &dyn DisplayMode,
|
|
|
|
CurrentDisplayMode::Vectorscope => &self.vectorscope as &dyn DisplayMode,
|
|
|
|
CurrentDisplayMode::Spectroscope => &self.spectroscope as &dyn DisplayMode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 07:10:08 +02:00
|
|
|
fn process_events(&mut self, event: Event) -> Result<bool, io::Error> {
|
|
|
|
let mut quit = false;
|
|
|
|
if let Event::Key(key) = event {
|
|
|
|
if let KeyModifiers::CONTROL = key.modifiers {
|
|
|
|
match key.code { // mimic other programs shortcuts to quit, for user friendlyness
|
|
|
|
KeyCode::Char('c') | KeyCode::Char('q') | KeyCode::Char('w') => quit = true,
|
|
|
|
_ => {},
|
|
|
|
}
|
2023-01-02 01:18:07 +01:00
|
|
|
}
|
2023-09-17 07:10:08 +02:00
|
|
|
let magnitude = match key.modifiers {
|
|
|
|
KeyModifiers::SHIFT => 10.0,
|
|
|
|
KeyModifiers::CONTROL => 5.0,
|
|
|
|
KeyModifiers::ALT => 0.2,
|
|
|
|
_ => 1.0,
|
|
|
|
};
|
|
|
|
match key.code {
|
2024-03-18 06:12:34 +01:00
|
|
|
KeyCode::Up => update_value_f(&mut self.graph.scale, 0.01, magnitude, 0.0..10.0), // inverted to act as zoom
|
|
|
|
KeyCode::Down => update_value_f(&mut self.graph.scale, -0.01, magnitude, 0.0..10.0), // inverted to act as zoom
|
2023-09-21 03:27:43 +02:00
|
|
|
KeyCode::Right => update_value_i(&mut self.graph.samples, true, 25, magnitude, 0..self.graph.width*2),
|
|
|
|
KeyCode::Left => update_value_i(&mut self.graph.samples, false, 25, magnitude, 0..self.graph.width*2),
|
2023-09-17 07:10:08 +02:00
|
|
|
KeyCode::Char('q') => quit = true,
|
2023-09-21 03:50:48 +02:00
|
|
|
KeyCode::Char(' ') => self.graph.pause = !self.graph.pause,
|
2023-09-17 07:10:08 +02:00
|
|
|
KeyCode::Char('s') => self.graph.scatter = !self.graph.scatter,
|
|
|
|
KeyCode::Char('h') => self.graph.show_ui = !self.graph.show_ui,
|
|
|
|
KeyCode::Char('r') => self.graph.references = !self.graph.references,
|
|
|
|
KeyCode::Tab => { // switch modes
|
|
|
|
match self.mode {
|
|
|
|
CurrentDisplayMode::Oscilloscope => self.mode = CurrentDisplayMode::Vectorscope,
|
|
|
|
CurrentDisplayMode::Vectorscope => self.mode = CurrentDisplayMode::Spectroscope,
|
|
|
|
CurrentDisplayMode::Spectroscope => self.mode = CurrentDisplayMode::Oscilloscope,
|
|
|
|
}
|
|
|
|
},
|
2023-09-18 01:40:04 +02:00
|
|
|
KeyCode::Esc => {
|
|
|
|
self.graph.samples = self.graph.width;
|
2024-03-18 03:36:38 +01:00
|
|
|
self.graph.scale = 1.;
|
2023-09-18 01:40:04 +02:00
|
|
|
},
|
2023-09-17 07:10:08 +02:00
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(quit)
|
2022-12-28 03:55:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 00:57:16 +02:00
|
|
|
// TODO can these be removed or merged somewhere else?
|
|
|
|
|
2023-09-17 07:10:08 +02:00
|
|
|
pub fn update_value_f(val: &mut f64, base: f64, magnitude: f64, range: Range<f64>) {
|
|
|
|
let delta = base * magnitude;
|
|
|
|
if *val + delta > range.end {
|
|
|
|
*val = range.end
|
|
|
|
} else if *val + delta < range.start {
|
|
|
|
*val = range.start
|
|
|
|
} else {
|
|
|
|
*val += delta;
|
2023-01-15 23:30:33 +01:00
|
|
|
}
|
2023-09-17 07:10:08 +02:00
|
|
|
}
|
2023-01-15 23:30:33 +01:00
|
|
|
|
2023-09-17 07:10:08 +02:00
|
|
|
pub fn update_value_i(val: &mut u32, inc: bool, base: u32, magnitude: f64, range: Range<u32>) {
|
|
|
|
let delta = (base as f64 * magnitude) as u32;
|
|
|
|
if inc {
|
|
|
|
if range.end - delta < *val {
|
|
|
|
*val = range.end
|
2023-01-15 23:30:33 +01:00
|
|
|
} else {
|
2023-09-17 07:10:08 +02:00
|
|
|
*val += delta
|
2023-01-15 23:30:33 +01:00
|
|
|
}
|
2023-09-17 07:10:08 +02:00
|
|
|
} else if range.start + delta > *val {
|
|
|
|
*val = range.start
|
|
|
|
} else {
|
|
|
|
*val -= delta
|
2023-01-15 23:30:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 01:39:37 +02:00
|
|
|
fn make_header<'a>(cfg: &GraphConfig, module_header: &'a str, kind_o_scope: &'static str, fps: usize, pause: bool) -> Table<'a> {
|
2023-01-05 00:51:05 +01:00
|
|
|
Table::new(
|
|
|
|
vec![
|
|
|
|
Row::new(
|
|
|
|
vec![
|
2024-03-18 03:36:38 +01:00
|
|
|
Cell::from(format!("{}::scope-tui", kind_o_scope)).style(Style::default().fg(*cfg.palette.first().expect("empty palette?")).add_modifier(Modifier::BOLD)),
|
2023-09-17 07:10:08 +02:00
|
|
|
Cell::from(module_header),
|
2024-03-18 03:36:38 +01:00
|
|
|
Cell::from(format!("-{:.2}x+", cfg.scale)),
|
2023-09-17 07:10:08 +02:00
|
|
|
Cell::from(format!("{}/{} spf", cfg.samples, cfg.width)),
|
|
|
|
Cell::from(format!("{}fps", fps)),
|
|
|
|
Cell::from(if cfg.scatter { "***" } else { "---" }),
|
|
|
|
Cell::from(if pause { "||" } else { "|>" }),
|
2023-01-05 00:51:05 +01:00
|
|
|
]
|
|
|
|
)
|
2024-02-09 20:19:35 +01:00
|
|
|
],
|
|
|
|
vec![
|
|
|
|
Constraint::Percentage(35),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
Constraint::Percentage(7),
|
|
|
|
Constraint::Percentage(13),
|
|
|
|
Constraint::Percentage(6),
|
|
|
|
Constraint::Percentage(6),
|
|
|
|
Constraint::Percentage(6)
|
2023-01-05 00:51:05 +01:00
|
|
|
]
|
|
|
|
)
|
2023-09-17 07:10:08 +02:00
|
|
|
.style(Style::default().fg(cfg.labels_color))
|
2023-01-05 00:51:05 +01:00
|
|
|
}
|