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 embassy_executor::Spawner;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use embassy_sync::blocking_mutex::raw::{ThreadModeRawMutex, CriticalSectionRawMutex};
|
||||
use embassy_sync::signal::Signal;
|
||||
use embassy_futures::select::{select, Either};
|
||||
use embassy_stm32::usb::Driver;
|
||||
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);
|
||||
}
|
||||
|
||||
static STOP: Mutex<ThreadModeRawMutex, bool> = Mutex::new(false);
|
||||
static STOP: Signal<CriticalSectionRawMutex, ()> = Signal::new();
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn start_single_impedance_handler(context: SpawnCtx, header: VarHeader, rqst: StartImpedance, sender: Sender<AppTx>) {
|
||||
@@ -183,10 +183,16 @@ pub async fn start_single_impedance_handler(context: SpawnCtx, header: VarHeader
|
||||
}
|
||||
|
||||
let mut seq: u8 = 0;
|
||||
while !*STOP.lock().await {
|
||||
|
||||
let msg = IMPEDANCE_CHANNEL.receive().await;
|
||||
loop {
|
||||
let stop_fut = STOP.wait();
|
||||
let recv_fut = IMPEDANCE_CHANNEL.receive();
|
||||
|
||||
match select(stop_fut, recv_fut).await {
|
||||
Either::First(_) => {
|
||||
info!("Stop signal received.");
|
||||
break;
|
||||
}
|
||||
Either::Second(msg) => {
|
||||
if sender
|
||||
.publish::<ImpedanceOutputTopic>(seq.into(), &msg)
|
||||
.await
|
||||
@@ -197,17 +203,19 @@ pub async fn start_single_impedance_handler(context: SpawnCtx, header: VarHeader
|
||||
}
|
||||
seq = seq.wrapping_add(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.impedance_setup.lock().await.running = false;
|
||||
info!("Impedance measurement stopped.");
|
||||
*STOP.lock().await = false;
|
||||
STOP.reset();
|
||||
}
|
||||
|
||||
pub async fn stop_single_impedance_handler(context: &mut Context, _header: VarHeader, _rqst: ()) -> bool {
|
||||
info!("Stop impedance measurement");
|
||||
let was_busy = context.impedance_setup.lock().await.running;
|
||||
if was_busy {
|
||||
*STOP.lock().await = true;
|
||||
STOP.signal(());
|
||||
}
|
||||
was_busy
|
||||
}
|
||||
Reference in New Issue
Block a user