#![cfg(feature = "rangefinder")]
use embassy_sync::{
blocking_mutex::raw::CriticalSectionRawMutex,
pubsub::{PubSubChannel, Publisher, Subscriber},
};
use static_cell::StaticCell;
use crate::rangefinder_sensors::{Rangefinder, RangefinderDevice, RangefinderMessage};
const MAX_RANGEFINDER_SUBSCRIBER_COUNT: usize = 4;
const RANGEFINDER_PUBLISHER_COUNT: usize = 1;
const RANGEFINDER_PUB_SUB_CAPACITY: usize = 1;
static RANGEFINDER_PUB_SUB_CHANNEL: PubSubChannel<
CriticalSectionRawMutex,
RangefinderMessage,
RANGEFINDER_PUB_SUB_CAPACITY,
MAX_RANGEFINDER_SUBSCRIBER_COUNT,
RANGEFINDER_PUBLISHER_COUNT,
> = PubSubChannel::new();
type RangefinderPublisher = Publisher<
'static,
CriticalSectionRawMutex,
RangefinderMessage,
RANGEFINDER_PUB_SUB_CAPACITY,
MAX_RANGEFINDER_SUBSCRIBER_COUNT,
RANGEFINDER_PUBLISHER_COUNT,
>;
pub type RangefinderSubscriber = Subscriber<
'static,
CriticalSectionRawMutex,
RangefinderMessage,
RANGEFINDER_PUB_SUB_CAPACITY,
MAX_RANGEFINDER_SUBSCRIBER_COUNT,
RANGEFINDER_PUBLISHER_COUNT,
>;
#[allow(clippy::expect_used)]
pub fn rangefinder_subscriber() -> RangefinderSubscriber {
RANGEFINDER_PUB_SUB_CHANNEL.subscriber().expect("rangefinder_subscriber failed")
}
static RANGEFINDER_CTX: StaticCell<RangefinderContext> = StaticCell::new();
pub struct RangefinderContext {
pub rangefinder: Rangefinder,
pub rangefinder_publisher: RangefinderPublisher,
}
impl RangefinderContext {
pub fn new(rangefinder: Rangefinder) -> Self {
#[allow(clippy::expect_used)]
Self {
rangefinder,
rangefinder_publisher: RANGEFINDER_PUB_SUB_CHANNEL.publisher().expect("rangefinder_publisher failed"),
}
}
}
pub fn init(rangefinder: Rangefinder) -> &'static mut RangefinderContext {
RANGEFINDER_CTX.init(RangefinderContext::new(rangefinder))
}
#[embassy_executor::task]
pub async fn run(ctx: &'static mut RangefinderContext) {
let mut ticker = embassy_time::Ticker::every(embassy_time::Duration::from_hz(40));
let mut loop_count: u32 = 0;
log::info!(" RANGEFINDER: task started");
loop {
ticker.next().await;
let rangefinder_message = ctx.rangefinder.message();
ctx.rangefinder_publisher.publish_immediate(rangefinder_message);
if loop_count.is_multiple_of(10) {
log::info!(" RANGE: loop {loop_count}");
}
loop_count = loop_count.wrapping_add(1);
}
}