Use signals instead of mutex.

This commit is contained in:
2025-09-16 16:19:33 +02:00
parent 230602f7c8
commit d150c70700

View File

@@ -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,31 +183,39 @@ pub async fn start_single_impedance_handler(context: SpawnCtx, header: VarHeader
}
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;
if sender
.publish::<ImpedanceOutputTopic>(seq.into(), &msg)
.await
.is_err()
{
defmt::error!("Topic send error!");
break;
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
.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;
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
}