6fc828e864
Reviewed-on: #4 Co-authored-by: Zack <zack@bfpower.io> Co-committed-by: Zack <zack@bfpower.io>
58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
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)
|
|
}
|
|
} |