Created new impedance file for impedance_setup.

This commit is contained in:
2025-08-17 19:04:08 +02:00
parent 3db4131784
commit eaeff4f527
6 changed files with 229 additions and 179 deletions

View File

@@ -1,7 +1,6 @@
use defmt::info;
use embassy_executor::Spawner;
use embassy_time::{Ticker, Duration};
use embassy_sync::mutex::Mutex;
use embassy_sync::blocking_mutex::raw::{ThreadModeRawMutex, CriticalSectionRawMutex};
use embassy_sync::signal::Signal;
@@ -20,9 +19,9 @@ use postcard_rpc::{
},
};
use bioz_icd_rs::{PingEndpoint, GetUniqueIdEndpoint, SetGreenLedEndpoint, StartImpedanceEndpoint, StopImpedanceEndpoint, StartImpedance, Impedance, ImpedanceTopic, ENDPOINT_LIST, TOPICS_IN_LIST, TOPICS_OUT_LIST};
use bioz_icd_rs::{PingEndpoint, GetUniqueIdEndpoint, SetGreenLedEndpoint, StartImpedanceEndpoint, StopImpedanceEndpoint, StartImpedance, ImpedanceOutputTopic, ENDPOINT_LIST, TOPICS_IN_LIST, TOPICS_OUT_LIST};
use crate::impedance_test::{ImpedanceTest, IMPEDANCE_TEST, IMPEDANCE_CHANNEL};
use crate::impedance::{IMPEDANCE_CHANNEL};
// Postcard RPC types
type AppDriver = usb::Driver<'static, peripherals::USB>;
@@ -38,17 +37,15 @@ static STORAGE: AppStorage = AppStorage::new();
pub struct Context {
pub unique_id: [u8; 12],
pub impedance: &'static Mutex<ThreadModeRawMutex, ImpedanceTest>,
}
pub struct SpawnCtx {
pub impedance: &'static Mutex<ThreadModeRawMutex, ImpedanceTest>,
}
impl SpawnContext for Context {
type SpawnCtxt = SpawnCtx;
fn spawn_ctxt(&mut self) -> Self::SpawnCtxt {
SpawnCtx { impedance: &self.impedance }
SpawnCtx {}
}
}
@@ -119,10 +116,8 @@ pub fn init_communication(usb_driver: Driver<'static, peripherals::USB>, spawner
let pbufs = PBUFS.take();
let config = usb_config();
let impedance_ref = IMPEDANCE_TEST.init(embassy_sync::mutex::Mutex::new(ImpedanceTest::new()));
let context = Context {
unique_id: *uid::uid(),
impedance: impedance_ref,
};
let (device, tx_impl, rx_impl) = STORAGE.init(usb_driver, config, pbufs.tx_buf.as_mut_slice());
@@ -159,13 +154,14 @@ pub async fn set_green_led_handler(_context: &mut Context, _header: VarHeader, r
LED_FREQUENCY_SIGNAL.signal(rqst);
}
static RUNNING: Mutex<ThreadModeRawMutex, bool> = Mutex::new(false);
static STOP: Mutex<ThreadModeRawMutex, bool> = Mutex::new(false);
#[embassy_executor::task]
pub async fn start_impedance_handler(context: SpawnCtx, header: VarHeader, rqst: StartImpedance, sender: Sender<AppTx>) {
pub async fn start_impedance_handler(_context: SpawnCtx, header: VarHeader, rqst: StartImpedance, sender: Sender<AppTx>) {
info!("Start impedance measurement with rate {:?} Hz.", rqst.update_frequency);
let mut impedance = context.impedance.lock().await;
*RUNNING.lock().await = true;
if sender
.reply::<StartImpedanceEndpoint>(header.seq_no, &())
@@ -176,28 +172,13 @@ pub async fn start_impedance_handler(context: SpawnCtx, header: VarHeader, rqst:
return;
}
let mut ticker = Ticker::every(Duration::from_hz(rqst.update_frequency.into()));
let mut seq: u8 = 0;
while !*STOP.lock().await {
// ticker.next().await;
// impedance.update(rqst.sinus_frequency);
// let msg = Impedance {
// magnitude: impedance.magnitude,
// phase: impedance.phase,
// };
let msg = IMPEDANCE_CHANNEL.receive().await;
// let msg = Impedance {
// magnitude: data,
// phase: data,
// };
if sender
.publish::<ImpedanceTopic>(seq.into(), &msg)
.publish::<ImpedanceOutputTopic>(seq.into(), &msg)
.await
.is_err()
{
@@ -206,14 +187,15 @@ pub async fn start_impedance_handler(context: SpawnCtx, header: VarHeader, rqst:
}
seq = seq.wrapping_add(1);
}
*RUNNING.lock().await = false;
info!("Impedance measurement stopped.");
*STOP.lock().await = false;
}
pub async fn stop_impedance_handler(context: &mut Context, _header: VarHeader, _rqst: ()) -> bool {
pub async fn stop_impedance_handler(_context: &mut Context, _header: VarHeader, _rqst: ()) -> bool {
info!("Stop impedance measurement");
let was_busy = context.impedance.try_lock().is_err();
let was_busy = *RUNNING.lock().await;
if was_busy {
*STOP.lock().await = true;
}