diff --git a/src/detector.rs b/src/detector.rs index 441961d..0f25eae 100644 --- a/src/detector.rs +++ b/src/detector.rs @@ -19,8 +19,7 @@ use crate::{ constants::BLOCK_SIZE, pwm::PwmDriver, rtc::GlobalRtc, - state::{DEVICE_STATE, DeviceState}, - updates::{NET_CHANNEL, NetDataSender}, + updates::UpdateConnection, utils::{static_alloc, try_buffer, try_static_timestamped_block_vecs}, }; @@ -34,7 +33,7 @@ pub async fn detector_task( while !rtc.is_running().await { Timer::after_secs(2).await; } - let net_data = NET_CHANNEL.sender(); + info!("Allocating detector resources"); let blocks = unwrap!( @@ -47,7 +46,7 @@ pub async fn detector_task( let (mut sender, mut receiver) = buf_channel.split(); - let mut detector = Detector::new(adc, pwm, net_data); + let mut detector = Detector::new(adc, pwm); let mut samples = unwrap!(try_buffer(64), "Failed to allocate sample buffer"); let mut peaks = Vec::new(); @@ -72,7 +71,6 @@ struct Detector<'device> { adc: AdcDriver<'device, NoopRawMutex, DMA_CH1>, pwm: PwmDriver<'device>, state: DetectorState, - net_data: NetDataSender, } #[derive(Debug)] @@ -91,17 +89,12 @@ impl Default for DetectorState { } impl<'device> Detector<'device> { - fn new( - adc: AdcDriver<'device, NoopRawMutex, DMA_CH1>, - mut pwm: PwmDriver<'device>, - net_data: NetDataSender, - ) -> Self { + fn new(adc: AdcDriver<'device, NoopRawMutex, DMA_CH1>, mut pwm: PwmDriver<'device>) -> Self { pwm.enable(); Self { adc, pwm, - net_data, state: DetectorState::default(), } } @@ -175,15 +168,15 @@ impl Detector<'_> { if blips > 0 { self.state .strikes - .set(self.state.strikes.get().saturating_add(32)); + .update(|strike| strike.saturating_add(32)); - if let Some(net_data) = self.get_data_channel() { + if let Some(net_data) = UpdateConnection::can_update() { data::transmit_strike( *timestamp, buf.as_slice(), peaks.as_slice(), average, - net_data, + &net_data, ); } @@ -230,19 +223,13 @@ impl Detector<'_> { inactive = Some(Instant::now()); } None => { - if let Some(net_data) = self.get_data_channel() { + if let Some(net_data) = UpdateConnection::can_update() { let now = rtc.get_timestamp().await; - data::transmit_level_update(now.timestamp(), decay, net_data); + data::transmit_level_update(now.timestamp(), decay, &net_data); } } _ => continue, } } } - - fn get_data_channel(&self) -> Option<&NetDataSender> { - DEVICE_STATE - .lock(|x| x.get() == DeviceState::Connected) - .then_some(&self.net_data) - } } diff --git a/src/errors.rs b/src/errors.rs index 39c8d28..6cd32af 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -2,7 +2,6 @@ use alloc::collections::TryReserveError; use embassy_rp::rtc::RtcError; use sachy_sntp::SntpError; - #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum PicoError { diff --git a/src/net.rs b/src/net.rs index 55a7e94..2561ce3 100644 --- a/src/net.rs +++ b/src/net.rs @@ -13,8 +13,7 @@ use sachy_sntp::SntpSocket; use crate::{ constants::{HOST_NAME, HOST_PORT}, rtc::GlobalRtc, - state::DEVICE_STATE, - updates::{NET_CHANNEL, NetDataReceiver}, + updates::UpdateConnection, }; #[embassy_executor::task] @@ -102,33 +101,33 @@ pub async fn tcp_stack(stack: embassy_net::Stack<'static>) { select(stack.wait_link_down(), data_loop(&mut tcp)).await; - DEVICE_STATE.lock(|state| state.set(crate::state::DeviceState::Disconnected)); + UpdateConnection::disconnect(); } async fn data_loop<'connection>(tcp: &mut TcpSocket<'connection>) { let mut buffer: Vec = vec![0u8; 2048]; - let net_data: NetDataReceiver = NET_CHANNEL.receiver(); + let net_data = UpdateConnection::get_receiver(); - 'outer: loop { - DEVICE_STATE.lock(|state| state.set(crate::state::DeviceState::Disconnected)); + loop { + UpdateConnection::disconnect(); if tcp.accept(HOST_PORT).await.is_err() { continue; } - DEVICE_STATE.lock(|state| state.set(crate::state::DeviceState::Connected)); + UpdateConnection::connect(); - loop { + 'inner: loop { let data = net_data.receive().await; if let Ok(written) = postcard::to_slice(&data, &mut buffer) && let Err(_) = tcp.write(written).await { - continue 'outer; + break 'inner; } if tcp.flush().await.is_err() { - continue 'outer; + break 'inner; } } } diff --git a/src/updates.rs b/src/updates.rs index db8d2b9..8a2bee0 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -2,7 +2,10 @@ use alloc::vec::Vec; use embassy_sync::channel::{Channel, Receiver, Sender}; -use crate::locks::NetDataLock; +use crate::{ + locks::NetDataLock, + state::{DEVICE_STATE, DeviceState}, +}; pub type NetDataChannel = Channel; pub type NetDataSender = Sender<'static, NetDataLock, Update, 8>; @@ -22,4 +25,28 @@ pub enum Update { }, } -pub static NET_CHANNEL: NetDataChannel = Channel::new(); +static NET_CHANNEL: NetDataChannel = Channel::new(); + +pub struct UpdateConnection; + +impl UpdateConnection { + pub fn disconnect() { + DEVICE_STATE.lock(|state| state.set(DeviceState::Disconnected)); + } + + pub fn connect() { + DEVICE_STATE.lock(|state| state.set(DeviceState::Connected)); + } + + pub fn is_connected() -> bool { + DEVICE_STATE.lock(|state| state.get() == DeviceState::Connected) + } + + pub fn get_receiver() -> NetDataReceiver { + NET_CHANNEL.receiver() + } + + pub fn can_update() -> Option { + Self::is_connected().then(|| NET_CHANNEL.sender()) + } +}