From e77bc9fcb4e54fbfabfcdfe6cda98c7b1945b9f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 6 Oct 2023 22:05:59 +0200 Subject: [PATCH] Remove some panics --- .../drivers/battery_monitor/battery_fg.rs | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/board/drivers/battery_monitor/battery_fg.rs b/src/board/drivers/battery_monitor/battery_fg.rs index 262609c6..e7424bd4 100644 --- a/src/board/drivers/battery_monitor/battery_fg.rs +++ b/src/board/drivers/battery_monitor/battery_fg.rs @@ -29,15 +29,19 @@ where Self { fg, enable } } - pub async fn enable(&mut self, delay: &mut D) { - unwrap!(self.enable.set_high().ok()); + pub async fn enable(&mut self, delay: &mut D) -> Result<(), ()> { + self.enable.set_high().map_err(|_| ())?; delay.delay_ms(10).await; - unwrap!(self.fg.load_initial_config_async(delay).await.ok()); + self.fg + .load_initial_config_async(delay) + .await + .map_err(|_| ())?; + Ok(()) } pub async fn read_data(&mut self) -> Result { - let voltage_uv = unwrap!(self.fg.read_vcell().await.ok()); - let percentage = unwrap!(self.fg.read_reported_soc().await.ok()); + let voltage_uv = self.fg.read_vcell().await.map_err(|_| ())?; + let percentage = self.fg.read_reported_soc().await.map_err(|_| ())?; Ok(BatteryFgData { voltage: (voltage_uv / 1000) as u16, // mV @@ -63,18 +67,20 @@ pub async fn monitor_task_fg( let mut timer = Ticker::every(Duration::from_secs(1)); info!("Fuel gauge monitor started"); - fuel_gauge.lock().await.enable(&mut Delay).await; + if fuel_gauge.lock().await.enable(&mut Delay).await.is_ok() { + loop { + if let Ok(data) = fuel_gauge.lock().await.read_data().await { + let mut state = battery_state.lock().await; + state.data = Some(data); + debug!("Battery data: {:?}", data); + } else { + error!("Failed to read battery data"); + } - loop { - let data = unwrap!(fuel_gauge.lock().await.read_data().await); - - { - let mut state = battery_state.lock().await; - state.data = Some(data); + timer.next().await; } - debug!("Battery data: {:?}", data); - - timer.next().await; + } else { + error!("Failed to enable fuel gauge"); } }) .await;