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

@ -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<Self::Value, CriticalError> {
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<Status>;
fn ad_control(self) -> Option<AdControl>;
@ -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<Status> {
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<Status> {
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<Status> {
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<Status> {
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<Status> {
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<Status> {
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<Status> {
Some(self.status)