diff --git a/src/app.rs b/src/app.rs index 88872c1..6a8a22e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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, scale: u32, width: u32, @@ -158,9 +157,8 @@ impl From::<&crate::Args> for App { let cfg = AppConfig { title: "TUI Oscilloscope -- ".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, diff --git a/src/main.rs b/src/main.rs index 37da1e1..8dcce78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,8 +154,7 @@ fn run_app(args: Args, terminal: &mut Terminal) -> 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,33 +165,49 @@ fn run_app(args: Args, terminal: &mut Terminal) -> Result<(), io }, } - let mut datasets = vec![]; - if !pause { - if app.vectorscope() { - merged = fmt.vectorscope(&mut buffer); - } else { - (left, right) = fmt.oscilloscope(&mut buffer); + 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 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 = 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; if last_poll.elapsed().as_secs() >= 1 { diff --git a/src/parser.rs b/src/parser.rs index c0abfa8..63c5f61 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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>; } 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> { + 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 }