Switch to fully composition based Transducers.
This commit is contained in:
@ -1,105 +1,68 @@
|
||||
use crate::cell::CellView;
|
||||
use crate::transducer::Publisher;
|
||||
use crate::transducer::{Publish, Publisher};
|
||||
use core::cell::Cell;
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
use embassy_sync::blocking_mutex::raw::RawMutex;
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
use embassy_sync::pubsub::PubSubChannel;
|
||||
use embassy_sync::pubsub::{Error, PubSubBehavior, Subscriber};
|
||||
use embassy_sync::pubsub::{Error, PubSubChannel, Subscriber};
|
||||
pub use physical::transducer::input::*;
|
||||
use physical::transducer::Stateful;
|
||||
use physical::transducer::{State, Stateful};
|
||||
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
pub struct RawPublishInput<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
{
|
||||
pub channel: PubSubChannel<MutexT, T, CAPACITY, NUM_SUBS, 0>,
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
RawPublishInput<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>
|
||||
Publisher<CAPACITY, NUM_SUBS> for RawPublishInput<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.channel.subscriber()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
pub struct RawStatePubInput<
|
||||
pub struct StatefulPublisher<
|
||||
T: Copy,
|
||||
MutexT: RawMutex,
|
||||
const CAPACITY: usize,
|
||||
const NUM_SUBS: usize,
|
||||
> {
|
||||
pub state_cell: Cell<T>,
|
||||
pub channel: PubSubChannel<MutexT, T, CAPACITY, NUM_SUBS, 0>,
|
||||
pub state: State<T>,
|
||||
pub publisher: Publisher<T, MutexT, CAPACITY, NUM_SUBS>,
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
RawStatePubInput<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
StatefulPublisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
#[inline(always)]
|
||||
pub fn new(
|
||||
state_cell: Cell<T>,
|
||||
channel: PubSubChannel<MutexT, T, CAPACITY, NUM_SUBS, 0>,
|
||||
state: State<T>,
|
||||
publisher: Publisher<T, MutexT, CAPACITY, NUM_SUBS>,
|
||||
) -> Self {
|
||||
Self {
|
||||
state_cell,
|
||||
channel,
|
||||
}
|
||||
Self { state, publisher }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn update(&self, value: T) {
|
||||
self.state_cell.set(value);
|
||||
self.channel.publish_immediate(value);
|
||||
self.state.update(value);
|
||||
self.publisher.update(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize> Stateful
|
||||
for RawStatePubInput<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
for StatefulPublisher<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
{
|
||||
type Value = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn state_cell(&self) -> CellView<Self::Value> {
|
||||
(&self.state_cell).into()
|
||||
self.state.state_cell()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn state(&self) -> Self::Value {
|
||||
self.state_cell.get()
|
||||
self.state.state()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, MutexT: RawMutex, const CAPACITY: usize, const NUM_SUBS: usize>
|
||||
Publisher<CAPACITY, NUM_SUBS> for RawStatePubInput<T, MutexT, CAPACITY, NUM_SUBS>
|
||||
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.channel.subscriber()
|
||||
self.publisher.subscribe()
|
||||
}
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ use embassy_sync::blocking_mutex::raw::RawMutex;
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
use embassy_sync::pubsub;
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
use embassy_sync::pubsub::Subscriber;
|
||||
use embassy_sync::pubsub::{PubSubBehavior, PubSubChannel, Subscriber};
|
||||
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
pub trait Publisher<const CAPACITY: usize, const NUM_SUBS: usize> {
|
||||
pub trait Publish<const CAPACITY: usize, const NUM_SUBS: usize> {
|
||||
type Value: Copy;
|
||||
type Mutex: RawMutex;
|
||||
|
||||
@ -19,3 +19,46 @@ pub trait Publisher<const CAPACITY: usize, const NUM_SUBS: usize> {
|
||||
&self,
|
||||
) -> Result<Subscriber<Self::Mutex, Self::Value, CAPACITY, NUM_SUBS, 0>, pubsub::Error>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "embassy-sync")]
|
||||
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