mirror of
https://github.com/hubaldv/bioz-firmware-rs.git
synced 2025-12-06 05:01:18 +00:00
Use signals instead of mutex.
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
use defmt::info;
|
use defmt::info;
|
||||||
|
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_sync::mutex::Mutex;
|
|
||||||
use embassy_sync::blocking_mutex::raw::{ThreadModeRawMutex, CriticalSectionRawMutex};
|
use embassy_sync::blocking_mutex::raw::{ThreadModeRawMutex, CriticalSectionRawMutex};
|
||||||
use embassy_sync::signal::Signal;
|
use embassy_sync::signal::Signal;
|
||||||
|
use embassy_futures::select::{select, Either};
|
||||||
use embassy_stm32::usb::Driver;
|
use embassy_stm32::usb::Driver;
|
||||||
use embassy_stm32::{peripherals, uid, usb};
|
use embassy_stm32::{peripherals, uid, usb};
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ pub async fn set_green_led_handler(_context: &mut Context, _header: VarHeader, r
|
|||||||
LED_FREQUENCY_SIGNAL.signal(rqst);
|
LED_FREQUENCY_SIGNAL.signal(rqst);
|
||||||
}
|
}
|
||||||
|
|
||||||
static STOP: Mutex<ThreadModeRawMutex, bool> = Mutex::new(false);
|
static STOP: Signal<CriticalSectionRawMutex, ()> = Signal::new();
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn start_single_impedance_handler(context: SpawnCtx, header: VarHeader, rqst: StartImpedance, sender: Sender<AppTx>) {
|
pub async fn start_single_impedance_handler(context: SpawnCtx, header: VarHeader, rqst: StartImpedance, sender: Sender<AppTx>) {
|
||||||
@@ -183,31 +183,39 @@ pub async fn start_single_impedance_handler(context: SpawnCtx, header: VarHeader
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut seq: u8 = 0;
|
let mut seq: u8 = 0;
|
||||||
while !*STOP.lock().await {
|
loop {
|
||||||
|
let stop_fut = STOP.wait();
|
||||||
|
let recv_fut = IMPEDANCE_CHANNEL.receive();
|
||||||
|
|
||||||
let msg = IMPEDANCE_CHANNEL.receive().await;
|
match select(stop_fut, recv_fut).await {
|
||||||
|
Either::First(_) => {
|
||||||
if sender
|
info!("Stop signal received.");
|
||||||
.publish::<ImpedanceOutputTopic>(seq.into(), &msg)
|
break;
|
||||||
.await
|
}
|
||||||
.is_err()
|
Either::Second(msg) => {
|
||||||
{
|
if sender
|
||||||
defmt::error!("Topic send error!");
|
.publish::<ImpedanceOutputTopic>(seq.into(), &msg)
|
||||||
break;
|
.await
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
defmt::error!("Topic send error!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
seq = seq.wrapping_add(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
seq = seq.wrapping_add(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context.impedance_setup.lock().await.running = false;
|
context.impedance_setup.lock().await.running = false;
|
||||||
info!("Impedance measurement stopped.");
|
info!("Impedance measurement stopped.");
|
||||||
*STOP.lock().await = false;
|
STOP.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stop_single_impedance_handler(context: &mut Context, _header: VarHeader, _rqst: ()) -> bool {
|
pub async fn stop_single_impedance_handler(context: &mut Context, _header: VarHeader, _rqst: ()) -> bool {
|
||||||
info!("Stop impedance measurement");
|
info!("Stop impedance measurement");
|
||||||
let was_busy = context.impedance_setup.lock().await.running;
|
let was_busy = context.impedance_setup.lock().await.running;
|
||||||
if was_busy {
|
if was_busy {
|
||||||
*STOP.lock().await = true;
|
STOP.signal(());
|
||||||
}
|
}
|
||||||
was_busy
|
was_busy
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user