Removed a bunch of over abstracted bullshit.
This commit is contained in:
2
src/adc/mod.rs
Normal file
2
src/adc/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
#[cfg(feature = "stm32")]
|
||||
pub mod stm32;
|
13
src/adc/stm32.rs
Normal file
13
src/adc/stm32.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use uom::si::electric_potential::millivolt;
|
||||
use uom::si::quantities::ElectricPotential;
|
||||
|
||||
pub fn reading_to_voltage(
|
||||
reading: u16,
|
||||
reference_voltage: ElectricPotential<f32>,
|
||||
v_ref_int_scale: f32,
|
||||
) -> ElectricPotential<f32> {
|
||||
let reference_mv = reference_voltage.get::<millivolt>();
|
||||
let reading = f32::from(reading);
|
||||
|
||||
ElectricPotential::new::<millivolt>(reading * v_ref_int_scale / reference_mv)
|
||||
}
|
20
src/cell.rs
20
src/cell.rs
@ -1,20 +0,0 @@
|
||||
use core::cell::Cell;
|
||||
|
||||
/// Provides a view only reference to a [Cell].
|
||||
/// Useful alternative to `&Cell` when an API wants to control where a [Cell]s value can be set.
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct CellView<'a, T: Copy>(&'a Cell<T>);
|
||||
|
||||
impl<T: Copy> CellView<'_, T> {
|
||||
#[inline(always)]
|
||||
pub fn get(self) -> T {
|
||||
self.0.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Copy> From<&'a Cell<T>> for CellView<'a, T> {
|
||||
#[inline(always)]
|
||||
fn from(value: &'a Cell<T>) -> Self {
|
||||
CellView(value)
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
use crate::transducer::InvalidValue;
|
||||
/// Indicates the transducer value is statically known to be impossible.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub struct InvalidValue;
|
||||
|
||||
/// An error that it is likely impossible to recover from. This error should only be created in
|
||||
/// situations where attempts to recover have already been attempted and have failed. Error handling
|
||||
|
@ -2,7 +2,8 @@
|
||||
|
||||
pub mod transducer;
|
||||
pub mod control;
|
||||
pub mod cell;
|
||||
pub mod error;
|
||||
|
||||
pub mod adc;
|
||||
|
||||
pub use error::CriticalError;
|
||||
|
@ -1,6 +0,0 @@
|
||||
pub trait Poll {
|
||||
type Value: Copy;
|
||||
type Error: Copy;
|
||||
|
||||
async fn poll(&self) -> Result<Self::Value, Self::Error>;
|
||||
}
|
@ -1,62 +1,3 @@
|
||||
use core::cell::Cell;
|
||||
use crate::cell::CellView;
|
||||
|
||||
pub mod input;
|
||||
pub mod output;
|
||||
mod part;
|
||||
|
||||
pub use part::*;
|
||||
|
||||
// Initialisation will always be async and won't complete until a state is available for all
|
||||
// stateful transducers.
|
||||
pub trait Stateful {
|
||||
type Value: Copy;
|
||||
|
||||
fn state_cell(&self) -> CellView<Self::Value>;
|
||||
|
||||
fn state(&self) -> Self::Value;
|
||||
}
|
||||
|
||||
pub struct State<T: Copy> {
|
||||
state_cell: Cell<T>,
|
||||
}
|
||||
|
||||
impl<T: Copy> State<T> {
|
||||
#[inline(always)]
|
||||
pub fn new(state_cell: Cell<T>) -> Self {
|
||||
Self { state_cell }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn update(&self, value: T) {
|
||||
self.state_cell.set(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Stateful for State<T> {
|
||||
type Value = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn state_cell(&self) -> CellView<Self::Value> {
|
||||
(&self.state_cell).into()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn state(&self) -> Self::Value {
|
||||
self.state_cell.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> From<Cell<T>> for State<T> {
|
||||
#[inline(always)]
|
||||
fn from(state_cell: Cell<T>) -> Self {
|
||||
State::new(state_cell)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Error ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
/// Indicates the transducer value is statically known to be impossible.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub struct InvalidValue;
|
||||
pub use part::*;
|
@ -1,8 +0,0 @@
|
||||
use crate::transducer::InvalidValue;
|
||||
|
||||
pub trait Output {
|
||||
type Value: Copy;
|
||||
|
||||
//TODO: Should this be maybe async?
|
||||
fn output(&mut self, setting: Self::Value) -> Result<(), InvalidValue>;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
use crate::transducer::InvalidValue;
|
||||
use crate::error::InvalidValue;
|
||||
use uom::si::electric_potential::volt;
|
||||
use uom::si::quantities::{ElectricPotential, ThermodynamicTemperature};
|
||||
use uom::si::thermodynamic_temperature::degree_celsius;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::transducer::InvalidValue;
|
||||
use crate::error::InvalidValue;
|
||||
use libm::powf;
|
||||
use uom::si::electric_potential::millivolt;
|
||||
use uom::si::quantities::{ElectricPotential, ThermodynamicTemperature};
|
||||
|
Reference in New Issue
Block a user