mirror of
https://github.com/alemidev/scope-tui.git
synced 2024-11-14 10:49:20 +01:00
chore: generalized channel number parsing
This commit is contained in:
parent
a337852dac
commit
59b736bf12
3 changed files with 44 additions and 70 deletions
|
@ -40,9 +40,8 @@ impl Default for ChartReferences {
|
||||||
|
|
||||||
pub struct AppConfig {
|
pub struct AppConfig {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub primary_color: Color,
|
|
||||||
pub secondary_color: Color,
|
|
||||||
pub axis_color: Color,
|
pub axis_color: Color,
|
||||||
|
pub palette: Vec<Color>,
|
||||||
|
|
||||||
scale: u32,
|
scale: u32,
|
||||||
width: u32,
|
width: u32,
|
||||||
|
@ -158,9 +157,8 @@ impl From::<&crate::Args> for App {
|
||||||
|
|
||||||
let cfg = AppConfig {
|
let cfg = AppConfig {
|
||||||
title: "TUI Oscilloscope -- <me@alemi.dev>".into(),
|
title: "TUI Oscilloscope -- <me@alemi.dev>".into(),
|
||||||
primary_color: Color::Red,
|
|
||||||
secondary_color: Color::Yellow,
|
|
||||||
axis_color: Color::DarkGray,
|
axis_color: Color::DarkGray,
|
||||||
|
palette: vec![Color::Red, Color::Yellow],
|
||||||
scale: args.range,
|
scale: args.range,
|
||||||
width: args.buffer / 4, // TODO It's 4 because 2 channels and 2 bytes per sample!
|
width: args.buffer / 4, // TODO It's 4 because 2 channels and 2 bytes per sample!
|
||||||
vectorscope: args.vectorscope,
|
vectorscope: args.vectorscope,
|
||||||
|
|
53
src/main.rs
53
src/main.rs
|
@ -154,8 +154,7 @@ fn run_app<T : Backend>(args: Args, terminal: &mut Terminal<T>) -> Result<(), io
|
||||||
let mut fps = 0;
|
let mut fps = 0;
|
||||||
let mut framerate = 0;
|
let mut framerate = 0;
|
||||||
let mut last_poll = Instant::now();
|
let mut last_poll = Instant::now();
|
||||||
let (mut left, mut right) = (vec![], vec![]);
|
let mut channels = vec![];
|
||||||
let mut merged = vec![];
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match s.read(&mut buffer) {
|
match s.read(&mut buffer) {
|
||||||
|
@ -166,33 +165,49 @@ fn run_app<T : Backend>(args: Args, terminal: &mut Terminal<T>) -> Result<(), io
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut datasets = vec![];
|
|
||||||
|
|
||||||
if !pause {
|
if !pause {
|
||||||
if app.vectorscope() {
|
channels = fmt.oscilloscope(&mut buffer, 2);
|
||||||
merged = fmt.vectorscope(&mut buffer);
|
}
|
||||||
} else {
|
|
||||||
(left, right) = fmt.oscilloscope(&mut buffer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut measures;
|
||||||
|
|
||||||
|
if app.vectorscope() {
|
||||||
|
measures = vec![];
|
||||||
|
for chunk in channels.chunks(2) {
|
||||||
|
let mut tmp = vec![];
|
||||||
|
for i in 0..chunk[0].len() {
|
||||||
|
tmp.push((chunk[0][i] as f64, chunk[1][i] as f64));
|
||||||
|
}
|
||||||
|
let pivot = tmp.len() / 2;
|
||||||
|
measures.push(tmp[..pivot].to_vec());
|
||||||
|
measures.push(tmp[pivot..].to_vec());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
measures = vec![vec![]; channels.len()];
|
||||||
|
for i in 0..channels[0].len() {
|
||||||
|
for j in 0..channels.len() {
|
||||||
|
measures[j].push((i as f64, channels[j][i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut datasets = vec![];
|
||||||
|
|
||||||
if app.cfg.references {
|
if app.cfg.references {
|
||||||
// for reference in app.references() {
|
|
||||||
// datasets.push(reference);
|
|
||||||
// }
|
|
||||||
datasets.push(data_set("", &app.references.x, app.cfg.marker_type, GraphType::Line, app.cfg.axis_color));
|
datasets.push(data_set("", &app.references.x, app.cfg.marker_type, GraphType::Line, app.cfg.axis_color));
|
||||||
datasets.push(data_set("", &app.references.y, app.cfg.marker_type, GraphType::Line, app.cfg.axis_color));
|
datasets.push(data_set("", &app.references.y, app.cfg.marker_type, GraphType::Line, app.cfg.axis_color));
|
||||||
}
|
}
|
||||||
|
|
||||||
if app.vectorscope() {
|
let ds_names = if app.vectorscope() { vec!["2", "1"] } else { vec!["R", "L"] };
|
||||||
let pivot = merged.len() / 2;
|
let palette : Vec<Color> = app.cfg.palette.iter().rev().map(|x| x.clone()).collect();
|
||||||
datasets.push(data_set("1", &merged[..pivot], app.cfg.marker_type, app.graph_type(), app.cfg.secondary_color));
|
|
||||||
datasets.push(data_set("2", &merged[pivot..], app.cfg.marker_type, app.graph_type(), app.cfg.primary_color));
|
for (i, ds) in measures.iter().rev().enumerate() {
|
||||||
} else {
|
datasets.push(data_set(ds_names[i], ds, app.cfg.marker_type, app.graph_type(), palette[i % palette.len()]));
|
||||||
datasets.push(data_set("R", &right, app.cfg.marker_type, app.graph_type(), app.cfg.secondary_color));
|
|
||||||
datasets.push(data_set("L", &left, app.cfg.marker_type, app.graph_type(), app.cfg.primary_color));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fps += 1;
|
fps += 1;
|
||||||
|
|
||||||
if last_poll.elapsed().as_secs() >= 1 {
|
if last_poll.elapsed().as_secs() >= 1 {
|
||||||
|
|
|
@ -8,59 +8,20 @@
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub trait SampleParser {
|
pub trait SampleParser {
|
||||||
fn oscilloscope(&self, data: &mut [u8]) -> (Vec<(f64, f64)>, Vec<(f64, f64)>);
|
fn oscilloscope(&self, data: &mut [u8], channels: u32) -> Vec<Vec<f64>>;
|
||||||
fn vectorscope (&self, data: &mut [u8]) -> Vec<(f64, f64)>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Signed16PCM {}
|
pub struct Signed16PCM {}
|
||||||
|
|
||||||
/// TODO these are kinda inefficient, can they be faster?
|
/// TODO these are kinda inefficient, can they be faster?
|
||||||
impl SampleParser for Signed16PCM {
|
impl SampleParser for Signed16PCM {
|
||||||
fn oscilloscope(&self, data: &mut [u8]) -> (Vec<(f64, f64)>, Vec<(f64, f64)>) {
|
fn oscilloscope(&self, data: &mut [u8], channels: u32) -> Vec<Vec<f64>> {
|
||||||
let mut left = Vec::new(); // TODO does left really come first?
|
let mut out = vec![vec![]; channels as usize];
|
||||||
let mut right = Vec::new();
|
let mut channel = 0;
|
||||||
let mut buf : i16 = 0;
|
for chunk in data.chunks(2) {
|
||||||
let mut count : f64 = 0.0;
|
let buf = chunk[0] as i16 | (chunk[1] as i16) << 8;
|
||||||
let mut flip = false;
|
out[channel].push(buf as f64);
|
||||||
let mut side = false;
|
channel = (channel + 1 ) % channels as usize;
|
||||||
for sample in data {
|
|
||||||
if flip {
|
|
||||||
buf |= (*sample as i16) << 8;
|
|
||||||
if side {
|
|
||||||
left.push((count, buf as f64));
|
|
||||||
} else {
|
|
||||||
right.push((count, buf as f64));
|
|
||||||
count += 1.0;
|
|
||||||
}
|
|
||||||
buf = 0;
|
|
||||||
side = !side;
|
|
||||||
} else {
|
|
||||||
buf |= *sample as i16;
|
|
||||||
}
|
|
||||||
flip = !flip;
|
|
||||||
}
|
|
||||||
(left, right)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn vectorscope(&self, data: &mut [u8]) -> Vec<(f64, f64)> {
|
|
||||||
let mut out = Vec::new(); // TODO does left really come first?
|
|
||||||
let mut buf : i16 = 0;
|
|
||||||
let mut flip = false;
|
|
||||||
let mut point = None;
|
|
||||||
for sample in data {
|
|
||||||
if flip {
|
|
||||||
buf |= (*sample as i16) << 8;
|
|
||||||
if point.is_none() {
|
|
||||||
point = Some(buf as f64);
|
|
||||||
} else {
|
|
||||||
out.push((point.unwrap(), buf as f64));
|
|
||||||
point = None;
|
|
||||||
}
|
|
||||||
buf = 0;
|
|
||||||
} else {
|
|
||||||
buf |= *sample as i16;
|
|
||||||
}
|
|
||||||
flip = !flip;
|
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue