chore: cargo fmt

This commit is contained in:
2025-06-20 16:26:03 +02:00
parent 017078ef8e
commit 4b919b9613
10 changed files with 335 additions and 235 deletions

View File

@@ -1,3 +1,4 @@
use crate::to_string;
use anyhow::bail;
use bq34z100::{Bq34Z100Error, Bq34z100g1, Bq34z100g1Driver};
use embedded_hal_bus::i2c::MutexDevice;
@@ -6,7 +7,6 @@ use esp_idf_hal::i2c::{I2cDriver, I2cError};
use measurements::Temperature;
use serde::Serialize;
use std::result::Result::Ok as OkStd;
use crate::to_string;
pub trait BatteryInteraction {
fn state_charge_percent(&mut self) -> anyhow::Result<u8>;
@@ -31,29 +31,24 @@ pub struct BatteryState {
state_of_health: String,
temperature: String,
}
pub enum BatteryMonitor<'a> {
Disabled {
},
Disabled {},
BQ34Z100G1 {
battery_driver: Bq34z100g1Driver<MutexDevice<'a, I2cDriver<'a>>, Delay>
battery_driver: Bq34z100g1Driver<MutexDevice<'a, I2cDriver<'a>>, Delay>,
},
WchI2cSlave {
}
WchI2cSlave {},
}
impl BatteryInteraction for BatteryMonitor<'_> {
fn state_charge_percent(&mut self) -> anyhow::Result<u8> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
BatteryMonitor::BQ34Z100G1 { battery_driver } => {
match battery_driver.state_of_charge() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading SoC {:?}", err),
}
},
}
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
}
@@ -65,128 +60,122 @@ impl BatteryInteraction for BatteryMonitor<'_> {
fn remaining_milli_ampere_hour(&mut self) -> anyhow::Result<u16> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
match battery_driver.remaining_capacity(){
BatteryMonitor::BQ34Z100G1 { battery_driver } => {
match battery_driver.remaining_capacity() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading remaining_milli_ampere_hour {:?}", err),
}
},
}
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
}
fn max_milli_ampere_hour(&mut self) -> anyhow::Result<u16> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
BatteryMonitor::BQ34Z100G1 { battery_driver } => {
match battery_driver.full_charge_capacity() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading max_milli_ampere_hour {:?}", err),
}
},
}
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
}
fn design_milli_ampere_hour(&mut self) -> anyhow::Result<u16> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
BatteryMonitor::BQ34Z100G1 { battery_driver } => {
match battery_driver.design_capacity() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading design_milli_ampere_hour {:?}", err),
}
},
}
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
}
fn voltage_milli_volt(&mut self) -> anyhow::Result<u16> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
match battery_driver.voltage() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading voltage_milli_volt {:?}", err),
}
BatteryMonitor::BQ34Z100G1 { battery_driver } => match battery_driver.voltage() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading voltage_milli_volt {:?}", err),
},
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
}
fn average_current_milli_ampere(&mut self) -> anyhow::Result<i16> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
BatteryMonitor::BQ34Z100G1 { battery_driver } => {
match battery_driver.average_current() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading average_current_milli_ampere {:?}", err),
}
},
}
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
}
fn cycle_count(&mut self) -> anyhow::Result<u16> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
match battery_driver.cycle_count() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading cycle_count {:?}", err),
}
BatteryMonitor::BQ34Z100G1 { battery_driver } => match battery_driver.cycle_count() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading cycle_count {:?}", err),
},
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
}
fn state_health_percent(&mut self) -> anyhow::Result<u8> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
BatteryMonitor::BQ34Z100G1 { battery_driver } => {
match battery_driver.state_of_health() {
OkStd(r) => anyhow::Ok(r as u8),
Err(err) => bail!("Error reading state_health_percent {:?}", err),
}
},
}
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
}
fn bat_temperature(&mut self) -> anyhow::Result<u16> {
match self {
BatteryMonitor::BQ34Z100G1 { battery_driver} => {
match battery_driver.temperature() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading bat_temperature {:?}", err),
}
BatteryMonitor::BQ34Z100G1 { battery_driver } => match battery_driver.temperature() {
OkStd(r) => anyhow::Ok(r),
Err(err) => bail!("Error reading bat_temperature {:?}", err),
},
BatteryMonitor::WchI2cSlave { .. } => {
bail!("Not implemented")
},
&mut BatteryMonitor::Disabled { } => {
}
&mut BatteryMonitor::Disabled {} => {
bail!("Battery monitor is disabled")
}
}
@@ -205,12 +194,8 @@ impl BatteryInteraction for BatteryMonitor<'_> {
};
match serde_json::to_string(&bat) {
Ok(state) => {
state
}
Err(err) => {
format!("{:?}", err).to_owned()
}
Ok(state) => state,
Err(err) => format!("{:?}", err).to_owned(),
}
}
}

View File

@@ -6,10 +6,15 @@ use anyhow::{anyhow, bail, Context};
use chrono::{DateTime, Utc};
use embedded_svc::ipv4::IpInfo;
use embedded_svc::mqtt::client::QoS::{AtLeastOnce, ExactlyOnce};
use embedded_svc::wifi::{AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration};
use embedded_svc::wifi::{
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
};
use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{Level, PinDriver};
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration};
use esp_idf_svc::sntp;
use esp_idf_svc::sntp::SyncStatus;
use esp_idf_svc::systime::EspSystemTime;
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
use esp_idf_svc::wifi::EspWifi;
use esp_idf_sys::{esp_spiffs_info, vTaskDelay};
@@ -23,10 +28,6 @@ use std::str::FromStr;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Duration;
use esp_idf_svc::sntp;
use esp_idf_svc::sntp::SyncStatus;
use esp_idf_svc::systime::EspSystemTime;
#[link_section = ".rtc.data"]
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
@@ -37,7 +38,6 @@ static mut LOW_VOLTAGE_DETECTED: bool = false;
#[link_section = ".rtc.data"]
static mut RESTART_TO_CONF: bool = false;
#[derive(Serialize, Debug)]
pub struct FileInfo {
filename: String,
@@ -61,16 +61,15 @@ pub struct FileSystemSizeInfo {
pub struct MqttClient<'a> {
mqtt_client: EspMqttClient<'a>,
base_topic: heapless::String<64>
base_topic: heapless::String<64>,
}
pub struct ESP<'a> {
pub(crate) mqtt_client: Option<MqttClient<'a>>,
pub(crate) wifi_driver: EspWifi<'a>,
pub(crate) boot_button: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
pub(crate) delay: Delay
pub(crate) delay: Delay,
}
impl ESP<'_> {
const SPIFFS_PARTITION_NAME: &'static str = "storage";
const CONFIG_FILE: &'static str = "/spiffs/config.cfg";
@@ -131,9 +130,7 @@ impl ESP<'_> {
}
pub(crate) fn low_voltage_in_cycle(&mut self) -> bool {
unsafe {
LOW_VOLTAGE_DETECTED
}
unsafe { LOW_VOLTAGE_DETECTED }
}
pub(crate) fn store_consecutive_pump_count(&mut self, plant: usize, count: u32) {
unsafe {
@@ -153,13 +150,9 @@ impl ESP<'_> {
}
pub(crate) fn wifi_ap(&mut self) -> anyhow::Result<()> {
let ssid = match self.load_config(){
Ok(config) => {
config.network.ap_ssid.clone()
}
Err(_) => {
heapless::String::from_str("PlantCtrl Emergency Mode").unwrap()
}
let ssid = match self.load_config() {
Ok(config) => config.network.ap_ssid.clone(),
Err(_) => heapless::String::from_str("PlantCtrl Emergency Mode").unwrap(),
};
let apconfig = AccessPointConfiguration {
@@ -168,17 +161,17 @@ impl ESP<'_> {
ssid_hidden: false,
..Default::default()
};
self.wifi_driver.set_configuration(&Configuration::AccessPoint(apconfig))?;
self.wifi_driver
.set_configuration(&Configuration::AccessPoint(apconfig))?;
self.wifi_driver.start()?;
anyhow::Ok(())
}
pub(crate) fn wifi(
&mut self,
network_config: &NetworkConfig
) -> anyhow::Result<IpInfo> {
let ssid = network_config.ssid.clone().ok_or(anyhow!("No ssid configured"))?;
pub(crate) fn wifi(&mut self, network_config: &NetworkConfig) -> anyhow::Result<IpInfo> {
let ssid = network_config
.ssid
.clone()
.ok_or(anyhow!("No ssid configured"))?;
let password = network_config.password.clone();
let max_wait = network_config.max_wait;
@@ -298,7 +291,6 @@ impl ESP<'_> {
})
}
pub(crate) fn list_files(&self) -> FileList {
let storage = CString::new(Self::SPIFFS_PARTITION_NAME).unwrap();
@@ -367,7 +359,7 @@ impl ESP<'_> {
})
}
pub(crate) fn init_rtc_deepsleep_memory(&self, init_rtc_store: bool, to_config_mode: bool){
pub(crate) fn init_rtc_deepsleep_memory(&self, init_rtc_store: bool, to_config_mode: bool) {
if init_rtc_store {
unsafe {
LAST_WATERING_TIMESTAMP = [0; PLANT_COUNT];
@@ -544,9 +536,9 @@ impl ESP<'_> {
match round_trip_ok.load(std::sync::atomic::Ordering::Relaxed) {
true => {
println!("Round trip registered, proceeding");
self.mqtt_client = Some(MqttClient{
self.mqtt_client = Some(MqttClient {
mqtt_client: client,
base_topic: base_topic_copy
base_topic: base_topic_copy,
});
return anyhow::Ok(());
}
@@ -569,11 +561,7 @@ impl ESP<'_> {
}
bail!("Mqtt did not fire connection callback in time");
}
pub(crate) fn mqtt_publish(
&mut self,
subtopic: &str,
message: &[u8],
) -> anyhow::Result<()> {
pub(crate) fn mqtt_publish(&mut self, subtopic: &str, message: &[u8]) -> anyhow::Result<()> {
if self.mqtt_client.is_none() {
return anyhow::Ok(());
}
@@ -587,10 +575,7 @@ impl ESP<'_> {
}
let client = self.mqtt_client.as_mut().unwrap();
let mut full_topic: heapless::String<256> = heapless::String::new();
if full_topic
.push_str(client.base_topic.as_str())
.is_err()
{
if full_topic.push_str(client.base_topic.as_str()).is_err() {
println!("Some error assembling full_topic 1");
bail!("Some error assembling full_topic 1")
};
@@ -598,7 +583,9 @@ impl ESP<'_> {
println!("Some error assembling full_topic 2");
bail!("Some error assembling full_topic 2")
};
let publish = client.mqtt_client.publish(&full_topic, ExactlyOnce, true, message);
let publish = client
.mqtt_client
.publish(&full_topic, ExactlyOnce, true, message);
Delay::new(10).delay_ms(50);
match publish {
OkStd(message_id) => {
@@ -621,4 +608,4 @@ impl ESP<'_> {
}
}
}
}
}

View File

@@ -1,11 +1,11 @@
use crate::config::{BoardHardware, PlantControllerConfig};
use crate::hal::battery::{BatteryInteraction, BatteryMonitor};
use crate::hal::esp::ESP;
use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, Sensor, FreePeripherals};
use esp_idf_hal::gpio::{IOPin, Pull};
use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor};
use anyhow::{bail, Result};
use chrono::{DateTime, Utc};
use embedded_hal::digital::OutputPin;
use esp_idf_hal::gpio::{IOPin, Pull};
use esp_idf_hal::gpio::{InputOutput, PinDriver};
pub struct Initial<'a> {

View File

@@ -305,7 +305,8 @@ impl PlantHal {
}
BatteryBoardVersion::WchI2cSlave => BatteryMonitor::WchI2cSlave {},
};
let battery_interaction = Box::new(battery_monitor) as Box<dyn BatteryInteraction + Send>;
let battery_interaction =
Box::new(battery_monitor) as Box<dyn BatteryInteraction + Send>;
let board_hal: Box<dyn BoardInteraction + Send> = match config.hardware.board {
BoardVersion::INITIAL => {

View File

@@ -23,12 +23,12 @@ use esp_idf_hal::pcnt::{
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
};
use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use one_wire_bus::OneWire;
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
use std::result::Result::Ok as OkStd;
use ina219::address::Address;
use ina219::calibration::{Calibration, UnCalibrated};
use ina219::SyncIna219;
use one_wire_bus::OneWire;
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
use std::result::Result::Ok as OkStd;
const MS0: u8 = 1_u8;
const MS1: u8 = 0_u8;
@@ -174,14 +174,19 @@ pub(crate) fn create_v4(
}
let mut mppt_ina = SyncIna219::new(MutexDevice::new(&I2C_DRIVER), Address::from_byte(68)?)?;
esp.delay.delay_ms(mppt_ina.configuration()?.conversion_time().unwrap().as_millis() as u32);
esp.delay.delay_ms(
mppt_ina
.configuration()?
.conversion_time()
.unwrap()
.as_millis() as u32,
);
println!("Bus Voltage: {}", mppt_ina.bus_voltage()?);
println!("Shunt Voltage: {}", mppt_ina.shunt_voltage()?);
let volt = (mppt_ina.shunt_voltage()?.shunt_voltage_mv()) as f32 / 1000_f32;
let current = volt /0.05;
let current = volt / 0.05;
println!("Shunt Current: {}", current);
let v = V4 {
mppt_ina,
esp,