1
0
Fork 0
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:
əlemi 2022-12-27 19:01:48 +01:00
parent a337852dac
commit 59b736bf12
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 44 additions and 70 deletions

View file

@ -40,9 +40,8 @@ impl Default for ChartReferences {
pub struct AppConfig {
pub title: String,
pub primary_color: Color,
pub secondary_color: Color,
pub axis_color: Color,
pub palette: Vec<Color>,
scale: u32,
width: u32,
@ -158,9 +157,8 @@ impl From::<&crate::Args> for App {
let cfg = AppConfig {
title: "TUI Oscilloscope -- <me@alemi.dev>".into(),
primary_color: Color::Red,
secondary_color: Color::Yellow,
axis_color: Color::DarkGray,
palette: vec![Color::Red, Color::Yellow],
scale: args.range,
width: args.buffer / 4, // TODO It's 4 because 2 channels and 2 bytes per sample!
vectorscope: args.vectorscope,

View file

@ -154,8 +154,7 @@ fn run_app<T : Backend>(args: Args, terminal: &mut Terminal<T>) -> Result<(), io
let mut fps = 0;
let mut framerate = 0;
let mut last_poll = Instant::now();
let (mut left, mut right) = (vec![], vec![]);
let mut merged = vec![];
let mut channels = vec![];
loop {
match s.read(&mut buffer) {
@ -166,31 +165,47 @@ fn run_app<T : Backend>(args: Args, terminal: &mut Terminal<T>) -> Result<(), io
},
}
if !pause {
channels = fmt.oscilloscope(&mut buffer, 2);
}
}
}
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 !pause {
if app.vectorscope() {
merged = fmt.vectorscope(&mut buffer);
} else {
(left, right) = fmt.oscilloscope(&mut buffer);
}
}
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.y, app.cfg.marker_type, GraphType::Line, app.cfg.axis_color));
}
if app.vectorscope() {
let pivot = merged.len() / 2;
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));
} else {
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));
let ds_names = if app.vectorscope() { vec!["2", "1"] } else { vec!["R", "L"] };
let palette : Vec<Color> = app.cfg.palette.iter().rev().map(|x| x.clone()).collect();
for (i, ds) in measures.iter().rev().enumerate() {
datasets.push(data_set(ds_names[i], ds, app.cfg.marker_type, app.graph_type(), palette[i % palette.len()]));
}
fps += 1;

View file

@ -8,59 +8,20 @@
// }
pub trait SampleParser {
fn oscilloscope(&self, data: &mut [u8]) -> (Vec<(f64, f64)>, Vec<(f64, f64)>);
fn vectorscope (&self, data: &mut [u8]) -> Vec<(f64, f64)>;
fn oscilloscope(&self, data: &mut [u8], channels: u32) -> Vec<Vec<f64>>;
}
pub struct Signed16PCM {}
/// TODO these are kinda inefficient, can they be faster?
impl SampleParser for Signed16PCM {
fn oscilloscope(&self, data: &mut [u8]) -> (Vec<(f64, f64)>, Vec<(f64, f64)>) {
let mut left = Vec::new(); // TODO does left really come first?
let mut right = Vec::new();
let mut buf : i16 = 0;
let mut count : f64 = 0.0;
let mut flip = false;
let mut side = false;
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;
fn oscilloscope(&self, data: &mut [u8], channels: u32) -> Vec<Vec<f64>> {
let mut out = vec![vec![]; channels as usize];
let mut channel = 0;
for chunk in data.chunks(2) {
let buf = chunk[0] as i16 | (chunk[1] as i16) << 8;
out[channel].push(buf as f64);
channel = (channel + 1 ) % channels as usize;
}
out
}