Most of the shady sync shenenigans should be fixed
This commit is contained in:
parent
efe9f37065
commit
06f3e2b443
54
src/scan.rs
54
src/scan.rs
|
|
@ -62,6 +62,10 @@ pub(crate) fn scan_with_progress(connection_info: ConnectionInfo) -> Result<(),
|
||||||
connection_info.update_message("Acquiring version & info");
|
connection_info.update_message("Acquiring version & info");
|
||||||
let v = send_cmd(&mut port, "version\r", SHORT_WAIT)?;
|
let v = send_cmd(&mut port, "version\r", SHORT_WAIT)?;
|
||||||
let i = send_cmd(&mut port, "info\r", SHORT_WAIT)?;
|
let i = send_cmd(&mut port, "info\r", SHORT_WAIT)?;
|
||||||
|
// let f: String = send_cmd(&mut port, "leveloffset\r", SHORT_WAIT)?;
|
||||||
|
// TODO : Also add deviceid, vbat, usart_cfg; threads
|
||||||
|
// TODO: Parse this info in a struct and do a version check
|
||||||
|
|
||||||
// TODO: have a more thourough version check
|
// TODO: have a more thourough version check
|
||||||
if v.is_empty() || i.is_empty() {
|
if v.is_empty() || i.is_empty() {
|
||||||
return Err(TinyError::NoVersionFound);
|
return Err(TinyError::NoVersionFound);
|
||||||
|
|
@ -196,7 +200,7 @@ fn send_cmd(port: &mut Box<dyn SerialPort>, command: &str, wait: u64) -> Result<
|
||||||
/// This command will produce unknown results if the output buffer is never read
|
/// This command will produce unknown results if the output buffer is never read
|
||||||
unsafe fn send_raw(port: &mut Box<dyn SerialPort>, command: &str) -> Result<(), TinyError> {
|
unsafe fn send_raw(port: &mut Box<dyn SerialPort>, command: &str) -> Result<(), TinyError> {
|
||||||
let output = command.as_bytes();
|
let output = command.as_bytes();
|
||||||
let _r = port.write(output).map_err(|e| TinyError::SendError(e))?;
|
let _r = port.write(output).map_err(TinyError::SendError)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -280,25 +284,28 @@ pub fn parse_data(
|
||||||
let mut csvdata = Vec::with_capacity(points);
|
let mut csvdata = Vec::with_capacity(points);
|
||||||
if let Some(begining) = memchr::memchr(b'{', &v) {
|
if let Some(begining) = memchr::memchr(b'{', &v) {
|
||||||
let end_tmp = v.len();
|
let end_tmp = v.len();
|
||||||
let data = &v[begining + 1..end_tmp - 4]; // -4 since ch>\r will always be ignored
|
let data = &v[begining + 1..end_tmp]; // -4 since ch>\r will always be ignored
|
||||||
// TODO: Fix weird data
|
// TODO: Fix weird data
|
||||||
info!("The begining looks like :{:?}", &v[begining..begining + 10]);
|
info!(
|
||||||
info!("The end looks like :{:?}", &v[end_tmp - 10..end_tmp - 1]);
|
"The begining looks like :{:?}",
|
||||||
|
&v[begining - 1..begining + 9]
|
||||||
|
);
|
||||||
|
info!("The end looks like :{:?}", &v[end_tmp - 10..end_tmp]);
|
||||||
|
|
||||||
let mut end = end_tmp;
|
let mut end = end_tmp;
|
||||||
let chunks_exact = data.chunks_exact(3);
|
let chunks_exact = data.chunks_exact(3);
|
||||||
if chunks_exact.remainder().is_empty() {
|
// if chunks_exact.remainder().is_empty() {
|
||||||
return Err(TinyError::ParseEnd(
|
// return Err(TinyError::ParseEnd(
|
||||||
"The data received doesn't have a end char (empty)".to_owned(),
|
// "The data received doesn't have a end char (empty)".to_owned(),
|
||||||
));
|
// ));
|
||||||
} else if chunks_exact.remainder()[0] != b'}' {
|
// } else if chunks_exact.remainder()[0] != b'}' {
|
||||||
error!("Incorrect right end char {:?}", chunks_exact.remainder());
|
// error!("Incorrect right end char {:?}", chunks_exact.remainder());
|
||||||
// No end char, bogus
|
// // No end char, bogus
|
||||||
return Err(TinyError::ParseEnd(format!(
|
// return Err(TinyError::ParseEnd(format!(
|
||||||
"The returned data doesn't have the right end char expected }} found {:?}",
|
// "The returned data doesn't have the right end char expected }} found {:?}",
|
||||||
chunks_exact.remainder()
|
// chunks_exact.remainder()
|
||||||
)));
|
// )));
|
||||||
}
|
// }
|
||||||
for (i, slice) in chunks_exact.enumerate() {
|
for (i, slice) in chunks_exact.enumerate() {
|
||||||
let s: &[u8; 3] = &[slice[0], slice[1], slice[2]];
|
let s: &[u8; 3] = &[slice[0], slice[1], slice[2]];
|
||||||
let d = DataPoint::unpack(s)?;
|
let d = DataPoint::unpack(s)?;
|
||||||
|
|
@ -306,25 +313,30 @@ pub fn parse_data(
|
||||||
// Found an end and the rest is garbage
|
// Found an end and the rest is garbage
|
||||||
end = i * 3;
|
end = i * 3;
|
||||||
// TODO: why doesn't it pop ?
|
// TODO: why doesn't it pop ?
|
||||||
warn!("Found an end at {end}");
|
warn!("Found an end at {end} {:?}", &v[end - 4..end]);
|
||||||
break;
|
break;
|
||||||
} else if d.align != b'x' {
|
} else if d.align != b'x' {
|
||||||
// The alignment char is wrong
|
// The alignment char is wrong
|
||||||
error!("Wrong alignment char");
|
error!("Wrong alignment char expected {} found {}", b'x', d.align);
|
||||||
return Err(TinyError::ParseEnd("Wrong alignement char".to_owned()));
|
return Err(TinyError::ParseEnd("Wrong alignement char".to_owned()));
|
||||||
}
|
}
|
||||||
let (pos, val) = (
|
let (pos, val) = (
|
||||||
(i as f64 * increment + start as f64) / 1000000.,
|
(i as f64 * increment + start as f64) / 1000000.,
|
||||||
u16_to_f64(d.value),
|
u16_to_f64(d.value),
|
||||||
);
|
);
|
||||||
trace!("{},{},{} : {pos},{val}", i, increment, start);
|
trace!("{} : {pos},{val}", i);
|
||||||
|
|
||||||
csvdata.push((pos, val));
|
csvdata.push((pos, val));
|
||||||
writeln!(out, "{},{}", pos, val,)?;
|
writeln!(out, "{},{}", pos, val,)?;
|
||||||
}
|
}
|
||||||
// TODO: Dont return use mutex
|
// TODO: Dont return use mutex
|
||||||
// info!("debug",format!("{} {}", data.len(), v.len());
|
// info!("debug",format!("{} {}", data.len(), v.len());
|
||||||
info!("End at {end} {}", v[end - 1]);
|
info!(
|
||||||
|
"End at {end} expected {} and found {} ({:?})",
|
||||||
|
b'}',
|
||||||
|
v[end - 1],
|
||||||
|
&v[end - 4..=end - 1]
|
||||||
|
);
|
||||||
Ok((name, csvdata))
|
Ok((name, csvdata))
|
||||||
} else {
|
} else {
|
||||||
Err(TinyError::ParseBeggining("{".to_owned()))
|
Err(TinyError::ParseBeggining("{".to_owned()))
|
||||||
|
|
|
||||||
|
|
@ -161,21 +161,36 @@ pub(crate) enum ConnectionStatus {
|
||||||
ClosedWithSuccess,
|
ClosedWithSuccess,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ScanData = Vec<(f64, f64)>;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
/// All data needed to handle a connection to the TinySA
|
/// All data needed to handle a connection to the TinySA
|
||||||
pub(crate) struct ConnectionInfo {
|
pub(crate) struct ConnectionInfo {
|
||||||
|
/// The number of points to scan
|
||||||
pub(crate) points: usize,
|
pub(crate) points: usize,
|
||||||
|
/// The baudrate of the connection
|
||||||
pub(crate) baudrate: u32,
|
pub(crate) baudrate: u32,
|
||||||
|
/// The Serial port to open for connection
|
||||||
pub(crate) port: String,
|
pub(crate) port: String,
|
||||||
|
/// A mutex to share the progression (over 100)
|
||||||
progress: Arc<Mutex<usize>>,
|
progress: Arc<Mutex<usize>>,
|
||||||
|
/// The number of times to repeate a mesurement of a point
|
||||||
pub(crate) repeat: usize,
|
pub(crate) repeat: usize,
|
||||||
return_data: Arc<Mutex<Option<Vec<(f64, f64)>>>>,
|
/// A mutex to the result of the scan
|
||||||
|
return_data: Arc<Mutex<Option<ScanData>>>,
|
||||||
|
/// The information of the TinySA
|
||||||
scan_info: Arc<Mutex<String>>,
|
scan_info: Arc<Mutex<String>>,
|
||||||
|
/// The last scan message (what's happening as the scan goes)
|
||||||
scan_message: Arc<Mutex<String>>,
|
scan_message: Arc<Mutex<String>>,
|
||||||
|
/// The value to start at (in Hz)
|
||||||
pub(crate) start: usize,
|
pub(crate) start: usize,
|
||||||
|
/// The value to stop at (in Hz)
|
||||||
pub(crate) stop: usize,
|
pub(crate) stop: usize,
|
||||||
|
/// The file name to store all data
|
||||||
file_name: Arc<Mutex<String>>,
|
file_name: Arc<Mutex<String>>,
|
||||||
|
/// The connection status
|
||||||
connection_status: Arc<Mutex<ConnectionStatus>>,
|
connection_status: Arc<Mutex<ConnectionStatus>>,
|
||||||
|
/// The last error raised
|
||||||
pub(crate) error_raised: Arc<Mutex<Option<TinyUIError>>>,
|
pub(crate) error_raised: Arc<Mutex<Option<TinyUIError>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -415,6 +415,7 @@ impl<'a> AppInfo<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract a user explaination for an error message
|
||||||
fn error_message_from_current_error(&self) -> Result<String, TinyUIError> {
|
fn error_message_from_current_error(&self) -> Result<String, TinyUIError> {
|
||||||
let status_message = match self
|
let status_message = match self
|
||||||
.get_connection_info()?
|
.get_connection_info()?
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue