From 185ebbc69e54b81e336a2a7d297ed98f0fd798d5 Mon Sep 17 00:00:00 2001 From: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:22:02 +0100 Subject: [PATCH] extend esf packets api Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> --- examples/adr-message-parsing/src/main.rs | 7 +++ examples/basic-cli/src/main.rs | 7 +++ examples/tui/src/cli.rs | 2 +- examples/tui/src/main.rs | 7 +-- examples/tui/src/tui.rs | 6 +-- ublox/src/ubx_packets/packets.rs | 67 ++++++++++++++++++++---- 6 files changed, 79 insertions(+), 17 deletions(-) diff --git a/examples/adr-message-parsing/src/main.rs b/examples/adr-message-parsing/src/main.rs index 6fde7a0..43a0b0b 100644 --- a/examples/adr-message-parsing/src/main.rs +++ b/examples/adr-message-parsing/src/main.rs @@ -271,6 +271,8 @@ impl Device { pub fn wait_for_ack(&mut self) -> std::io::Result<()> { let mut found_packet = false; + let start = std::time::SystemTime::now(); + let timeout = Duration::from_secs(3); while !found_packet { self.update(|packet| { if let PacketRef::AckAck(ack) = packet { @@ -279,6 +281,11 @@ impl Device { } } })?; + + if start.elapsed().unwrap().as_millis() > timeout.as_millis() { + eprintln!("Did not receive ACK message for request"); + break; + } } Ok(()) } diff --git a/examples/basic-cli/src/main.rs b/examples/basic-cli/src/main.rs index 73cf978..680a9b2 100644 --- a/examples/basic-cli/src/main.rs +++ b/examples/basic-cli/src/main.rs @@ -450,6 +450,8 @@ impl Device { pub fn wait_for_ack(&mut self) -> std::io::Result<()> { let mut found_packet = false; + let start = std::time::SystemTime::now(); + let timeout = Duration::from_secs(3); while !found_packet { self.update(|packet| { if let PacketRef::AckAck(ack) = packet { @@ -458,6 +460,11 @@ impl Device { } } })?; + + if start.elapsed().unwrap().as_millis() > timeout.as_millis() { + error!("Did not receive ACK message for request"); + break; + } } Ok(()) } diff --git a/examples/tui/src/cli.rs b/examples/tui/src/cli.rs index 9ea205c..20a8278 100644 --- a/examples/tui/src/cli.rs +++ b/examples/tui/src/cli.rs @@ -34,7 +34,7 @@ pub fn parse_args() -> clap::ArgMatches { .short('p') .long("port") .required(true) - .help("Serial port to open"), + .help("Serial port to open. E.g., /dev/ttyACM0"), ) .arg( Arg::new("baud") diff --git a/examples/tui/src/main.rs b/examples/tui/src/main.rs index f7eb992..5ad98fc 100644 --- a/examples/tui/src/main.rs +++ b/examples/tui/src/main.rs @@ -14,14 +14,15 @@ fn main() -> Result<(), Box> { let cli = cli::parse_args(); if cli.get_flag("debug-mode") { - debug_mode(&cli); + device_debug_mode(&cli); } else { - crate::tui::run(&cli)?; + let log_file = logging::initialize(&cli)?; + crate::tui::run(&cli, log_file)?; } Ok(()) } -fn debug_mode(cli: &ArgMatches) { +fn device_debug_mode(cli: &ArgMatches) { use log::error; env_logger::Builder::new() .filter_level(log::LevelFilter::Info) diff --git a/examples/tui/src/tui.rs b/examples/tui/src/tui.rs index dfd4ccb..f76afc9 100644 --- a/examples/tui/src/tui.rs +++ b/examples/tui/src/tui.rs @@ -1,6 +1,7 @@ use std::{ error::Error, io, + path::PathBuf, sync::mpsc::{channel, Receiver}, time::{Duration, Instant}, }; @@ -24,11 +25,10 @@ use tracing::{debug, info, instrument}; use crate::{ app::{App, UbxStatus}, device::Device, - logging, ui, + ui, }; -pub fn run(cli: &ArgMatches) -> Result<(), Box> { - let log_file = logging::initialize(cli)?; +pub fn run(cli: &ArgMatches, log_file: PathBuf) -> Result<(), Box> { let tick_rate: u64 = *cli.get_one("tui-rate").ok_or("Missing tui-rate cli arg")?; let tick_rate = Duration::from_millis(tick_rate); diff --git a/ublox/src/ubx_packets/packets.rs b/ublox/src/ubx_packets/packets.rs index 964cc7a..92920f1 100644 --- a/ublox/src/ubx_packets/packets.rs +++ b/ublox/src/ubx_packets/packets.rs @@ -3479,6 +3479,7 @@ impl core::iter::Iterator for EsfRawDataIter<'_> { } } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum EsfAlgStatus { @@ -3524,6 +3525,9 @@ struct EsfAlg { pub struct EsfAlgFlags(u8); impl EsfAlgFlags { + pub fn flags_raw(&self) -> u8 { + self.0 + } pub fn auto_imu_mount_alg_on(self) -> bool { (self.0) & 0x1 != 0 } @@ -3653,8 +3657,12 @@ pub enum EsfStatusFusionMode { pub struct EsfInitStatus1(u8); impl EsfInitStatus1 { + const WHEEL_TICK_MASK: u8 = 0x03; + const MOUNTING_ANGLE_STATUS_MASK: u8 = 0x07; + const INS_STATUS_MASK: u8 = 0x03; + pub fn wheel_tick_init_status(self) -> EsfStatusWheelTickInit { - let bits = (self.0) & 0x03; + let bits = (self.0) & Self::WHEEL_TICK_MASK; match bits { 0 => EsfStatusWheelTickInit::Off, 1 => EsfStatusWheelTickInit::Initializing, @@ -3665,8 +3673,12 @@ impl EsfInitStatus1 { } } + pub fn wheel_tick_init_status_raw(self) -> u8 { + self.wheel_tick_init_status() as u8 + } + pub fn mounting_angle_status(self) -> EsfStatusMountAngle { - let bits = (self.0 >> 2) & 0x07; + let bits = (self.0 >> 2) & Self::MOUNTING_ANGLE_STATUS_MASK; match bits { 0 => EsfStatusMountAngle::Off, 1 => EsfStatusMountAngle::Initializing, @@ -3678,8 +3690,12 @@ impl EsfInitStatus1 { } } + pub fn mount_angle_status_raw(self) -> u8 { + self.mounting_angle_status() as u8 + } + pub fn ins_initialization_status(self) -> EsfStatusInsInit { - let bits = (self.0 >> 5) & 0x03; + let bits = (self.0 >> 5) & Self::INS_STATUS_MASK; match bits { 0 => EsfStatusInsInit::Off, 1 => EsfStatusInsInit::Initializing, @@ -3689,6 +3705,10 @@ impl EsfInitStatus1 { }, } } + + pub fn ins_initialization_status_raw(self) -> u8 { + self.ins_initialization_status() as u8 + } } impl fmt::Debug for EsfInitStatus1 { @@ -3701,6 +3721,7 @@ impl fmt::Debug for EsfInitStatus1 { } } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum EsfStatusWheelTickInit { @@ -3709,6 +3730,7 @@ pub enum EsfStatusWheelTickInit { Initialized = 2, } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum EsfStatusMountAngle { @@ -3717,6 +3739,7 @@ pub enum EsfStatusMountAngle { Initialized = 2, } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum EsfStatusInsInit { @@ -3731,6 +3754,10 @@ pub enum EsfStatusInsInit { pub struct EsfInitStatus2(u8); impl EsfInitStatus2 { + pub fn imu_init_status_raw(self) -> u8 { + self.imu_init_status() as u8 + } + pub fn imu_init_status(self) -> EsfStatusImuInit { let bits = (self.0) & 0x02; match bits { @@ -3752,6 +3779,7 @@ impl fmt::Debug for EsfInitStatus2 { } } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum EsfStatusImuInit { @@ -3778,10 +3806,18 @@ impl EsfSensorStatus { self.faults } + pub fn faults_raw(&self) -> u8 { + self.faults().into_raw() + } + pub fn sensor_type(&self) -> EsfSensorType { self.sens_status1.sensor_type } + pub fn sensor_type_raw(&self) -> u8 { + self.sensor_type() as u8 + } + pub fn sensor_used(&self) -> bool { self.sens_status1.used } @@ -3794,9 +3830,17 @@ impl EsfSensorStatus { self.sens_status2.calibration_status } + pub fn calibration_status_raw(&self) -> u8 { + self.calibration_status() as u8 + } + pub fn time_status(&self) -> EsfSensorStatusTime { self.sens_status2.time_status } + + pub fn time_status_raw(&self) -> u8 { + self.time_status() as u8 + } } #[derive(Clone, Debug)] @@ -3846,6 +3890,7 @@ impl From for SensorStatus1 { } } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize))] pub enum EsfSensorType { @@ -3908,6 +3953,7 @@ impl From for SensorStatus2 { } } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize))] pub enum EsfSensorStatusCalibration { @@ -3927,6 +3973,7 @@ impl From for EsfSensorStatusCalibration { } } +#[repr(u8)] #[derive(Copy, Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize))] pub enum EsfSensorStatusTime { @@ -3951,13 +3998,13 @@ impl From for EsfSensorStatusTime { #[ubx_extend_bitflags] #[ubx(from, into_raw, rest_reserved)] bitflags! { -#[derive(Debug, Default, Clone, Copy)] -pub struct EsfSensorFaults: u8 { - const BAD_MEASUREMENT = 1; - const BAD_TIME_TAG = 2; - const MISSING_MEASUREMENT = 4; - const NOISY_MEASUREMENT = 8; -} + #[derive(Debug, Default, Clone, Copy)] + pub struct EsfSensorFaults: u8 { + const BAD_MEASUREMENT = 1; + const BAD_TIME_TAG = 2; + const MISSING_MEASUREMENT = 4; + const NOISY_MEASUREMENT = 8; + } } impl From for EsfSensorFaults {