Finishes the scan

This commit is contained in:
Dandaedre 2024-09-24 16:32:59 +02:00
parent c88ef356b1
commit 8fcf274965
5 changed files with 254 additions and 141 deletions

View File

@ -7,6 +7,7 @@ edition = "2021"
# byteorder = "1.5.0"
clap = { version = "4.5.17", features = ["derive"] }
color-eyre = "0.6.3"
human-panic = "2.0.1"
# csv = "1.3.0"
memchr = "2.7.4"
once_cell = "1.19.0"

View File

@ -11,15 +11,14 @@ mod ui;
mod ui_struct;
use color_eyre::Result;
// use human_panic::setup_panic;
use structs::TinyError;
use ui_struct::AppInfo;
fn main() -> Result<(), TinyError> {
// let args = Args::parse();
// scan()
color_eyre::install()?;
let terminal = ratatui::init();
// setup_panic!();
let ports = serialport::available_ports()?;
let app_r = AppInfo::new(ports).run(terminal);

View File

@ -45,7 +45,7 @@ static EOF: OnceLock<memmem::Finder<'_>> = OnceLock::new();
/// Scan a specific range and returns the scan result
pub(crate) fn scan_with_progress(
progress: Arc<Mutex<f64>>,
progress: Arc<Mutex<usize>>,
scan_info: Arc<Mutex<String>>,
scan_message: Arc<Mutex<String>>,
port: String,
@ -59,34 +59,31 @@ pub(crate) fn scan_with_progress(
.open()
.expect("Failed to open port");
*scan_message.lock().unwrap() = "Initialisation...".to_string();
*progress.lock().unwrap() = 5.;
*progress.lock().unwrap() = 5;
initialize(&mut port);
*progress.lock().unwrap() = 15.;
*progress.lock().unwrap() = 15;
*scan_message.lock().unwrap() = "Acquiring version & info".to_string();
let v = send_cmd(&mut port, "version\r", SHORT_WAIT);
let i = send_cmd(&mut port, "info\r", SHORT_WAIT);
*scan_info.lock().unwrap() = format!("{}{}", v, i);
*progress.lock().unwrap() = 16.;
*progress.lock().unwrap() = 16;
*scan_message.lock().unwrap() = "Setting zero...".to_string();
send_cmd(&mut port, &format!("zero {}\r", OFFSET), SHORT_WAIT);
// ("{}", send_cmd(&mut port, "leveloffset\r", SHORT_WAIT));
// ("{}", send_cmd(&mut port, "deviceid\r", SHORT_WAIT));
// ("{}", send_cmd(&mut port, "usart_cfg\r", SHORT_WAIT));
*progress.lock().unwrap() = 17.;
*progress.lock().unwrap() = 17;
*scan_message.lock().unwrap() = "Setting up scan & mode".to_string();
send_cmd(&mut port, "marker\r", SHORT_WAIT);
send_cmd(&mut port, "pause\r", SHORT_WAIT);
send_cmd(&mut port, "mode high input\r", SHORT_WAIT);
*progress.lock().unwrap() = 18.;
*progress.lock().unwrap() = 18;
*scan_message.lock().unwrap() = "Setting up sweep".to_string();
send_cmd(&mut port, &format!("sweep start {}\r", START), SHORT_WAIT);
send_cmd(&mut port, &format!("sweep stop {}\r", STOP), SHORT_WAIT);
*progress.lock().unwrap() = 20.;
*progress.lock().unwrap() = 20;
*scan_message.lock().unwrap() = "Starting scan...".to_string();
send_cmd(&mut port, "sweep\r", SHORT_WAIT);
send_cmd(
@ -95,14 +92,14 @@ pub(crate) fn scan_with_progress(
TIMEOUT,
);
*progress.lock().unwrap() = 22.;
*progress.lock().unwrap() = 22;
scanraw(&mut port, progress.clone(), start, stop, points)?;
*scan_message.lock().unwrap() = "Scan finished".to_string();
send_cmd(&mut port, "resume\r", SHORT_WAIT);
*progress.lock().unwrap() = 100.;
wait_sync(LONG_WAIT);
*progress.lock().unwrap() = 100;
Ok(())
}
@ -122,7 +119,7 @@ fn wait_sync(duration: u64) {
/// Scan raw frequencies
fn scanraw(
port: &mut Box<dyn SerialPort>,
progress: Arc<Mutex<f64>>,
progress: Arc<Mutex<usize>>,
start: usize,
stop: usize,
points: usize,
@ -156,6 +153,29 @@ fn read_utf8(port: &mut Box<dyn SerialPort>) -> String {
}
/// Reads the serial port until it reaches EOF
fn read_port_with_progress(
port: &mut Box<dyn SerialPort>,
expected: usize,
progress: Arc<Mutex<usize>>,
) -> Vec<u8> {
let mut serial_buf: Vec<u8> = vec![0; 64];
let mut res = Vec::new();
let finder = EOF.get_or_init(|| memmem::Finder::new("ch>"));
while let Ok(r) = port.read(serial_buf.as_mut_slice()) {
*progress.lock().unwrap() = (100usize.saturating_sub(
(80 * (expected.saturating_sub(res.len())).clamp(0, expected)) / expected,
))
.clamp(20, 99);
if let Some(pos) = finder.find(&serial_buf) {
res.extend_from_slice(&serial_buf[..pos]);
break;
}
res.extend_from_slice(&serial_buf[..r]);
}
res
}
/// Read port without updating progress
fn read_port(port: &mut Box<dyn SerialPort>) -> Vec<u8> {
let mut serial_buf: Vec<u8> = vec![0; 256];
let mut res = Vec::new();
@ -176,16 +196,16 @@ fn read_port(port: &mut Box<dyn SerialPort>) -> Vec<u8> {
/// Returns a struct summerizing the information processed
fn read_scanraw(
port: &mut Box<dyn SerialPort>,
progress: Arc<Mutex<f64>>,
progress: Arc<Mutex<usize>>,
points: usize,
) -> Result<(), TinyError> {
let v = read_port(port);
let v = read_port_with_progress(port, points * 3 + 6, progress);
// Find the { marking the beggining
parse_data(v, progress, points)
parse_data(v, points)
}
/// A utility function used for parsing the raw data of a scan
pub fn parse_data(v: Vec<u8>, progress: Arc<Mutex<f64>>, points: usize) -> Result<(), TinyError> {
pub fn parse_data(v: Vec<u8>, points: usize) -> Result<(), TinyError> {
let mut values: Vec<u16> = Vec::with_capacity(points);
let mut out = BufWriter::new(File::create("test.csv")?);
if let Some(begining) = memchr::memchr(b'{', &v) {
@ -216,12 +236,3 @@ pub fn parse_data(v: Vec<u8>, progress: Arc<Mutex<f64>>, points: usize) -> Resul
fn u16_to_f64(v: u16) -> f64 {
(v as f64) / 32. - OFFSET as f64
}
// /// The result of a scan
// pub struct ScanResult {
// start: usize,
// stop: usize,
// points: usize,
// status: ScanStatus,
// data: Vec<f64>,
// }

291
src/ui.rs
View File

@ -2,24 +2,25 @@
#![allow(unused_imports)]
use clap::builder::Resettable;
use color_eyre::{owo_colors::OwoColorize, Section};
use core::time;
use core::{panic, time};
use ratatui::{
buffer::Buffer,
crossterm::{
event::{self, Event, KeyCode, KeyEvent, KeyEventKind},
style::Print,
},
layout::{Constraint, Direction, Layout, Rect},
layout::{Alignment, Constraint, Direction, Layout, Rect},
prelude::BlockExt,
style::{
palette::tailwind::{BLUE, GREEN, SLATE},
Color, Modifier, Style, Styled, Stylize,
},
symbols,
symbols::{self, Marker},
text::Line,
widgets::{
Block, Borders, HighlightSpacing, LineGauge, List, ListDirection, ListItem, ListState,
Padding, Paragraph, ScrollDirection, StatefulWidget, Widget, Wrap,
block::Title, Axis, Block, Borders, Chart, Dataset, GraphType, HighlightSpacing, LineGauge,
List, ListDirection, ListItem, ListState, Padding, Paragraph, ScrollDirection,
StatefulWidget, Widget, Wrap,
},
DefaultTerminal, Frame,
};
@ -59,7 +60,7 @@ impl<'a> AppInfo<'a> {
start_frequency: UIUsize::new(240, 10, 960),
stop_frequency: UIUsize::new(960, 10, 960),
points: UIUsize::new(10000, 100, 1000000),
progress: Arc::new(Mutex::new(0.)),
progress: Arc::new(Mutex::new(0)),
scan_info: Arc::new(Mutex::new(String::new())),
scan_message: Arc::new(Mutex::new(String::new())),
}
@ -91,9 +92,9 @@ impl<'a> AppInfo<'a> {
match self.flow_state {
AppState::InitialConfig(_) => self.draw_config(frame),
AppState::FinalConfig(_) => self.draw_final_config(frame),
// AppState::Popup => todo!(),
AppState::Finished => self.draw_progress(frame),
_ => todo!(),
AppState::EndWindow => self.draw_end_window(frame),
// _ => unimplemented!(),
}
}
@ -128,6 +129,11 @@ impl<'a> AppInfo<'a> {
self.update_color()
}
/// Initialisation of all the final ui elements
pub(crate) fn init_end(&self) -> Result<(), TinyUIError> {
Ok(())
}
/// Draw the final configuration labels
fn draw_final_config(&mut self, frame: &mut Frame<'_>) -> Result<(), TinyUIError> {
let [title_area, main_area, _, status_area] = Layout::vertical([
@ -209,60 +215,6 @@ impl<'a> AppInfo<'a> {
Ok(())
}
/// Draw the progress, updates as the progress is being done
fn draw_progress(&mut self, frame: &mut Frame<'_>) -> Result<(), TinyUIError> {
// let title = title_block("Blue / red only background");
let [_, main_s, _] = Layout::horizontal([
Constraint::Min(7),
Constraint::Percentage(90),
Constraint::Min(7),
])
.areas(frame.area());
let [title_area, _, main_area, _, progress_area] = Layout::vertical([
Constraint::Length(3),
// Constraint::Length(1),
Constraint::Length(1),
// Constraint::Length(1),
Constraint::Min(10),
Constraint::Length(1),
Constraint::Length(1),
])
.areas(main_s);
let (pb, status, message) = {
let status = self.scan_message.try_lock().unwrap();
let status = Paragraph::new(status.clone())
.block(Block::bordered().title("Operation"))
.style(SELECTED_COLOR);
let message = self.scan_info.try_lock().unwrap();
let message = Paragraph::new(message.clone())
.block(Block::bordered().title("TinySA Info"))
.style(SELECTED_COLOR);
let progress = self.progress.try_lock().unwrap();
let pb = self
.menu_items
.progress_bar
.take()
.ok_or(TinyUIError::UninitializedItemList(
"ProgressBar",
line!(),
file!(),
))?
.ratio(*progress / 100.);
(pb, status, message)
};
// let self= pb.clone
frame.render_widget(pb.clone(), progress_area);
frame.render_widget(status, title_area);
frame.render_widget(message, main_area);
// frame.render_widget(Paragraph::new(format!("{:?}", Instant::now())), main_area);
// thread::sleep(Duration::from_millis(100));
self.menu_items.progress_bar = Some(pb);
// TODO: If progress = 100 move to final step and show S / enter to save G for graph
Ok(())
}
/// Draw the config info
fn draw_config(&mut self, frame: &mut Frame) -> Result<(), TinyUIError> {
let [title_area, main_area, status_area] = Layout::vertical([
@ -296,6 +248,180 @@ impl<'a> AppInfo<'a> {
Ok(())
}
/// Draw the progress, updates as the progress is being done
fn draw_progress(&mut self, frame: &mut Frame<'_>) -> Result<(), TinyUIError> {
let [_, main_s, _] = Layout::horizontal([
Constraint::Min(7),
Constraint::Percentage(90),
Constraint::Min(7),
])
.areas(frame.area());
let [title_area, progress_area, main_area, _, _] = Layout::vertical([
Constraint::Length(3),
Constraint::Length(1),
Constraint::Min(10),
Constraint::Length(1),
Constraint::Length(1),
])
.areas(main_s);
let (pb, status, message) = {
let status = self.scan_message.try_lock().unwrap();
let status = Paragraph::new(status.clone())
.block(Block::bordered().title("Operation"))
.style(SELECTED_COLOR);
let message = self.scan_info.try_lock().unwrap();
let message = Paragraph::new(message.clone())
.block(Block::bordered().title("TinySA Info"))
.style(SELECTED_COLOR);
let progress = self.progress.try_lock().unwrap();
let pb = self
.menu_items
.progress_bar
.take()
.ok_or(TinyUIError::UninitializedItemList(
"ProgressBar",
line!(),
file!(),
))?
.ratio(*progress as f64 / 100.);
(pb, status, message)
};
frame.render_widget(pb.clone(), progress_area);
frame.render_widget(status, title_area);
frame.render_widget(message, main_area);
self.menu_items.progress_bar = Some(pb);
if *self.progress.lock().unwrap() >= 100 {
self.next_step()?;
}
Ok(())
}
/// Draw the last confirmation window
fn draw_end_window(&mut self, frame: &mut Frame) -> Result<(), TinyUIError> {
let [_, main_s, _] = Layout::horizontal([
Constraint::Min(7),
Constraint::Percentage(90),
Constraint::Min(7),
])
.areas(frame.area());
let [title_area, main_area, graph_area, _, footer_area] = Layout::vertical([
Constraint::Length(3),
Constraint::Length(4),
Constraint::Min(10),
Constraint::Length(1),
Constraint::Length(1),
])
.areas(main_s);
let status = Paragraph::new(format!(
"The scan ({}Mhz - {}Mhz) of {} points finished sucessfuly",
self.start_frequency.int_to_usize(),
self.stop_frequency.int_to_usize(),
self.points.int_to_usize()
))
.block(Block::bordered().title("Status"))
.style(SELECTED_COLOR);
// let dataset = Dataset::default()
// .marker(symbols::Marker::HalfBlock)
// .style(Style::new().fg(Color::Blue))
// .graph_type(GraphType::Bar)
// // a bell curve
// .data(&[
// (250., -2.9),
// (260., -13.5),
// (270., -41.1),
// (280., -80.1),
// (390., -100.0),
// (500., -80.1),
// (610., -41.1),
// (720., -13.5),
// (830., -2.9),
// (930., -2.9),
// ]);
// let chart = Chart::new(vec![dataset])
// .block(
// Block::bordered().title(
// Title::default()
// .content("Bar chart".cyan().bold())
// .alignment(Alignment::Center),
// ),
// )
// .x_axis(
// Axis::default()
// .style(Style::default().gray())
// .bounds([
// self.start_frequency.mhz_to_usize() as f64,
// self.stop_frequency.mhz_to_usize() as f64,
// ])
// .labels([
// self.start_frequency.int_to_string().bold(),
// format!(
// "{}",
// (self.stop_frequency.int_to_usize()
// - self.start_frequency.int_to_usize() / 2)
// )
// .into(),
// self.stop_frequency.int_to_string().bold(),
// ]),
// )
// .y_axis(
// Axis::default()
// .style(Style::default().gray())
// .bounds([-128.0, 0.])
// .labels(["-128".bold(), "-64".into(), "0".bold()]),
// );
// // .hidden_legend_constraints((Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)));
let datasets = vec![Dataset::default()
.name("Heavy")
.marker(Marker::Dot)
.graph_type(GraphType::Scatter)
.style(Style::new().yellow())
.data(&[
(1965., -82.),
(1967., -54.),
(1981., -65.),
(1989., -30.),
(1997., -10.),
(2004., -11.),
(2014., -45.),
(2016., -79.),
(2018., -15.),
])];
let chart = Chart::new(datasets)
.block(
Block::bordered().title(
Title::default()
.content("Scatter chart".cyan().bold())
.alignment(Alignment::Center),
),
)
.x_axis(
Axis::default()
.title("Year")
.bounds([1960., 2020.])
.style(Style::default().fg(Color::Gray))
.labels(["1960", "1990", "2020"]),
)
.y_axis(
Axis::default()
.title("Cost")
.bounds([-128., 0.])
.style(Style::default().fg(Color::Gray))
.labels(["0", "37 500", "75 000"]),
)
.hidden_legend_constraints((Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)));
frame.render_widget(chart, graph_area);
let footer =
Paragraph::new("Press ↲ to save the data as a csv or q to exit without saving.")
.centered();
frame.render_widget(status, title_area);
frame.render_widget(footer, footer_area);
Ok(())
}
/// Handle keys while in the first menu
fn handle_start_keys(&mut self, key: KeyEvent) -> Result<(), TinyUIError> {
if key.kind != KeyEventKind::Press {
@ -307,22 +433,10 @@ impl<'a> AppInfo<'a> {
KeyCode::Char('k') | KeyCode::Up => self.change_cursor_position(InputDirection::Up),
KeyCode::Char('h') | KeyCode::Left => self.next_menu(InputDirection::Left)?,
KeyCode::Char('l') | KeyCode::Right => self.next_menu(InputDirection::Right)?,
v @ (KeyCode::Char('0')
| KeyCode::Char('1')
| KeyCode::Char('2')
| KeyCode::Char('3')
| KeyCode::Char('4')
| KeyCode::Char('5')
| KeyCode::Char('6')
| KeyCode::Char('7')
| KeyCode::Char('8')
| KeyCode::Char('a')) => {
self.status_message = format!("{:?}", v);
} // KeyCode::Char('g') | KeyCode::Home => self.select_first(),
// KeyCode::Char('G') | KeyCode::End => self.select_last(),
KeyCode::Enter => {
self.next_step()?;
}
KeyCode::Enter => match self.flow_state {
AppState::Finished => (),
_ => self.next_step()?,
},
_ => {}
}
Ok(())
@ -340,7 +454,7 @@ impl<'a> AppInfo<'a> {
SelectedFinalConfig::FinalFreq => self.stop_frequency.change_value(direction),
SelectedFinalConfig::Points => self.points.change_value(direction),
},
AppState::Popup => (),
AppState::EndWindow => (),
AppState::Finished => (),
}
}
@ -366,7 +480,7 @@ impl<'a> AppInfo<'a> {
_ => None,
}
}
AppState::Popup => None,
AppState::EndWindow => None,
AppState::Finished => None,
} {
self.flow_state = s;
@ -393,13 +507,20 @@ impl<'a> AppInfo<'a> {
let start = self.start_frequency.mhz_to_usize();
let stop = self.stop_frequency.mhz_to_usize();
let points = self.points.int_to_usize();
thread::spawn(move || {
scan::scan_with_progress(pmtx, mmtx, cmtx, port, start, stop, points).unwrap()
});
// BYPASS
*self.progress.lock().unwrap() = 100;
// thread::spawn(move || {
// scan::scan_with_progress(pmtx, mmtx, cmtx, port, start, stop, points).unwrap()
// });
// scan::scan_with_progress(pmtx, mmtx, cmtx, port).unwrap();
}
AppState::Popup => todo!(),
AppState::Finished => todo!(),
AppState::EndWindow => {
self.should_quit = true;
}
AppState::Finished => {
self.flow_state = AppState::EndWindow;
self.init_end()?;
}
}
Ok(())
}

View File

@ -78,7 +78,7 @@ pub(crate) struct AppInfo<'a> {
/// The number of points
pub(crate) points: UIUsize,
/// Global progress
pub(crate) progress: Arc<Mutex<f64>>,
pub(crate) progress: Arc<Mutex<usize>>,
/// Global initialisation data
pub(crate) scan_info: Arc<Mutex<String>>,
/// Global message
@ -136,8 +136,8 @@ pub(crate) enum AppState {
InitialConfig(SelectedConfig),
/// Final config, frequencies and points
FinalConfig(SelectedFinalConfig),
/// Popup for text entering
Popup,
/// The end window for confirmation
EndWindow,
/// Save window
Finished,
}
@ -324,6 +324,7 @@ pub(crate) struct MenuItems<'a> {
/// Try to guess the best port to preselect
fn find_best_port(ports: &[String]) -> usize {
// TODO: Try to load from disk then save to disk
let best_ports = ["/dev/ttyACM0", "COM3", "COM5", "/dev/ttyS16"];
for port in best_ports {
if let Some(p) = ports.iter().position(|v| v == port) {
@ -347,6 +348,7 @@ impl<'a> AppInfo<'a> {
)
}
}
impl<'a> AppInfo<'a> {
/// Update all the colors
pub(crate) fn update_color(&mut self) -> Result<(), TinyUIError> {
@ -369,30 +371,9 @@ impl<'a> AppInfo<'a> {
self.menu_items.portlist = self.change_color("COM Ports", p, p_c);
self.menu_items.baudlist = self.change_color("Baudrate", b, b_c);
}
AppState::FinalConfig(ref c) => {
// self.status_message = format!("Moo");
// let colorlist = match c {
// SelectedFinalConfig::StartFreq => {
// [SELECTED_COLOR, UNSELECTED_COLOR, UNSELECTED_COLOR]
// }
// SelectedFinalConfig::FinalFreq => {
// [UNSELECTED_COLOR, SELECTED_COLOR, UNSELECTED_COLOR]
// }
// SelectedFinalConfig::Points => {
// [UNSELECTED_COLOR, UNSELECTED_COLOR, SELECTED_COLOR]
// }
// };
// for (area, color) in self.menu_items.textboxes.iter_mut().zip(colorlist) {
// .block(
// Block::default()
// // .borders(Borders::ALL)
// .style(Style::default().fg(Color::Blue)), // .title(" Inactive (^X to switch) "),
// )
// }
}
AppState::Popup => todo!(),
AppState::Finished => todo!(),
// TODO: Have all color update here
AppState::FinalConfig(_selected_final_config) => (),
_ => (),
}
Ok(())
}