From 6d3fbb926e1204260749847c6be538ad81eedea6 Mon Sep 17 00:00:00 2001 From: Sergey Savelyev Date: Sat, 20 Sep 2025 10:38:15 -0700 Subject: [PATCH] reformat --- flight/src/data/reader.rs | 18 ++++++++++++------ flight/src/data/writer.rs | 23 ++++++++++++----------- flight/src/hardware/bno085/mod.rs | 28 +++++++++++++--------------- flight/src/hardware/bno085/shtp.rs | 7 +++---- flight/src/hardware/error.rs | 4 ++-- flight/src/hardware/imu/mod.rs | 1 - flight/src/hardware/mcp23017/mod.rs | 24 ++++++++++++------------ flight/src/hardware/mcp3208/mod.rs | 14 +++++++------- flight/src/hardware/mod.rs | 1 - flight/src/hardware/raspi/mod.rs | 10 +++++----- flight/src/lib.rs | 8 ++++---- flight/src/logger.rs | 2 +- flight/src/test_utils.rs | 2 +- 13 files changed, 72 insertions(+), 70 deletions(-) diff --git a/flight/src/data/reader.rs b/flight/src/data/reader.rs index ecccd6d..d89e45e 100644 --- a/flight/src/data/reader.rs +++ b/flight/src/data/reader.rs @@ -42,8 +42,12 @@ trait DataReadAccess { pub trait DataReader { fn reset(&mut self); - fn read_be(&mut self) -> Option where T: ReadableDataType; - fn read_le(&mut self) -> Option where T: ReadableDataType; + fn read_be(&mut self) -> Option + where + T: ReadableDataType; + fn read_le(&mut self) -> Option + where + T: ReadableDataType; fn read(&mut self, bytes: usize) -> Option<&[u8]>; } @@ -56,7 +60,7 @@ impl<'a> DataViewReader<'a> { pub fn new(data: &'a [u8]) -> Self { Self { data, - position: 0usize + position: 0usize, } } } @@ -91,7 +95,7 @@ impl DataBufferReader { pub fn get_buffer(&mut self) -> &mut [u8] { &mut self.data } - + pub fn get_write_buffer(&mut self, length: usize) -> &mut [u8] { self.reset(); &mut self.data[..length] @@ -119,7 +123,8 @@ impl DataReader for U { fn read_be(&mut self) -> Option where - T: ReadableDataType { + T: ReadableDataType, + { let position = self.get_position(); if self.get_data().len() < position + T::BYTES { return None; @@ -133,7 +138,8 @@ impl DataReader for U { fn read_le(&mut self) -> Option where - T: ReadableDataType { + T: ReadableDataType, + { let position = self.get_position(); if self.get_data().len() < position + T::BYTES { return None; diff --git a/flight/src/data/writer.rs b/flight/src/data/writer.rs index e13c86d..5b7ba59 100644 --- a/flight/src/data/writer.rs +++ b/flight/src/data/writer.rs @@ -49,10 +49,11 @@ pub trait DataWriteAccess { fn set_position(&mut self, position: usize); } -pub struct DeferredDataWrite<'a, T: WriteableDataType, U> -where +pub struct DeferredDataWrite<'a, T: WriteableDataType, U> +where RefCell: DataWriter, - as DataWriter>::WriterType: DataWriteAccess { + as DataWriter>::WriterType: DataWriteAccess, +{ writer: &'a RefCell, position: usize, _phantom_data: PhantomData, @@ -64,7 +65,7 @@ pub trait DataWriter { fn reset(&mut self); fn get_position(&self) -> usize; fn get_buffer(&mut self) -> &[u8]; - + fn push_be(&self, data: T) -> Result<(), DataWriterError>; fn push_le(&self, data: T) -> Result<(), DataWriterError>; fn push(&self, data: &[u8]) -> Result<(), DataWriterError>; @@ -80,7 +81,7 @@ impl<'a> DataViewWriter<'a> { pub fn new(data: &'a mut [u8]) -> RefCell { RefCell::new(Self { data, - position: 0usize + position: 0usize, }) } } @@ -108,7 +109,7 @@ impl DataBufferWriter { pub fn new() -> RefCell { RefCell::new(Self { data: [0u8; LENGTH], - position: 0usize + position: 0usize, }) } } @@ -129,7 +130,7 @@ impl DataWriteAccess for DataBufferWriter { impl DataWriter for RefCell { type WriterType = U; - + fn reset(&mut self) { self.get_mut().set_position(0); } @@ -187,7 +188,10 @@ impl DataWriter for RefCell { } } -impl<'a, T: WriteableDataType, U: DataWriteAccess> DeferredDataWrite<'a, T, U> where RefCell: DataWriter { +impl<'a, T: WriteableDataType, U: DataWriteAccess> DeferredDataWrite<'a, T, U> +where + RefCell: DataWriter, +{ pub fn set_be(self, data: T) { data.to_be_slice(&mut self.writer.borrow_mut().get_data()[self.position..(self.position + T::BYTES)]); } @@ -198,9 +202,6 @@ impl<'a, T: WriteableDataType, U: DataWriteAccess> DeferredDataWrite<'a, T, U> w #[cfg(test)] mod tests { - use super::*; - use anyhow::Result; - #[test] fn test_buffer() -> Result<()> { let mut writer = DataBufferWriter::<128>::new(); diff --git a/flight/src/hardware/bno085/mod.rs b/flight/src/hardware/bno085/mod.rs index 7a0f669..7368843 100644 --- a/flight/src/hardware/bno085/mod.rs +++ b/flight/src/hardware/bno085/mod.rs @@ -1,6 +1,7 @@ mod shtp; use crate::data::reader::{DataBufferReader, DataReader}; +use crate::data::writer::{DataBufferWriter, DataWriter}; use crate::hardware::error::{NotAvailableError, SpiError}; use crate::hardware::imu::{Imu, ImuMeasurement}; use anyhow::{anyhow, bail, ensure, Result}; @@ -8,7 +9,6 @@ use chrono::TimeDelta; use embedded_hal::spi::SpiDevice; use log::info; use num_traits::ToPrimitive; -use crate::data::writer::{DataBufferWriter, DataWriter}; const RX_BUFFER_LENGTH: usize = 1024; @@ -30,15 +30,15 @@ pub struct Bno085 { impl Bno085 where - SPI : SpiDevice, - SPI::Error : Send, - SPI::Error : Sync, - SPI::Error : 'static, + SPI: SpiDevice, + SPI::Error: Send, + SPI::Error: Sync, + SPI::Error: 'static, { pub fn new(spi: SPI) -> Self { Self { spi, - sequence_number: 0u8 + sequence_number: 0u8, } } @@ -81,7 +81,7 @@ where let mut tx = DataBufferWriter::<128>::new(); self.produce_sensor_enable(&tx)?; - self.spi.write( tx.get_buffer()).map_err(SpiError)?; + self.spi.write(tx.get_buffer()).map_err(SpiError)?; Ok(()) } @@ -131,7 +131,7 @@ where fn produce_sensor_enable( &mut self, - tx: &impl DataWriter + tx: &impl DataWriter, ) -> Result<()> { let starting_position = tx.get_position(); let length = tx.defer::()?; @@ -146,7 +146,7 @@ where 0, TimeDelta::seconds(1) / 100, // 100Hz TimeDelta::zero(), - 0 + 0, )?; self.produce_set_feature( tx, @@ -155,7 +155,7 @@ where 0, TimeDelta::seconds(1) / 100, // 100Hz TimeDelta::zero(), - 0 + 0, )?; self.produce_set_feature( tx, @@ -164,7 +164,7 @@ where 0, TimeDelta::seconds(1) / 100, // 100Hz TimeDelta::zero(), - 0 + 0, )?; length.set_le((tx.get_position() - starting_position) as u16); // Fill in the length as we finish @@ -179,7 +179,7 @@ where change_sensitivity: u16, report_interval: TimeDelta, batch_interval: TimeDelta, - configuration_word: u32 + configuration_word: u32, ) -> Result<()> { tx.push_le(SET_FEATURE_REPORT_ID)?; tx.push_le(feature_report_id)?; @@ -198,7 +198,7 @@ where } } -impl Imu for Bno085 { +impl Imu for Bno085 { fn step(&mut self) -> Result { todo!("asd") } @@ -206,7 +206,6 @@ impl Imu for Bno085 { #[cfg(test)] mod tests { - use super::*; use crate::test_utils::SilentDrop; use embedded_hal_mock::eh1::spi::Mock as SpiMock; use embedded_hal_mock::eh1::spi::Transaction as SpiTransaction; @@ -258,7 +257,6 @@ mod tests { bno.spi.done(); Ok(()) - } } diff --git a/flight/src/hardware/bno085/shtp.rs b/flight/src/hardware/bno085/shtp.rs index 67e44ce..93544cb 100644 --- a/flight/src/hardware/bno085/shtp.rs +++ b/flight/src/hardware/bno085/shtp.rs @@ -1,21 +1,20 @@ use crate::data::reader::DataReader; use crate::hardware::error::NotAvailableError; -use anyhow::Result; pub struct Header { pub length: u16, pub channel: u8, - pub sequence_number: u8 + pub sequence_number: u8, } impl Header { pub const SIZE: usize = size_of::() + size_of::() + size_of::(); - + pub fn parse(bytes: &mut impl DataReader) -> Result { let length = bytes.read_le::().ok_or(NotAvailableError)?; let channel = bytes.read_le::().ok_or(NotAvailableError)?; let sequence_number = bytes.read_le::().ok_or(NotAvailableError)?; - + Ok(Self { length, channel, diff --git a/flight/src/hardware/error.rs b/flight/src/hardware/error.rs index a2e3a62..c207d52 100644 --- a/flight/src/hardware/error.rs +++ b/flight/src/hardware/error.rs @@ -10,7 +10,7 @@ impl Display for I2cError { } } -impl Error for I2cError { } +impl Error for I2cError {} #[derive(Copy, Clone, Debug)] pub struct SpiError(pub ERR); @@ -21,7 +21,7 @@ impl Display for SpiError { } } -impl Error for SpiError { } +impl Error for SpiError {} // #[derive(Copy, Clone, Debug)] // pub struct NotAvailableError; diff --git a/flight/src/hardware/imu/mod.rs b/flight/src/hardware/imu/mod.rs index f3912d3..c7e82ac 100644 --- a/flight/src/hardware/imu/mod.rs +++ b/flight/src/hardware/imu/mod.rs @@ -1,5 +1,4 @@ use nalgebra::Vector3; -use anyhow::Result; pub struct ImuMeasurement { pub acceleration: Vector3, diff --git a/flight/src/hardware/mcp23017/mod.rs b/flight/src/hardware/mcp23017/mod.rs index ce6bd8c..2bceb4f 100644 --- a/flight/src/hardware/mcp23017/mod.rs +++ b/flight/src/hardware/mcp23017/mod.rs @@ -1,9 +1,9 @@ -use std::fmt::{Debug, Formatter}; -use std::time::Instant; +use crate::hardware::error::I2cError; use anyhow::{bail, Result}; use embedded_hal::i2c::I2c; use log::{info, trace}; -use crate::hardware::error::I2cError; +use std::fmt::{Debug, Formatter}; +use std::time::Instant; pub trait Mcp23017 { fn init(&mut self) -> Result<()>; @@ -27,9 +27,9 @@ impl Debug for Mcp23017Driver { impl Mcp23017Driver where I2C: I2c, - I2C::Error : Send, - I2C::Error : Sync, - I2C::Error : 'static + I2C::Error: Send, + I2C::Error: Sync, + I2C::Error: 'static, { pub fn new(i2c: I2C, address: u8) -> Self { trace!("Mcp23017Driver::new(i2c, address: {address:07b})"); @@ -46,9 +46,9 @@ where impl Mcp23017 for Mcp23017Driver where I2C: I2c, - I2C::Error : Send, - I2C::Error : Sync, - I2C::Error : 'static + I2C::Error: Send, + I2C::Error: Sync, + I2C::Error: 'static, { fn init(&mut self) -> Result<()> { trace!("Mcp23017Driver::init(self: {self:?})"); @@ -64,10 +64,10 @@ where let (pin_bank, dirty_flag, pin_index) = match pin { 0..8 => { (&mut self.bank[0], &mut self.dirty, pin as u32) - }, - 8 ..16 => { + } + 8..16 => { (&mut self.bank[1], &mut self.dirty, (pin as u32) - 8) - }, + } _ => bail!("Invalid Pin ID"), }; let pin_mask = 1u8.unbounded_shl(pin_index); diff --git a/flight/src/hardware/mcp3208/mod.rs b/flight/src/hardware/mcp3208/mod.rs index 29ec579..d05581b 100644 --- a/flight/src/hardware/mcp3208/mod.rs +++ b/flight/src/hardware/mcp3208/mod.rs @@ -1,12 +1,12 @@ -use std::fmt::{Debug, Formatter}; use crate::hardware::error::SpiError; use anyhow::{ensure, Result}; use embedded_hal::spi::SpiDevice; use log::trace; +use std::fmt::{Debug, Formatter}; pub struct Mcp3208 { spi: SPI, - vref: f64 + vref: f64, } impl Debug for Mcp3208 { @@ -17,16 +17,16 @@ impl Debug for Mcp3208 { impl Mcp3208 where - SPI : SpiDevice, - SPI::Error : Send, - SPI::Error : Sync, - SPI::Error : 'static, + SPI: SpiDevice, + SPI::Error: Send, + SPI::Error: Sync, + SPI::Error: 'static, { pub fn new(spi: SPI, vref: f64) -> Self { trace!("Mcp3208::new(spi, vref: {vref})"); Self { spi, - vref + vref, } } diff --git a/flight/src/hardware/mod.rs b/flight/src/hardware/mod.rs index d7fbd44..8ea9ebb 100644 --- a/flight/src/hardware/mod.rs +++ b/flight/src/hardware/mod.rs @@ -2,7 +2,6 @@ use crate::hardware::mcp23017::Mcp23017; use anyhow::Result; pub trait Hardware { - fn get_mcp23017_a(&self) -> impl Mcp23017; fn get_mcp23017_b(&self) -> impl Mcp23017; diff --git a/flight/src/hardware/raspi/mod.rs b/flight/src/hardware/raspi/mod.rs index 7f210dd..e211885 100644 --- a/flight/src/hardware/raspi/mod.rs +++ b/flight/src/hardware/raspi/mod.rs @@ -1,15 +1,15 @@ -use std::cell::RefCell; -use std::sync::Mutex; +use crate::hardware::mcp23017::{Mcp23017, Mcp23017Driver}; +use crate::hardware::mcp3208::Mcp3208; use crate::hardware::Hardware; use anyhow::Result; use embedded_hal_bus::i2c::MutexDevice; use log::{debug, info, trace}; // use rpi_pal::gpio::Gpio; use rpi_pal::i2c::I2c; -use rpi_pal::spi::{Bus, Mode, SlaveSelect, Spi}; -use crate::hardware::mcp23017::{Mcp23017, Mcp23017Driver}; -use crate::hardware::mcp3208::Mcp3208; use rpi_pal::spi::SimpleHalSpiDevice; +use rpi_pal::spi::{Bus, Mode, SlaveSelect, Spi}; +use std::cell::RefCell; +use std::sync::Mutex; const CLOCK_1MHZ: u32 = 1_000_000; diff --git a/flight/src/lib.rs b/flight/src/lib.rs index 9088259..e0bd209 100644 --- a/flight/src/lib.rs +++ b/flight/src/lib.rs @@ -1,11 +1,11 @@ -use std::thread::sleep; -use std::time::Duration; use crate::hardware::initialize; +use crate::hardware::mcp23017::Mcp23017; +use crate::hardware::Hardware; use crate::logger::setup_logger; use anyhow::Result; use log::info; -use crate::hardware::Hardware; -use crate::hardware::mcp23017::Mcp23017; +use std::thread::sleep; +use std::time::Duration; mod hardware; mod logger; diff --git a/flight/src/logger.rs b/flight/src/logger.rs index 6a9db07..fe9dfe1 100644 --- a/flight/src/logger.rs +++ b/flight/src/logger.rs @@ -1,8 +1,8 @@ use anyhow::Result; +use log::debug; use std::env; use std::fs::create_dir_all; use std::str::FromStr; -use log::debug; pub fn setup_logger() -> Result<()> { let log_file = env::var("LOG_FILE").or_else(|_| { diff --git a/flight/src/test_utils.rs b/flight/src/test_utils.rs index 5de14c7..1ceec9e 100644 --- a/flight/src/test_utils.rs +++ b/flight/src/test_utils.rs @@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut}; use std::panic; pub struct SilentDrop { - inner: ManuallyDrop + inner: ManuallyDrop, } impl SilentDrop {