diff --git a/macros/node-poll-variants/src/lib.rs b/macros/node-poll-variants/src/lib.rs index 2f3936e..92c5a7e 100644 --- a/macros/node-poll-variants/src/lib.rs +++ b/macros/node-poll-variants/src/lib.rs @@ -142,6 +142,8 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { let publisher_path: Path = parse_quote!(physical_node::transducer::Publisher); let cellview_path: Path = parse_quote!(physical_node::cell::CellView); + let error_path: Path = parse_quote!(physical_node::CriticalError); + let expanded = quote! { // ----- Stateful struct ---------------------------------- #vis struct #stateful_variant_ident #og_generics #og_where_clause { @@ -153,11 +155,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { impl #og_impl_generics #poll_path for #stateful_variant_ident #og_type_generics #og_where_clause { type Value = #value_type_path; - #[inline(always)] - async fn poll(&self) -> Self::Value { - let value = self.poll.poll().await; - self.state.update(value); - value + #[inline] + async fn poll(&self) -> Result { + let result = self.poll.poll().await; + if let Ok(value) = result { + self.state.update(value); + } + result } } @@ -187,11 +191,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { impl #publ_impl_generics #poll_path for #publish_variant_ident #publ_type_generics #publ_where_clause { type Value = #value_type_path; - #[inline(always)] - async fn poll(&self) -> Self::Value { - let value = self.poll.poll().await; - self.publisher.update(value); - value + #[inline] + async fn poll(&self) -> Result { + let result = self.poll.poll().await; + if let Ok(value) = result { + self.publisher.update(value); + } + result } } @@ -221,11 +227,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream { type Value = #value_type_path; #[inline] - async fn poll(&self) -> Self::Value { - let value = self.poll.poll().await; - self.state.update(value); - self.publisher.update(value); - value + async fn poll(&self) -> Result { + let result = self.poll.poll().await; + if let Ok(value) = result { + self.state.update(value); + self.publisher.update(value); + } + result } } diff --git a/macros/node-poll-variants/tests/generate.rs b/macros/node-poll-variants/tests/generate.rs index 01cf375..73c119a 100644 --- a/macros/node-poll-variants/tests/generate.rs +++ b/macros/node-poll-variants/tests/generate.rs @@ -1,6 +1,7 @@ #![feature(async_fn_in_trait, impl_trait_projections)] use node_poll_variants::PollVariants; +use physical_node::CriticalError; use physical_node::transducer::input::Poll; #[derive(PollVariants)] @@ -21,8 +22,8 @@ where { type Value = SecondT; - async fn poll(&self) -> Self::Value { - self.second + async fn poll(&self) -> Result { + Ok(self.second) } } diff --git a/node/src/lib.rs b/node/src/lib.rs index e712a34..ff5daeb 100644 --- a/node/src/lib.rs +++ b/node/src/lib.rs @@ -6,3 +6,5 @@ pub mod transducer; pub mod cell { pub use physical::cell::*; } + +pub use physical::CriticalError; diff --git a/peripheral-components/ads1256/node/src/standard_multiplexer/sync/poll.rs b/peripheral-components/ads1256/node/src/standard_multiplexer/sync/poll.rs index 62513eb..ea30b64 100644 --- a/peripheral-components/ads1256/node/src/standard_multiplexer/sync/poll.rs +++ b/peripheral-components/ads1256/node/src/standard_multiplexer/sync/poll.rs @@ -1,11 +1,12 @@ use ads1256::{ - AdControl, Ads1256, BlockingDelay, DataRate, Multiplexer, OutputPin, SpiBus, Status, Wait, + AdControl, Ads1256, BlockingDelay, DataRate, Gain, Multiplexer, OutputPin, SpiBus, Status, Wait, }; use core::ops::DerefMut; use embassy_sync::blocking_mutex::raw::RawMutex; use embassy_sync::mutex::Mutex; use node_poll_variants::PollVariants; use physical_node::transducer::input::Poll; +use physical_node::CriticalError; use uom::si::f32; #[derive(PollVariants)] @@ -36,8 +37,9 @@ where { type Value = f32::ElectricPotential; - async fn poll(&self) -> Self::Value { - self.ads1256 + async fn poll(&self) -> Result { + let result = self + .ads1256 .lock() .await .deref_mut() @@ -49,13 +51,20 @@ where self.input_mod.data_rate(), true, ) - .await + .await; + + match result { + Ok(conversion) => Ok(conversion.to_voltage(self.input_mod.gain())), + Err(_) => Err(CriticalError::Communication), + } } } -pub trait ModInput { +pub trait ModInput: Copy { fn multiplexer(self) -> Multiplexer; + fn gain(self) -> Gain; + fn status(self) -> Option; fn ad_control(self) -> Option; @@ -64,8 +73,12 @@ pub trait ModInput { } /// buffer +#[derive(Copy, Clone, Eq, PartialEq)] pub struct ModInStatus { pub multiplexer: Multiplexer, + /// Only used for converting to voltage, this value must match what is set on the ADS1256, it + /// will not *be* set unlike the other fields. + pub gain: Gain, pub status: Status, } @@ -75,6 +88,11 @@ impl ModInput for ModInStatus { self.multiplexer } + #[inline(always)] + fn gain(self) -> Gain { + self.gain + } + #[inline(always)] fn status(self) -> Option { Some(self.status) @@ -92,6 +110,7 @@ impl ModInput for ModInStatus { } /// gain +#[derive(Copy, Clone, Eq, PartialEq)] pub struct ModInAdControl { pub multiplexer: Multiplexer, pub ad_control: AdControl, @@ -103,6 +122,11 @@ impl ModInput for ModInAdControl { self.multiplexer } + #[inline(always)] + fn gain(self) -> Gain { + self.ad_control.gain() + } + #[inline(always)] fn status(self) -> Option { None @@ -120,8 +144,12 @@ impl ModInput for ModInAdControl { } /// data rate +#[derive(Copy, Clone, Eq, PartialEq)] pub struct ModInDataRate { pub multiplexer: Multiplexer, + /// Only used for converting to voltage, this value must match what is set on the ADS1256, it + /// will not *be* set unlike the other fields. + pub gain: Gain, pub data_rate: DataRate, } @@ -131,6 +159,11 @@ impl ModInput for ModInDataRate { self.multiplexer } + #[inline(always)] + fn gain(self) -> Gain { + self.gain + } + #[inline(always)] fn status(self) -> Option { None @@ -148,6 +181,7 @@ impl ModInput for ModInDataRate { } /// buffer, gain +#[derive(Copy, Clone, Eq, PartialEq)] pub struct ModInStatAdc { pub multiplexer: Multiplexer, pub status: Status, @@ -160,6 +194,11 @@ impl ModInput for ModInStatAdc { self.multiplexer } + #[inline(always)] + fn gain(self) -> Gain { + self.ad_control.gain() + } + #[inline(always)] fn status(self) -> Option { Some(self.status) @@ -177,8 +216,12 @@ impl ModInput for ModInStatAdc { } /// buffer, data rate +#[derive(Copy, Clone, Eq, PartialEq)] pub struct ModInStatDrate { pub multiplexer: Multiplexer, + /// Only used for converting to voltage, this value must match what is set on the ADS1256, it + /// will not *be* set unlike the other fields. + pub gain: Gain, pub status: Status, pub data_rate: DataRate, } @@ -189,6 +232,11 @@ impl ModInput for ModInStatDrate { self.multiplexer } + #[inline(always)] + fn gain(self) -> Gain { + self.gain + } + #[inline(always)] fn status(self) -> Option { Some(self.status) @@ -206,6 +254,7 @@ impl ModInput for ModInStatDrate { } // gain, data rate +#[derive(Copy, Clone, Eq, PartialEq)] pub struct ModInAdcDrate { pub multiplexer: Multiplexer, pub ad_control: AdControl, @@ -218,6 +267,11 @@ impl ModInput for ModInAdcDrate { self.multiplexer } + #[inline(always)] + fn gain(self) -> Gain { + self.ad_control.gain() + } + #[inline(always)] fn status(self) -> Option { None @@ -235,6 +289,7 @@ impl ModInput for ModInAdcDrate { } /// buffer, gain, data rate +#[derive(Copy, Clone, Eq, PartialEq)] pub struct ModInAll { pub multiplexer: Multiplexer, pub status: Status, @@ -248,6 +303,11 @@ impl ModInput for ModInAll { self.multiplexer } + #[inline(always)] + fn gain(self) -> Gain { + self.ad_control.gain() + } + #[inline(always)] fn status(self) -> Option { Some(self.status) diff --git a/src/lib.rs b/src/lib.rs index d700e0c..c8d494c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,4 @@ pub mod transducer; pub mod cell; mod error; -pub use error::*; +pub use error::CriticalError; diff --git a/src/transducer/input.rs b/src/transducer/input.rs index 43b8fa4..4664d79 100644 --- a/src/transducer/input.rs +++ b/src/transducer/input.rs @@ -1,5 +1,7 @@ +use crate::CriticalError; + pub trait Poll { type Value: Copy; - async fn poll(&self) -> Self::Value; + async fn poll(&self) -> Result; }