Initial node implementation (#4)
Reviewed-on: #4 Co-authored-by: Zack <zack@bfpower.io> Co-committed-by: Zack <zack@bfpower.io>
This commit is contained in:
10
node/src/lib.rs
Normal file
10
node/src/lib.rs
Normal file
@ -0,0 +1,10 @@
|
||||
#![no_std]
|
||||
#![feature(async_fn_in_trait)]
|
||||
|
||||
pub mod transducer;
|
||||
|
||||
pub mod cell {
|
||||
pub use physical::cell::*;
|
||||
}
|
||||
|
||||
pub use physical::CriticalError;
|
1
node/src/transducer/input.rs
Normal file
1
node/src/transducer/input.rs
Normal file
@ -0,0 +1 @@
|
||||
pub use physical::transducer::input::*;
|
8
node/src/transducer/mod.rs
Normal file
8
node/src/transducer/mod.rs
Normal file
@ -0,0 +1,8 @@
|
||||
pub mod input;
|
||||
pub mod output;
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
mod sync;
|
||||
|
||||
pub use physical::transducer::*;
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
pub use sync::*;
|
1
node/src/transducer/output.rs
Normal file
1
node/src/transducer/output.rs
Normal file
@ -0,0 +1 @@
|
||||
pub use physical::transducer::output::*;
|
61
node/src/transducer/sync/input.rs
Normal file
61
node/src/transducer/sync/input.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use crate::cell::CellView;
|
||||
use crate::transducer::{Publish, Publisher};
|
||||
use core::cell::Cell;
|
||||
use embassy_sync::blocking_mutex::raw::RawMutex;
|
||||
use embassy_sync::pubsub::{Error, PubSubChannel, Subscriber};
|
||||
use physical::transducer::{State, Stateful};
|
||||
|
||||
pub struct StatefulPublisher<
|
||||
T: Copy,
|
||||
MutexT: RawMutex,
|
||||
const CAPACITY: usize,
|
||||
const NUM_SUBS: usize,
|
||||
> {
|
||||
pub state: State<T>,
|
||||
pub publisher: Publisher<T, MutexT, CAPACITY, NUM_SUBS>,
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
StatefulPublisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
#[inline(always)]
|
||||
pub fn new(state: State<T>, publisher: Publisher<T, MutexT, CAPACITY, NUM_SUBS>) -> Self {
|
||||
Self { state, publisher }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn update(&self, value: T) {
|
||||
self.state.update(value);
|
||||
self.publisher.update(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize> Stateful
|
||||
for StatefulPublisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn state_cell(&self) -> CellView<Self::Value> {
|
||||
self.state.state_cell()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn state(&self) -> Self::Value {
|
||||
self.state.state()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
Publish<CAPACITY, NUM_SUBS> for StatefulPublisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
type Value = T;
|
||||
type Mutex = MutexT;
|
||||
|
||||
#[inline(always)]
|
||||
fn subscribe(
|
||||
&self,
|
||||
) -> Result<Subscriber<Self::Mutex, Self::Value, CAPACITY, NUM_SUBS, 0>, Error> {
|
||||
self.publisher.subscribe()
|
||||
}
|
||||
}
|
58
node/src/transducer/sync/mod.rs
Normal file
58
node/src/transducer/sync/mod.rs
Normal file
@ -0,0 +1,58 @@
|
||||
mod input;
|
||||
|
||||
pub use input::*;
|
||||
|
||||
use embassy_sync::blocking_mutex::raw::RawMutex;
|
||||
use embassy_sync::pubsub;
|
||||
use embassy_sync::pubsub::{PubSubBehavior, PubSubChannel, Subscriber};
|
||||
|
||||
pub trait Publish<const CAPACITY: usize, const NUM_SUBS: usize> {
|
||||
type Value: Copy;
|
||||
type Mutex: RawMutex;
|
||||
|
||||
fn subscribe(
|
||||
&self,
|
||||
) -> Result<Subscriber<Self::Mutex, Self::Value, CAPACITY, NUM_SUBS, 0>, pubsub::Error>;
|
||||
}
|
||||
|
||||
pub struct Publisher<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize> {
|
||||
channel: PubSubChannel<MutexT, T, CAPACITY, NUM_SUBS, 0>,
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
Publisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
#[inline(always)]
|
||||
pub fn new(channel: PubSubChannel<MutexT, T, CAPACITY, NUM_SUBS, 0>) -> Self {
|
||||
Self { channel }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn update(&self, value: T) {
|
||||
self.channel.publish_immediate(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
Publish<CAPACITY, NUM_SUBS> for Publisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
type Value = T;
|
||||
type Mutex = MutexT;
|
||||
|
||||
#[inline(always)]
|
||||
fn subscribe(
|
||||
&self,
|
||||
) -> Result<Subscriber<Self::Mutex, Self::Value, CAPACITY, NUM_SUBS, 0>, pubsub::Error> {
|
||||
self.channel.subscriber()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
From<PubSubChannel<MutexT, T, CAPACITY, NUM_SUBS, 0>>
|
||||
for Publisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
#[inline(always)]
|
||||
fn from(channel: PubSubChannel<MutexT, T, CAPACITY, NUM_SUBS, 0>) -> Self {
|
||||
Publisher::new(channel)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user