PollInput implementation

This commit is contained in:
Zachary Sunforge
2023-06-23 16:55:33 -07:00
parent 07cb49bee5
commit 2a0013481c
6 changed files with 97 additions and 24 deletions

View File

@ -142,6 +142,8 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
let publisher_path: Path = parse_quote!(physical_node::transducer::Publisher); let publisher_path: Path = parse_quote!(physical_node::transducer::Publisher);
let cellview_path: Path = parse_quote!(physical_node::cell::CellView); let cellview_path: Path = parse_quote!(physical_node::cell::CellView);
let error_path: Path = parse_quote!(physical_node::CriticalError);
let expanded = quote! { let expanded = quote! {
// ----- Stateful struct ---------------------------------- // ----- Stateful struct ----------------------------------
#vis struct #stateful_variant_ident #og_generics #og_where_clause { #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 { impl #og_impl_generics #poll_path for #stateful_variant_ident #og_type_generics #og_where_clause {
type Value = #value_type_path; type Value = #value_type_path;
#[inline(always)] #[inline]
async fn poll(&self) -> Self::Value { async fn poll(&self) -> Result<Self::Value, #error_path> {
let value = self.poll.poll().await; let result = self.poll.poll().await;
if let Ok(value) = result {
self.state.update(value); self.state.update(value);
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 { impl #publ_impl_generics #poll_path for #publish_variant_ident #publ_type_generics #publ_where_clause {
type Value = #value_type_path; type Value = #value_type_path;
#[inline(always)] #[inline]
async fn poll(&self) -> Self::Value { async fn poll(&self) -> Result<Self::Value, #error_path> {
let value = self.poll.poll().await; let result = self.poll.poll().await;
if let Ok(value) = result {
self.publisher.update(value); self.publisher.update(value);
value }
result
} }
} }
@ -221,11 +227,13 @@ pub fn poll_variant_macro(input: TokenStream) -> TokenStream {
type Value = #value_type_path; type Value = #value_type_path;
#[inline] #[inline]
async fn poll(&self) -> Self::Value { async fn poll(&self) -> Result<Self::Value, #error_path> {
let value = self.poll.poll().await; let result = self.poll.poll().await;
if let Ok(value) = result {
self.state.update(value); self.state.update(value);
self.publisher.update(value); self.publisher.update(value);
value }
result
} }
} }

View File

@ -1,6 +1,7 @@
#![feature(async_fn_in_trait, impl_trait_projections)] #![feature(async_fn_in_trait, impl_trait_projections)]
use node_poll_variants::PollVariants; use node_poll_variants::PollVariants;
use physical_node::CriticalError;
use physical_node::transducer::input::Poll; use physical_node::transducer::input::Poll;
#[derive(PollVariants)] #[derive(PollVariants)]
@ -21,8 +22,8 @@ where
{ {
type Value = SecondT; type Value = SecondT;
async fn poll(&self) -> Self::Value { async fn poll(&self) -> Result<Self::Value, CriticalError> {
self.second Ok(self.second)
} }
} }

View File

@ -6,3 +6,5 @@ pub mod transducer;
pub mod cell { pub mod cell {
pub use physical::cell::*; pub use physical::cell::*;
} }
pub use physical::CriticalError;

View File

@ -1,11 +1,12 @@
use ads1256::{ 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 core::ops::DerefMut;
use embassy_sync::blocking_mutex::raw::RawMutex; use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::mutex::Mutex; use embassy_sync::mutex::Mutex;
use node_poll_variants::PollVariants; use node_poll_variants::PollVariants;
use physical_node::transducer::input::Poll; use physical_node::transducer::input::Poll;
use physical_node::CriticalError;
use uom::si::f32; use uom::si::f32;
#[derive(PollVariants)] #[derive(PollVariants)]
@ -36,8 +37,9 @@ where
{ {
type Value = f32::ElectricPotential; type Value = f32::ElectricPotential;
async fn poll(&self) -> Self::Value { async fn poll(&self) -> Result<Self::Value, CriticalError> {
self.ads1256 let result = self
.ads1256
.lock() .lock()
.await .await
.deref_mut() .deref_mut()
@ -49,13 +51,20 @@ where
self.input_mod.data_rate(), self.input_mod.data_rate(),
true, 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 multiplexer(self) -> Multiplexer;
fn gain(self) -> Gain;
fn status(self) -> Option<Status>; fn status(self) -> Option<Status>;
fn ad_control(self) -> Option<AdControl>; fn ad_control(self) -> Option<AdControl>;
@ -64,8 +73,12 @@ pub trait ModInput {
} }
/// buffer /// buffer
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInStatus { pub struct ModInStatus {
pub multiplexer: Multiplexer, 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 status: Status,
} }
@ -75,6 +88,11 @@ impl ModInput for ModInStatus {
self.multiplexer self.multiplexer
} }
#[inline(always)]
fn gain(self) -> Gain {
self.gain
}
#[inline(always)] #[inline(always)]
fn status(self) -> Option<Status> { fn status(self) -> Option<Status> {
Some(self.status) Some(self.status)
@ -92,6 +110,7 @@ impl ModInput for ModInStatus {
} }
/// gain /// gain
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInAdControl { pub struct ModInAdControl {
pub multiplexer: Multiplexer, pub multiplexer: Multiplexer,
pub ad_control: AdControl, pub ad_control: AdControl,
@ -103,6 +122,11 @@ impl ModInput for ModInAdControl {
self.multiplexer self.multiplexer
} }
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)] #[inline(always)]
fn status(self) -> Option<Status> { fn status(self) -> Option<Status> {
None None
@ -120,8 +144,12 @@ impl ModInput for ModInAdControl {
} }
/// data rate /// data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInDataRate { pub struct ModInDataRate {
pub multiplexer: Multiplexer, 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, pub data_rate: DataRate,
} }
@ -131,6 +159,11 @@ impl ModInput for ModInDataRate {
self.multiplexer self.multiplexer
} }
#[inline(always)]
fn gain(self) -> Gain {
self.gain
}
#[inline(always)] #[inline(always)]
fn status(self) -> Option<Status> { fn status(self) -> Option<Status> {
None None
@ -148,6 +181,7 @@ impl ModInput for ModInDataRate {
} }
/// buffer, gain /// buffer, gain
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInStatAdc { pub struct ModInStatAdc {
pub multiplexer: Multiplexer, pub multiplexer: Multiplexer,
pub status: Status, pub status: Status,
@ -160,6 +194,11 @@ impl ModInput for ModInStatAdc {
self.multiplexer self.multiplexer
} }
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)] #[inline(always)]
fn status(self) -> Option<Status> { fn status(self) -> Option<Status> {
Some(self.status) Some(self.status)
@ -177,8 +216,12 @@ impl ModInput for ModInStatAdc {
} }
/// buffer, data rate /// buffer, data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInStatDrate { pub struct ModInStatDrate {
pub multiplexer: Multiplexer, 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 status: Status,
pub data_rate: DataRate, pub data_rate: DataRate,
} }
@ -189,6 +232,11 @@ impl ModInput for ModInStatDrate {
self.multiplexer self.multiplexer
} }
#[inline(always)]
fn gain(self) -> Gain {
self.gain
}
#[inline(always)] #[inline(always)]
fn status(self) -> Option<Status> { fn status(self) -> Option<Status> {
Some(self.status) Some(self.status)
@ -206,6 +254,7 @@ impl ModInput for ModInStatDrate {
} }
// gain, data rate // gain, data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInAdcDrate { pub struct ModInAdcDrate {
pub multiplexer: Multiplexer, pub multiplexer: Multiplexer,
pub ad_control: AdControl, pub ad_control: AdControl,
@ -218,6 +267,11 @@ impl ModInput for ModInAdcDrate {
self.multiplexer self.multiplexer
} }
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)] #[inline(always)]
fn status(self) -> Option<Status> { fn status(self) -> Option<Status> {
None None
@ -235,6 +289,7 @@ impl ModInput for ModInAdcDrate {
} }
/// buffer, gain, data rate /// buffer, gain, data rate
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct ModInAll { pub struct ModInAll {
pub multiplexer: Multiplexer, pub multiplexer: Multiplexer,
pub status: Status, pub status: Status,
@ -248,6 +303,11 @@ impl ModInput for ModInAll {
self.multiplexer self.multiplexer
} }
#[inline(always)]
fn gain(self) -> Gain {
self.ad_control.gain()
}
#[inline(always)] #[inline(always)]
fn status(self) -> Option<Status> { fn status(self) -> Option<Status> {
Some(self.status) Some(self.status)

View File

@ -5,4 +5,4 @@ pub mod transducer;
pub mod cell; pub mod cell;
mod error; mod error;
pub use error::*; pub use error::CriticalError;

View File

@ -1,5 +1,7 @@
use crate::CriticalError;
pub trait Poll { pub trait Poll {
type Value: Copy; type Value: Copy;
async fn poll(&self) -> Self::Value; async fn poll(&self) -> Result<Self::Value, CriticalError>;
} }