remove anyhow
This commit is contained in:
@@ -1,27 +1,20 @@
|
||||
use crate::hal::Box;
|
||||
use crate::FatError::{FatError, FatResult};
|
||||
use alloc::string::String;
|
||||
use async_trait::async_trait;
|
||||
use core::error::Error;
|
||||
use serde::Serialize;
|
||||
|
||||
#[async_trait]
|
||||
pub trait BatteryInteraction {
|
||||
async fn state_charge_percent(&mut self) -> Result<f32, BatteryError>;
|
||||
async fn remaining_milli_ampere_hour(&mut self) -> Result<u16, BatteryError>;
|
||||
async fn max_milli_ampere_hour(&mut self) -> Result<u16, BatteryError>;
|
||||
async fn design_milli_ampere_hour(&mut self) -> Result<u16, BatteryError>;
|
||||
async fn voltage_milli_volt(&mut self) -> Result<u16, BatteryError>;
|
||||
async fn average_current_milli_ampere(&mut self) -> Result<i16, BatteryError>;
|
||||
async fn cycle_count(&mut self) -> Result<u16, BatteryError>;
|
||||
async fn state_health_percent(&mut self) -> Result<u16, BatteryError>;
|
||||
async fn bat_temperature(&mut self) -> Result<u16, BatteryError>;
|
||||
async fn get_battery_state(&mut self) -> Result<BatteryState, BatteryError>;
|
||||
}
|
||||
|
||||
impl From<BatteryError> for anyhow::Error {
|
||||
fn from(err: BatteryError) -> Self {
|
||||
anyhow::anyhow!(err)
|
||||
}
|
||||
async fn state_charge_percent(&mut self) -> FatResult<f32>;
|
||||
async fn remaining_milli_ampere_hour(&mut self) -> FatResult<u16>;
|
||||
async fn max_milli_ampere_hour(&mut self) -> FatResult<u16>;
|
||||
async fn design_milli_ampere_hour(&mut self) -> FatResult<u16>;
|
||||
async fn voltage_milli_volt(&mut self) -> FatResult<u16>;
|
||||
async fn average_current_milli_ampere(&mut self) -> FatResult<i16>;
|
||||
async fn cycle_count(&mut self) -> FatResult<u16>;
|
||||
async fn state_health_percent(&mut self) -> FatResult<u16>;
|
||||
async fn bat_temperature(&mut self) -> FatResult<u16>;
|
||||
async fn get_battery_state(&mut self) -> FatResult<BatteryState>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -60,43 +53,43 @@ pub enum BatteryState {
|
||||
pub struct NoBatteryMonitor {}
|
||||
#[async_trait]
|
||||
impl BatteryInteraction for NoBatteryMonitor {
|
||||
async fn state_charge_percent(&mut self) -> Result<f32, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn state_charge_percent(&mut self) -> FatResult<f32> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn remaining_milli_ampere_hour(&mut self) -> Result<u16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn remaining_milli_ampere_hour(&mut self) -> FatResult<u16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn max_milli_ampere_hour(&mut self) -> Result<u16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn max_milli_ampere_hour(&mut self) -> FatResult<u16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn design_milli_ampere_hour(&mut self) -> Result<u16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn design_milli_ampere_hour(&mut self) -> FatResult<u16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn voltage_milli_volt(&mut self) -> Result<u16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn voltage_milli_volt(&mut self) -> FatResult<u16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn average_current_milli_ampere(&mut self) -> Result<i16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn average_current_milli_ampere(&mut self) -> FatResult<i16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn cycle_count(&mut self) -> Result<u16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn cycle_count(&mut self) -> FatResult<u16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn state_health_percent(&mut self) -> Result<u16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn state_health_percent(&mut self) -> FatResult<u16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn bat_temperature(&mut self) -> Result<u16, BatteryError> {
|
||||
Err(BatteryError::NoBatteryMonitor)
|
||||
async fn bat_temperature(&mut self) -> FatResult<u16> {
|
||||
Err(FatError::NoBatteryMonitor)
|
||||
}
|
||||
|
||||
async fn get_battery_state(&mut self) -> Result<BatteryState, BatteryError> {
|
||||
async fn get_battery_state(&mut self) -> FatResult<BatteryState> {
|
||||
Ok(BatteryState::Unknown)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,31 @@
|
||||
use crate::config::{NetworkConfig, PlantControllerConfig};
|
||||
use crate::hal::{GW_IP_ADDR_ENV, PLANT_COUNT, TIME_ACCESS};
|
||||
use crate::log::{ LogArray, LogMessage, LOG_ACCESS};
|
||||
use crate::STAY_ALIVE;
|
||||
use anyhow::{anyhow, bail, Context};
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
use crate::log::{LogMessage, LOG_ACCESS};
|
||||
use crate::{bail, STAY_ALIVE};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
|
||||
use crate::FatError::{ContextExt, FatError, FatResult};
|
||||
use alloc::string::ToString;
|
||||
use alloc::sync::Arc;
|
||||
use alloc::{format, string::String, vec::Vec};
|
||||
use core::marker::PhantomData;
|
||||
use core::net::{IpAddr, Ipv4Addr};
|
||||
use core::str::FromStr;
|
||||
use embassy_executor::{Spawner};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::{Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use embassy_time::{Duration, Instant, Timer};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_storage::nor_flash::ReadNorFlash;
|
||||
use embedded_storage::Storage;
|
||||
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState, Slot};
|
||||
use esp_bootloader_esp_idf::partitions::{Error, FlashRegion, PartitionEntry, PartitionTable};
|
||||
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState};
|
||||
use esp_bootloader_esp_idf::partitions::FlashRegion;
|
||||
use esp_hal::gpio::Input;
|
||||
use esp_hal::rng::Rng;
|
||||
use esp_hal::rtc_cntl::Rtc;
|
||||
use esp_hal::rtc_cntl::sleep::RtcSleepConfig;
|
||||
use esp_hal::system::software_reset;
|
||||
use esp_println::{println};
|
||||
use esp_println::println;
|
||||
use esp_storage::FlashStorage;
|
||||
use esp_wifi::wifi::{
|
||||
AccessPointConfiguration, AccessPointInfo, Configuration, Interfaces, ScanConfig,
|
||||
@@ -35,7 +33,7 @@ use esp_wifi::wifi::{
|
||||
};
|
||||
use littlefs2::fs::Filesystem;
|
||||
use littlefs2_core::{DynFile, FileType, PathBuf, SeekFrom};
|
||||
use log::{info};
|
||||
use log::info;
|
||||
|
||||
#[esp_hal::ram(rtc_fast, persistent)]
|
||||
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
@@ -113,12 +111,10 @@ macro_rules! mk_static {
|
||||
}
|
||||
|
||||
impl Esp<'_> {
|
||||
pub(crate) async fn delete_file(&self, filename: String) -> anyhow::Result<()> {
|
||||
pub(crate) async fn delete_file(&self, filename: String) -> FatResult<()> {
|
||||
let file = PathBuf::try_from(filename.as_str()).unwrap();
|
||||
let access = self.fs.lock().await;
|
||||
access
|
||||
.remove(&*file)
|
||||
.map_err(|err| anyhow!("Could not delete file: {:?}", err))?;
|
||||
access.remove(&*file)?;
|
||||
Ok(())
|
||||
}
|
||||
pub(crate) async fn write_file(
|
||||
@@ -126,11 +122,10 @@ impl Esp<'_> {
|
||||
filename: String,
|
||||
offset: u32,
|
||||
buf: &[u8],
|
||||
) -> anyhow::Result<()> {
|
||||
let file = PathBuf::try_from(filename.as_str()).unwrap();
|
||||
) -> Result<(), FatError> {
|
||||
let file = PathBuf::try_from(filename.as_str())?;
|
||||
let access = self.fs.lock().await;
|
||||
info!("write file {} at offset {}", filename, offset);
|
||||
match access.open_file_with_options_and_then(
|
||||
access.open_file_with_options_and_then(
|
||||
|options| options.read(true).write(true).create(true),
|
||||
&*file,
|
||||
|file| {
|
||||
@@ -138,51 +133,45 @@ impl Esp<'_> {
|
||||
file.write(buf)?;
|
||||
Ok(())
|
||||
},
|
||||
) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
bail!(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_size(&mut self, filename: String) -> FatResult<usize> {
|
||||
let file = PathBuf::try_from(filename.as_str())?;
|
||||
let access = self.fs.lock().await;
|
||||
let data = access.metadata(&*file)?;
|
||||
Ok(data.len())
|
||||
}
|
||||
pub(crate) async fn get_file(
|
||||
&mut self,
|
||||
filename: String,
|
||||
chunk: u32,
|
||||
) -> anyhow::Result<([u8; 128], usize)> {
|
||||
) -> FatResult<([u8; 512], usize)> {
|
||||
use littlefs2::io::Error as lfs2Error;
|
||||
|
||||
let file = PathBuf::try_from(filename.as_str()).unwrap();
|
||||
let file = PathBuf::try_from(filename.as_str())?;
|
||||
let access = self.fs.lock().await;
|
||||
let mut buf = [0_u8; 128];
|
||||
let mut buf = [0_u8; 512];
|
||||
let mut read = 0;
|
||||
let offset = chunk * 128;
|
||||
info!("read file {} at offset {}", filename, offset);
|
||||
match access.open_file_with_options_and_then(
|
||||
let offset = chunk * buf.len() as u32;
|
||||
access.open_file_with_options_and_then(
|
||||
|options| options.read(true),
|
||||
&*file,
|
||||
|file| {
|
||||
let length = file.len()? as u32;
|
||||
info!("file length {}", length);
|
||||
if length == 0 {
|
||||
Err(lfs2Error::IO)
|
||||
} else if length > offset {
|
||||
file.seek(SeekFrom::Start(offset))?;
|
||||
info!("seek to {}", offset);
|
||||
read = file.read(&mut buf)?;
|
||||
info!("read {} bytes", read);
|
||||
Ok(())
|
||||
} else {
|
||||
//exactly at end, do nothing
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
bail!(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
)?;
|
||||
Ok((buf, read))
|
||||
}
|
||||
|
||||
@@ -219,7 +208,7 @@ impl Esp<'_> {
|
||||
pub(crate) fn mode_override_pressed(&mut self) -> bool {
|
||||
self.boot_button.is_low()
|
||||
}
|
||||
pub(crate) async fn sntp(&mut self, _max_wait_ms: u32) -> anyhow::Result<DateTime<Utc>> {
|
||||
pub(crate) async fn sntp(&mut self, _max_wait_ms: u32) -> FatResult<DateTime<Utc>> {
|
||||
//let sntp = sntp::EspSntp::new_default()?;
|
||||
//let mut counter = 0;
|
||||
//while sntp.get_sync_status() != SyncStatus::Completed {
|
||||
@@ -233,19 +222,15 @@ impl Esp<'_> {
|
||||
todo!();
|
||||
}
|
||||
|
||||
pub async fn flash_ota(&mut self) -> anyhow::Result<()> {
|
||||
pub async fn flash_ota(&mut self) -> FatResult<()> {
|
||||
let capacity = self.ota_next.capacity();
|
||||
|
||||
bail!("not implemented")
|
||||
}
|
||||
|
||||
|
||||
pub(crate) async fn wifi_scan(&mut self) -> anyhow::Result<Vec<AccessPointInfo>> {
|
||||
pub(crate) async fn wifi_scan(&mut self) -> FatResult<Vec<AccessPointInfo>> {
|
||||
info!("start wifi scan");
|
||||
let mut lock = self
|
||||
.controller
|
||||
.try_lock()
|
||||
.map_err(|_| anyhow!("Could not lock wifi controller, currently in use"))?;
|
||||
let mut lock = self.controller.try_lock()?;
|
||||
info!("start wifi scan lock");
|
||||
let scan_config = ScanConfig {
|
||||
ssid: None,
|
||||
@@ -254,10 +239,7 @@ impl Esp<'_> {
|
||||
show_hidden: false,
|
||||
scan_type: ScanTypeConfig::Passive(core::time::Duration::from_secs(2)),
|
||||
};
|
||||
let rv = lock
|
||||
.scan_with_config_async(scan_config)
|
||||
.await
|
||||
.map_err(|err| anyhow!("Could not scan wifi: {:?}", err))?;
|
||||
let rv = lock.scan_with_config_async(scan_config).await?;
|
||||
info!("end wifi scan lock");
|
||||
Ok(rv)
|
||||
}
|
||||
@@ -294,7 +276,7 @@ impl Esp<'_> {
|
||||
unsafe { CONSECUTIVE_WATERING_PLANT[plant] }
|
||||
}
|
||||
pub(crate) fn get_restart_to_conf(&mut self) -> bool {
|
||||
unsafe { RESTART_TO_CONF == 1}
|
||||
unsafe { RESTART_TO_CONF == 1 }
|
||||
}
|
||||
pub(crate) fn set_restart_to_conf(&mut self, to_conf: bool) {
|
||||
unsafe {
|
||||
@@ -306,7 +288,7 @@ impl Esp<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn wifi_ap(&mut self) -> anyhow::Result<Stack<'static>> {
|
||||
pub(crate) async fn wifi_ap(&mut self) -> FatResult<Stack<'static>> {
|
||||
let ssid = match self.load_config().await {
|
||||
Ok(config) => config.network.ap_ssid.as_str().to_string(),
|
||||
Err(_) => "PlantCtrl Emergency Mode".to_string(),
|
||||
@@ -358,7 +340,7 @@ impl Esp<'_> {
|
||||
.config_v4()
|
||||
.inspect(|c| println!("ipv4 config: {c:?}"));
|
||||
|
||||
anyhow::Ok(stack.clone())
|
||||
Ok(stack.clone())
|
||||
}
|
||||
|
||||
pub async fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
|
||||
@@ -376,7 +358,6 @@ impl Esp<'_> {
|
||||
if duration_in_ms == 0 {
|
||||
software_reset();
|
||||
} else {
|
||||
|
||||
loop {
|
||||
info!("todo deepsleep")
|
||||
}
|
||||
@@ -390,11 +371,19 @@ impl Esp<'_> {
|
||||
//};
|
||||
}
|
||||
|
||||
pub(crate) async fn wifi(&mut self, network_config: &NetworkConfig) -> anyhow::Result<IpInfo> {
|
||||
let _ssid = network_config
|
||||
.ssid
|
||||
.clone()
|
||||
.ok_or(anyhow!("No ssid configured"))?;
|
||||
pub(crate) async fn wifi(&mut self, network_config: &NetworkConfig) -> FatResult<IpInfo> {
|
||||
let _ssid = network_config.ssid.clone();
|
||||
match &_ssid {
|
||||
Some(ssid) => {
|
||||
if ssid.is_empty() {
|
||||
bail!("Wifi ssid was empty")
|
||||
}
|
||||
}
|
||||
None => {
|
||||
bail!("Wifi ssid was empty")
|
||||
}
|
||||
}
|
||||
let _ssid = _ssid.unwrap();
|
||||
let _password = network_config.password.clone();
|
||||
let _max_wait = network_config.max_wait;
|
||||
bail!("todo")
|
||||
@@ -452,59 +441,32 @@ impl Esp<'_> {
|
||||
// log(LogMessage::WifiInfo, 0, 0, "", &format!("{address:?}"));
|
||||
// anyhow::Ok(address)
|
||||
}
|
||||
pub(crate) async fn load_config(&mut self) -> anyhow::Result<PlantControllerConfig> {
|
||||
pub(crate) async fn load_config(&mut self) -> FatResult<PlantControllerConfig> {
|
||||
let cfg = PathBuf::try_from(CONFIG_FILE).unwrap();
|
||||
match self.fs.lock().await.read::<4096>(&cfg) {
|
||||
Ok(data) => {
|
||||
let config: PlantControllerConfig = serde_json::from_slice(&data)?;
|
||||
anyhow::Ok(config)
|
||||
}
|
||||
Err(err) => {
|
||||
bail!(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
let data = self.fs.lock().await.read::<4096>(&cfg)?;
|
||||
let config: PlantControllerConfig = serde_json::from_slice(&data)?;
|
||||
return Ok(config);
|
||||
}
|
||||
pub(crate) async fn save_config(&mut self, config: Vec<u8>) -> anyhow::Result<()> {
|
||||
pub(crate) async fn save_config(&mut self, config: Vec<u8>) -> FatResult<()> {
|
||||
let filesystem = self.fs.lock().await;
|
||||
let cfg = PathBuf::try_from(CONFIG_FILE).unwrap();
|
||||
|
||||
match filesystem.write(&cfg, &*config) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
bail!(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
anyhow::Ok(())
|
||||
let cfg = PathBuf::try_from(CONFIG_FILE)?;
|
||||
filesystem.write(&cfg, &*config)?;
|
||||
Ok(())
|
||||
}
|
||||
async fn file_system_size(&mut self) -> anyhow::Result<FileSystemSizeInfo> {
|
||||
bail!("fail");
|
||||
// let storage = CString::new(Self::SPIFFS_PARTITION_NAME)?;
|
||||
// let mut total_size = 0;
|
||||
// let mut used_size = 0;
|
||||
// unsafe {
|
||||
// esp_idf_sys::esp!(esp_spiffs_info(
|
||||
// storage.as_ptr(),
|
||||
// &mut total_size,
|
||||
// &mut used_size
|
||||
// ))?;
|
||||
// }
|
||||
// anyhow::Ok(FileSystemSizeInfo {
|
||||
// total_size,
|
||||
// used_size,
|
||||
// free_size: total_size - used_size,
|
||||
// })
|
||||
}
|
||||
|
||||
pub(crate) async fn list_files(&self) -> anyhow::Result<FileList> {
|
||||
pub(crate) async fn list_files(&self) -> FatResult<FileList> {
|
||||
let path = PathBuf::new();
|
||||
|
||||
let fs = self.fs.lock().await;
|
||||
let free_size = fs.available_space()?;
|
||||
let total_size = fs.total_space();
|
||||
|
||||
let mut result = FileList {
|
||||
total: 0,
|
||||
used: 0,
|
||||
total: total_size,
|
||||
used: total_size - free_size,
|
||||
files: Vec::new(),
|
||||
};
|
||||
|
||||
match self.fs.lock().await.read_dir_and_then(&path, |dir| {
|
||||
fs.read_dir_and_then(&path, |dir| {
|
||||
for entry in dir {
|
||||
let e = entry?;
|
||||
if e.file_type() == FileType::File {
|
||||
@@ -514,29 +476,11 @@ impl Esp<'_> {
|
||||
});
|
||||
}
|
||||
}
|
||||
Result::Ok(())
|
||||
}) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
bail!(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// pub(crate) async fn get_file_handle(
|
||||
// &self,
|
||||
// filename: &str,
|
||||
// write: bool,
|
||||
// ) -> anyhow::Result<File> {
|
||||
// let filepath = Path::new(Self::BASE_PATH).join(Path::new(filename));
|
||||
// anyhow::Ok(if write {
|
||||
// File::create(filepath)?
|
||||
// } else {
|
||||
// File::open(filepath)?
|
||||
// })
|
||||
// }
|
||||
|
||||
pub(crate) async fn init_rtc_deepsleep_memory(
|
||||
&self,
|
||||
init_rtc_store: bool,
|
||||
@@ -558,22 +502,28 @@ impl Esp<'_> {
|
||||
if to_config_mode {
|
||||
RESTART_TO_CONF = 1;
|
||||
}
|
||||
LOG_ACCESS.lock().await.log(
|
||||
LogMessage::RestartToConfig,
|
||||
RESTART_TO_CONF as u32,
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
).await
|
||||
;
|
||||
LOG_ACCESS.lock().await.log(
|
||||
LogMessage::LowVoltage,
|
||||
LOW_VOLTAGE_DETECTED as u32,
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
).await
|
||||
;
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::RestartToConfig,
|
||||
RESTART_TO_CONF as u32,
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.await;
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::LowVoltage,
|
||||
LOW_VOLTAGE_DETECTED as u32,
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.await;
|
||||
for i in 0..PLANT_COUNT {
|
||||
log::info!(
|
||||
"LAST_WATERING_TIMESTAMP[{}] = UTC {}",
|
||||
@@ -592,7 +542,7 @@ impl Esp<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn mqtt(&mut self, network_config: &NetworkConfig) -> anyhow::Result<()> {
|
||||
pub(crate) async fn mqtt(&mut self, network_config: &NetworkConfig) -> FatResult<()> {
|
||||
let base_topic = network_config
|
||||
.base_topic
|
||||
.as_ref()
|
||||
@@ -756,11 +706,7 @@ impl Esp<'_> {
|
||||
// }
|
||||
// bail!("Mqtt did not fire connection callback in time");
|
||||
}
|
||||
pub(crate) async fn mqtt_publish(
|
||||
&mut self,
|
||||
_subtopic: &str,
|
||||
_message: &[u8],
|
||||
) -> anyhow::Result<()> {
|
||||
pub(crate) async fn mqtt_publish(&mut self, _subtopic: &str, _message: &[u8]) -> FatResult<()> {
|
||||
bail!("todo");
|
||||
//
|
||||
// if self.mqtt_client.is_none() {
|
||||
|
||||
@@ -3,16 +3,17 @@ use crate::hal::rtc::{BackupHeader, RTCModuleInteraction};
|
||||
use alloc::vec::Vec;
|
||||
//use crate::hal::water::TankSensor;
|
||||
use crate::alloc::boxed::Box;
|
||||
use crate::hal::water::TankSensor;
|
||||
use crate::hal::{BoardInteraction, FreePeripherals, Sensor};
|
||||
use crate::FatError::FatError;
|
||||
use crate::{
|
||||
bail,
|
||||
config::PlantControllerConfig,
|
||||
hal::battery::{BatteryInteraction, NoBatteryMonitor},
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use esp_hal::gpio::{Level, Output, OutputConfig};
|
||||
use log::info;
|
||||
use measurements::{Current, Voltage};
|
||||
|
||||
pub struct Initial<'a> {
|
||||
@@ -23,27 +24,27 @@ pub struct Initial<'a> {
|
||||
pub rtc: Box<dyn RTCModuleInteraction + Send>,
|
||||
}
|
||||
|
||||
struct NoRTC {}
|
||||
pub(crate) struct NoRTC {}
|
||||
|
||||
#[async_trait]
|
||||
impl RTCModuleInteraction for NoRTC {
|
||||
async fn get_backup_info(&mut self) -> Result<BackupHeader> {
|
||||
async fn get_backup_info(&mut self) -> Result<BackupHeader, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn get_backup_config(&mut self) -> Result<Vec<u8>> {
|
||||
async fn get_backup_config(&mut self) -> Result<Vec<u8>, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn backup_config(&mut self, _bytes: &[u8]) -> Result<()> {
|
||||
async fn backup_config(&mut self, _bytes: &[u8]) -> Result<(), FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn get_rtc_time(&mut self) -> Result<DateTime<Utc>> {
|
||||
async fn get_rtc_time(&mut self) -> Result<DateTime<Utc>, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn set_rtc_time(&mut self, _time: &DateTime<Utc>) -> Result<()> {
|
||||
async fn set_rtc_time(&mut self, _time: &DateTime<Utc>) -> Result<(), FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
}
|
||||
@@ -52,7 +53,7 @@ pub(crate) fn create_initial_board(
|
||||
free_pins: FreePeripherals<'static>,
|
||||
config: PlantControllerConfig,
|
||||
esp: Esp<'static>,
|
||||
) -> Result<Box<dyn BoardInteraction<'static> + Send>> {
|
||||
) -> Result<Box<dyn BoardInteraction<'static> + Send>, FatError> {
|
||||
log::info!("Start initial");
|
||||
let general_fault = Output::new(free_pins.gpio23, Level::Low, OutputConfig::default());
|
||||
let v = Initial {
|
||||
@@ -67,9 +68,9 @@ pub(crate) fn create_initial_board(
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> BoardInteraction<'a> for Initial<'a> {
|
||||
// fn get_tank_sensor(&mut self) -> Option<&mut TankSensor<'a>> {
|
||||
// None
|
||||
// }
|
||||
fn get_tank_sensor(&mut self) -> Result<&mut TankSensor<'a>, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
fn get_esp(&mut self) -> &mut Esp<'a> {
|
||||
&mut self.esp
|
||||
@@ -87,7 +88,7 @@ impl<'a> BoardInteraction<'a> for Initial<'a> {
|
||||
&mut self.rtc
|
||||
}
|
||||
|
||||
fn set_charge_indicator(&mut self, _charging: bool) -> Result<()> {
|
||||
fn set_charge_indicator(&mut self, _charging: bool) -> Result<(), FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
@@ -97,23 +98,27 @@ impl<'a> BoardInteraction<'a> for Initial<'a> {
|
||||
fn is_day(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn light(&mut self, _enable: bool) -> Result<()> {
|
||||
fn light(&mut self, _enable: bool) -> Result<(), FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn pump(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
||||
async fn pump(&mut self, _plant: usize, _enable: bool) -> Result<(), FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn pump_current(&mut self, _plant: usize) -> Result<Current> {
|
||||
async fn pump_current(&mut self, _plant: usize) -> Result<Current, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn fault(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
||||
async fn fault(&mut self, _plant: usize, _enable: bool) -> Result<(), FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn measure_moisture_hz(&mut self, _plant: usize, _sensor: Sensor) -> Result<f32> {
|
||||
async fn measure_moisture_hz(
|
||||
&mut self,
|
||||
_plant: usize,
|
||||
_sensor: Sensor,
|
||||
) -> Result<f32, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
@@ -121,7 +126,7 @@ impl<'a> BoardInteraction<'a> for Initial<'a> {
|
||||
self.general_fault.set_level(enable.into());
|
||||
}
|
||||
|
||||
async fn test(&mut self) -> Result<()> {
|
||||
async fn test(&mut self) -> Result<(), FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
@@ -129,11 +134,11 @@ impl<'a> BoardInteraction<'a> for Initial<'a> {
|
||||
self.config = config;
|
||||
}
|
||||
|
||||
async fn get_mptt_voltage(&mut self) -> Result<Voltage> {
|
||||
async fn get_mptt_voltage(&mut self) -> Result<Voltage, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
|
||||
async fn get_mptt_current(&mut self) -> Result<Current> {
|
||||
async fn get_mptt_current(&mut self) -> Result<Current, FatError> {
|
||||
bail!("Please configure board revision")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,9 @@ use esp_storage::FlashStorage;
|
||||
use littlefs2::consts::U512 as lfsCache;
|
||||
use littlefs2::consts::U512 as lfsLookahead;
|
||||
use littlefs2::driver::Storage as lfs2Storage;
|
||||
use littlefs2::fs::Filesystem as lfs2Filesystem;
|
||||
use littlefs2::io::Error as lfs2Error;
|
||||
use littlefs2::io::Result as lfs2Result;
|
||||
use log::{error, info};
|
||||
use log::error;
|
||||
|
||||
pub struct LittleFs2Filesystem {
|
||||
pub(crate) storage: &'static mut FlashRegion<'static, FlashStorage>,
|
||||
@@ -27,7 +26,7 @@ impl lfs2Storage for LittleFs2Filesystem {
|
||||
assert_eq!(off % read_size, 0);
|
||||
assert_eq!(buf.len() % read_size, 0);
|
||||
match self.storage.read(off as u32, buf) {
|
||||
anyhow::Result::Ok(..) => lfs2Result::Ok(buf.len()),
|
||||
Ok(..) => Ok(buf.len()),
|
||||
Err(err) => {
|
||||
error!("Littlefs2Filesystem read error: {:?}", err);
|
||||
Err(lfs2Error::IO)
|
||||
@@ -36,16 +35,11 @@ impl lfs2Storage for LittleFs2Filesystem {
|
||||
}
|
||||
|
||||
fn write(&mut self, off: usize, data: &[u8]) -> lfs2Result<usize> {
|
||||
info!(
|
||||
"Littlefs2Filesystem write at offset {} with len {}",
|
||||
off,
|
||||
data.len()
|
||||
);
|
||||
let write_size: usize = Self::WRITE_SIZE;
|
||||
assert_eq!(off % write_size, 0);
|
||||
assert_eq!(data.len() % write_size, 0);
|
||||
match self.storage.write(off as u32, data) {
|
||||
anyhow::Result::Ok(..) => lfs2Result::Ok(data.len()),
|
||||
Ok(..) => Ok(data.len()),
|
||||
Err(err) => {
|
||||
error!("Littlefs2Filesystem write error: {:?}", err);
|
||||
Err(lfs2Error::IO)
|
||||
@@ -54,10 +48,6 @@ impl lfs2Storage for LittleFs2Filesystem {
|
||||
}
|
||||
|
||||
fn erase(&mut self, off: usize, len: usize) -> lfs2Result<usize> {
|
||||
info!(
|
||||
"Littlefs2Filesystem erase at offset {} with len {}",
|
||||
off, len
|
||||
);
|
||||
let block_size: usize = Self::BLOCK_SIZE;
|
||||
debug_assert!(off % block_size == 0);
|
||||
debug_assert!(len % block_size == 0);
|
||||
|
||||
@@ -3,28 +3,55 @@ pub mod esp;
|
||||
mod initial_hal;
|
||||
mod little_fs2storage_adapter;
|
||||
mod rtc;
|
||||
mod v4_hal;
|
||||
mod water;
|
||||
//mod water;
|
||||
|
||||
use crate::alloc::string::ToString;
|
||||
use crate::hal::rtc::RTCModuleInteraction;
|
||||
use esp_hal::peripherals::Peripherals;
|
||||
use esp_hal::peripherals::GPIO0;
|
||||
use esp_hal::peripherals::GPIO1;
|
||||
use esp_hal::peripherals::GPIO10;
|
||||
use esp_hal::peripherals::GPIO11;
|
||||
use esp_hal::peripherals::GPIO12;
|
||||
use esp_hal::peripherals::GPIO13;
|
||||
use esp_hal::peripherals::GPIO14;
|
||||
use esp_hal::peripherals::GPIO15;
|
||||
use esp_hal::peripherals::GPIO16;
|
||||
use esp_hal::peripherals::GPIO17;
|
||||
use esp_hal::peripherals::GPIO18;
|
||||
use esp_hal::peripherals::GPIO2;
|
||||
use esp_hal::peripherals::GPIO21;
|
||||
use esp_hal::peripherals::GPIO22;
|
||||
use esp_hal::peripherals::GPIO23;
|
||||
use esp_hal::peripherals::GPIO24;
|
||||
use esp_hal::peripherals::GPIO25;
|
||||
use esp_hal::peripherals::GPIO26;
|
||||
use esp_hal::peripherals::GPIO27;
|
||||
use esp_hal::peripherals::GPIO28;
|
||||
use esp_hal::peripherals::GPIO29;
|
||||
use esp_hal::peripherals::GPIO3;
|
||||
use esp_hal::peripherals::GPIO30;
|
||||
use esp_hal::peripherals::GPIO4;
|
||||
use esp_hal::peripherals::GPIO5;
|
||||
use esp_hal::peripherals::GPIO6;
|
||||
use esp_hal::peripherals::GPIO7;
|
||||
use esp_hal::peripherals::GPIO8;
|
||||
|
||||
//use crate::hal::water::TankSensor;
|
||||
use crate::{
|
||||
bail,
|
||||
config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig},
|
||||
hal::{
|
||||
battery::{BatteryInteraction, NoBatteryMonitor},
|
||||
esp::Esp,
|
||||
},
|
||||
log::{LogMessage},
|
||||
log::LogMessage,
|
||||
};
|
||||
use alloc::boxed::Box;
|
||||
use alloc::format;
|
||||
use alloc::sync::Arc;
|
||||
use core::cell::OnceCell;
|
||||
use anyhow::{bail, Ok, Result};
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
use embassy_executor::Spawner;
|
||||
@@ -39,6 +66,9 @@ use esp_hal::gpio::{Input, InputConfig, Pull};
|
||||
use measurements::{Current, Voltage};
|
||||
|
||||
use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
|
||||
use crate::hal::water::TankSensor;
|
||||
use crate::log::LOG_ACCESS;
|
||||
use crate::FatError::FatError;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use embassy_sync::once_lock::OnceLock;
|
||||
use esp_alloc as _;
|
||||
@@ -53,7 +83,6 @@ use esp_wifi::{init, EspWifiController};
|
||||
use littlefs2::fs::{Allocation, Filesystem as lfs2Filesystem};
|
||||
use littlefs2::object_safe::DynStorage;
|
||||
use log::{info, warn};
|
||||
use crate::log::{LogArray, LOG_ACCESS};
|
||||
|
||||
pub static TIME_ACCESS: OnceLock<Rtc> = OnceLock::new();
|
||||
|
||||
@@ -78,26 +107,26 @@ pub struct HAL<'a> {
|
||||
|
||||
#[async_trait]
|
||||
pub trait BoardInteraction<'a> {
|
||||
//fn get_tank_sensor(&mut self) -> Option<&mut TankSensor>;
|
||||
fn get_tank_sensor(&mut self) -> Result<&mut TankSensor<'a>, FatError>;
|
||||
fn get_esp(&mut self) -> &mut Esp<'a>;
|
||||
fn get_config(&mut self) -> &PlantControllerConfig;
|
||||
fn get_battery_monitor(&mut self) -> &mut Box<dyn BatteryInteraction + Send>;
|
||||
fn get_rtc_module(&mut self) -> &mut Box<dyn RTCModuleInteraction + Send>;
|
||||
fn set_charge_indicator(&mut self, charging: bool) -> Result<()>;
|
||||
fn set_charge_indicator(&mut self, charging: bool) -> Result<(), FatError>;
|
||||
async fn deep_sleep(&mut self, duration_in_ms: u64) -> !;
|
||||
|
||||
fn is_day(&self) -> bool;
|
||||
//should be multsampled
|
||||
fn light(&mut self, enable: bool) -> Result<()>;
|
||||
async fn pump(&mut self, plant: usize, enable: bool) -> Result<()>;
|
||||
async fn pump_current(&mut self, plant: usize) -> Result<Current>;
|
||||
async fn fault(&mut self, plant: usize, enable: bool) -> Result<()>;
|
||||
async fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> Result<f32>;
|
||||
fn light(&mut self, enable: bool) -> Result<(), FatError>;
|
||||
async fn pump(&mut self, plant: usize, enable: bool) -> Result<(), FatError>;
|
||||
async fn pump_current(&mut self, plant: usize) -> Result<Current, FatError>;
|
||||
async fn fault(&mut self, plant: usize, enable: bool) -> Result<(), FatError>;
|
||||
async fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> Result<f32, FatError>;
|
||||
async fn general_fault(&mut self, enable: bool);
|
||||
async fn test(&mut self) -> Result<()>;
|
||||
async fn test(&mut self) -> Result<(), FatError>;
|
||||
fn set_config(&mut self, config: PlantControllerConfig);
|
||||
async fn get_mptt_voltage(&mut self) -> anyhow::Result<Voltage>;
|
||||
async fn get_mptt_current(&mut self) -> anyhow::Result<Current>;
|
||||
async fn get_mptt_voltage(&mut self) -> Result<Voltage, FatError>;
|
||||
async fn get_mptt_current(&mut self) -> Result<Current, FatError>;
|
||||
}
|
||||
|
||||
impl dyn BoardInteraction<'_> {
|
||||
@@ -119,36 +148,36 @@ impl dyn BoardInteraction<'_> {
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct FreePeripherals<'a> {
|
||||
// pub gpio0: Gpio0,
|
||||
// pub gpio1: Gpio1,
|
||||
// pub gpio2: Gpio2,
|
||||
// pub gpio3: Gpio3,
|
||||
// pub gpio4: Gpio4,
|
||||
// pub gpio5: Gpio5,
|
||||
pub gpio0: GPIO0<'a>,
|
||||
pub gpio1: GPIO1<'a>,
|
||||
pub gpio2: GPIO2<'a>,
|
||||
pub gpio3: GPIO3<'a>,
|
||||
pub gpio4: GPIO4<'a>,
|
||||
pub gpio5: GPIO5<'a>,
|
||||
pub gpio6: GPIO6<'a>,
|
||||
// pub gpio7: Gpio7,
|
||||
// pub gpio8: Gpio8,
|
||||
pub gpio7: GPIO7<'a>,
|
||||
pub gpio8: GPIO8<'a>,
|
||||
// //config button here
|
||||
// pub gpio10: Gpio10,
|
||||
// pub gpio11: Gpio11,
|
||||
// pub gpio12: Gpio12,
|
||||
// pub gpio13: Gpio13,
|
||||
// pub gpio14: Gpio14,
|
||||
// pub gpio15: Gpio15,
|
||||
// pub gpio16: Gpio16,
|
||||
// pub gpio17: Gpio17,
|
||||
// pub gpio18: Gpio18,
|
||||
pub gpio10: GPIO10<'a>,
|
||||
pub gpio11: GPIO11<'a>,
|
||||
pub gpio12: GPIO12<'a>,
|
||||
pub gpio13: GPIO13<'a>,
|
||||
pub gpio14: GPIO14<'a>,
|
||||
pub gpio15: GPIO15<'a>,
|
||||
pub gpio16: GPIO16<'a>,
|
||||
pub gpio17: GPIO17<'a>,
|
||||
pub gpio18: GPIO18<'a>,
|
||||
// //i2c here
|
||||
// pub gpio21: Gpio21,
|
||||
// pub gpio22: Gpio22,
|
||||
pub gpio21: GPIO21<'a>,
|
||||
pub gpio22: GPIO22<'a>,
|
||||
pub gpio23: GPIO23<'a>,
|
||||
// pub gpio24: Gpio24,
|
||||
// pub gpio25: Gpio25,
|
||||
// pub gpio26: Gpio26,
|
||||
// pub gpio27: Gpio27,
|
||||
// pub gpio28: Gpio28,
|
||||
// pub gpio29: Gpio29,
|
||||
// pub gpio30: Gpio30,
|
||||
pub gpio24: GPIO24<'a>,
|
||||
pub gpio25: GPIO25<'a>,
|
||||
pub gpio26: GPIO26<'a>,
|
||||
pub gpio27: GPIO27<'a>,
|
||||
pub gpio28: GPIO28<'a>,
|
||||
pub gpio29: GPIO29<'a>,
|
||||
pub gpio30: GPIO30<'a>,
|
||||
// pub pcnt0: PCNT0,
|
||||
// pub pcnt1: PCNT1,
|
||||
// pub adc1: ADC1,
|
||||
@@ -183,7 +212,9 @@ impl PlantHal {
|
||||
// Mutex::new(I2cDriver::new(i2c, sda, scl, &config).unwrap())
|
||||
// }
|
||||
|
||||
pub async fn create(spawner: Spawner) -> Result<Mutex<CriticalSectionRawMutex, HAL<'static>>> {
|
||||
pub async fn create(
|
||||
spawner: Spawner,
|
||||
) -> Result<Mutex<CriticalSectionRawMutex, HAL<'static>>, FatError> {
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals: Peripherals = esp_hal::init(config);
|
||||
|
||||
@@ -191,7 +222,7 @@ impl PlantHal {
|
||||
esp_alloc::heap_allocator!(#[link_section = ".dram2_uninit"] size: 64000);
|
||||
|
||||
let rtc: Rtc = Rtc::new(peripherals.LPWR);
|
||||
match(TIME_ACCESS.init(rtc)){
|
||||
match (TIME_ACCESS.init(rtc)) {
|
||||
Result::Ok(_) => {}
|
||||
Err(_) => {}
|
||||
}
|
||||
@@ -223,36 +254,35 @@ impl PlantHal {
|
||||
// adc1: peripherals.adc1,
|
||||
// pcnt0: peripherals.pcnt0,
|
||||
// pcnt1: peripherals.pcnt1,
|
||||
// gpio0: peripherals.pins.gpio0,
|
||||
// gpio1: peripherals.pins.gpio1,
|
||||
// gpio2: peripherals.pins.gpio2,
|
||||
// gpio3: peripherals.pins.gpio3,
|
||||
// gpio4: peripherals.pins.gpio4,
|
||||
// gpio5: peripherals.pins.gpio5,
|
||||
gpio0: peripherals.GPIO0,
|
||||
gpio1: peripherals.GPIO1,
|
||||
gpio2: peripherals.GPIO2,
|
||||
gpio3: peripherals.GPIO3,
|
||||
gpio4: peripherals.GPIO4,
|
||||
gpio5: peripherals.GPIO5,
|
||||
gpio6: peripherals.GPIO6,
|
||||
// gpio7: peripherals.pins.gpio7,
|
||||
// gpio8: peripherals.pins.gpio8,
|
||||
// gpio10: peripherals.pins.gpio10,
|
||||
// gpio11: peripherals.pins.gpio11,
|
||||
// gpio12: peripherals.pins.gpio12,
|
||||
// gpio13: peripherals.pins.gpio13,
|
||||
// gpio14: peripherals.pins.gpio14,
|
||||
// gpio15: peripherals.pins.gpio15,
|
||||
// gpio16: peripherals.pins.gpio16,
|
||||
// gpio17: peripherals.pins.gpio17,
|
||||
// gpio18: peripherals.pins.gpio18,
|
||||
// gpio21: peripherals.pins.gpio21,
|
||||
// gpio22: peripherals.pins.gpio22,
|
||||
gpio7: peripherals.GPIO7,
|
||||
gpio8: peripherals.GPIO8,
|
||||
gpio10: peripherals.GPIO10,
|
||||
gpio11: peripherals.GPIO11,
|
||||
gpio12: peripherals.GPIO12,
|
||||
gpio13: peripherals.GPIO13,
|
||||
gpio14: peripherals.GPIO14,
|
||||
gpio15: peripherals.GPIO15,
|
||||
gpio16: peripherals.GPIO16,
|
||||
gpio17: peripherals.GPIO17,
|
||||
gpio18: peripherals.GPIO18,
|
||||
gpio21: peripherals.GPIO21,
|
||||
gpio22: peripherals.GPIO22,
|
||||
gpio23: peripherals.GPIO23,
|
||||
// gpio24: peripherals.pins.gpio24,
|
||||
// gpio25: peripherals.pins.gpio25,
|
||||
// gpio26: peripherals.pins.gpio26,
|
||||
// gpio27: peripherals.pins.gpio27,
|
||||
// gpio28: peripherals.pins.gpio28,
|
||||
// gpio29: peripherals.pins.gpio29,
|
||||
// gpio30: peripherals.pins.gpio30,
|
||||
gpio24: peripherals.GPIO24,
|
||||
gpio25: peripherals.GPIO25,
|
||||
gpio26: peripherals.GPIO26,
|
||||
gpio27: peripherals.GPIO27,
|
||||
gpio28: peripherals.GPIO28,
|
||||
gpio29: peripherals.GPIO29,
|
||||
gpio30: peripherals.GPIO30,
|
||||
};
|
||||
//
|
||||
|
||||
let tablebuffer = mk_static!(
|
||||
[u8; esp_bootloader_esp_idf::partitions::PARTITION_TABLE_MAX_LEN],
|
||||
@@ -351,77 +381,45 @@ impl PlantHal {
|
||||
let mut init_rtc_store: bool = false;
|
||||
let mut to_config_mode: bool = false;
|
||||
let reasons = match reset_reason() {
|
||||
None => {
|
||||
"unknown"
|
||||
}
|
||||
Some(reason) => {
|
||||
match reason {
|
||||
SocResetReason::ChipPowerOn => {
|
||||
"power on"
|
||||
}
|
||||
SocResetReason::CoreSw => {
|
||||
"software reset"
|
||||
}
|
||||
SocResetReason::CoreDeepSleep => {
|
||||
"deep sleep"
|
||||
}
|
||||
SocResetReason::CoreSDIO => {
|
||||
"sdio reset"
|
||||
}
|
||||
SocResetReason::CoreMwdt0 => {
|
||||
"Watchdog Main"
|
||||
}
|
||||
SocResetReason::CoreMwdt1 => {
|
||||
"Watchdog 1"
|
||||
}
|
||||
SocResetReason::CoreRtcWdt => {
|
||||
"Watchdog RTC"
|
||||
}
|
||||
SocResetReason::Cpu0Mwdt0 => {
|
||||
"Watchdog MCpu0"
|
||||
}
|
||||
SocResetReason::Cpu0Sw => {
|
||||
"software reset cpu0"
|
||||
}
|
||||
SocResetReason::Cpu0RtcWdt => {
|
||||
init_rtc_store = true;
|
||||
"Watchdog RTC cpu0"
|
||||
}
|
||||
SocResetReason::SysBrownOut => {
|
||||
"sys brown out"
|
||||
}
|
||||
SocResetReason::SysRtcWdt => {
|
||||
"Watchdog Sys rtc"
|
||||
}
|
||||
SocResetReason::Cpu0Mwdt1 => {
|
||||
"cpu0 mwdt1"
|
||||
}
|
||||
SocResetReason::SysSuperWdt => {
|
||||
"Watchdog Super"
|
||||
}
|
||||
SocResetReason::CoreEfuseCrc => {
|
||||
"core efuse crc"
|
||||
}
|
||||
SocResetReason::CoreUsbUart => {
|
||||
to_config_mode = true;
|
||||
"core usb uart"
|
||||
}
|
||||
SocResetReason::CoreUsbJtag => {
|
||||
"core usb jtag"
|
||||
}
|
||||
SocResetReason::Cpu0JtagCpu => {
|
||||
"cpu0 jtag cpu"
|
||||
}
|
||||
None => "unknown",
|
||||
Some(reason) => match reason {
|
||||
SocResetReason::ChipPowerOn => "power on",
|
||||
SocResetReason::CoreSw => "software reset",
|
||||
SocResetReason::CoreDeepSleep => "deep sleep",
|
||||
SocResetReason::CoreSDIO => "sdio reset",
|
||||
SocResetReason::CoreMwdt0 => "Watchdog Main",
|
||||
SocResetReason::CoreMwdt1 => "Watchdog 1",
|
||||
SocResetReason::CoreRtcWdt => "Watchdog RTC",
|
||||
SocResetReason::Cpu0Mwdt0 => "Watchdog MCpu0",
|
||||
SocResetReason::Cpu0Sw => "software reset cpu0",
|
||||
SocResetReason::Cpu0RtcWdt => {
|
||||
init_rtc_store = true;
|
||||
"Watchdog RTC cpu0"
|
||||
}
|
||||
}
|
||||
SocResetReason::SysBrownOut => "sys brown out",
|
||||
SocResetReason::SysRtcWdt => "Watchdog Sys rtc",
|
||||
SocResetReason::Cpu0Mwdt1 => "cpu0 mwdt1",
|
||||
SocResetReason::SysSuperWdt => "Watchdog Super",
|
||||
SocResetReason::CoreEfuseCrc => "core efuse crc",
|
||||
SocResetReason::CoreUsbUart => {
|
||||
to_config_mode = true;
|
||||
"core usb uart"
|
||||
}
|
||||
SocResetReason::CoreUsbJtag => "core usb jtag",
|
||||
SocResetReason::Cpu0JtagCpu => "cpu0 jtag cpu",
|
||||
},
|
||||
};
|
||||
LOG_ACCESS.lock().await.log(
|
||||
LogMessage::ResetReason,
|
||||
init_rtc_store as u32,
|
||||
to_config_mode as u32,
|
||||
"",
|
||||
&format!("{reasons:?}"),
|
||||
).await;
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::ResetReason,
|
||||
init_rtc_store as u32,
|
||||
to_config_mode as u32,
|
||||
"",
|
||||
&format!("{reasons:?}"),
|
||||
)
|
||||
.await;
|
||||
|
||||
esp.init_rtc_deepsleep_memory(init_rtc_store, to_config_mode)
|
||||
.await;
|
||||
@@ -449,7 +447,7 @@ impl PlantHal {
|
||||
// }
|
||||
|
||||
// let storage = Storage::new(eeprom, Delay::new(1000));
|
||||
//let rtc_module: Box<dyn RTCModuleInteraction + Send> =
|
||||
let rtc_module: Box<dyn RTCModuleInteraction + Send> = Box::new(initial_hal::NoRTC {});
|
||||
// Box::new(DS3231Module { rtc, storage }) as Box<dyn RTCModuleInteraction + Send>;
|
||||
|
||||
let hal = match config {
|
||||
@@ -487,32 +485,35 @@ impl PlantHal {
|
||||
}
|
||||
};
|
||||
|
||||
let board_hal: Box<dyn BoardInteraction + Send> = //match config.hardware.board {
|
||||
//BoardVersion::INITIAL => {
|
||||
let board_hal: Box<dyn BoardInteraction + Send> = match config.hardware.board {
|
||||
BoardVersion::INITIAL => {
|
||||
initial_hal::create_initial_board(free_pins, config, esp)?
|
||||
;
|
||||
//}
|
||||
}
|
||||
// BoardVersion::V3 => {
|
||||
// v3_hal::create_v3(free_pins, esp, config, battery_interaction, rtc_module)?
|
||||
// }
|
||||
//BoardVersion::V4 => {
|
||||
// v4_hal::create_v4(free_pins, esp, config, battery_interaction, rtc_module)?
|
||||
//}
|
||||
//_ => {
|
||||
// todo!()
|
||||
//}
|
||||
//};
|
||||
BoardVersion::V4 => {
|
||||
v4_hal::create_v4(free_pins, esp, config, battery_interaction, rtc_module)?
|
||||
}
|
||||
_ => {
|
||||
todo!()
|
||||
}
|
||||
};
|
||||
|
||||
HAL { board_hal }
|
||||
}
|
||||
Err(err) => {
|
||||
LOG_ACCESS.lock().await.log(
|
||||
LogMessage::ConfigModeMissingConfig,
|
||||
0,
|
||||
0,
|
||||
"",
|
||||
&err.to_string(),
|
||||
).await;
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::ConfigModeMissingConfig,
|
||||
0,
|
||||
0,
|
||||
"",
|
||||
&err.to_string(),
|
||||
)
|
||||
.await;
|
||||
HAL {
|
||||
board_hal: initial_hal::create_initial_board(
|
||||
free_pins,
|
||||
@@ -527,11 +528,13 @@ impl PlantHal {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn esp_time() -> DateTime<Utc> {
|
||||
DateTime::from_timestamp_micros(TIME_ACCESS.get().await.current_time_us() as i64).unwrap()
|
||||
}
|
||||
|
||||
pub async fn esp_set_time(time: DateTime<FixedOffset>) {
|
||||
TIME_ACCESS.get().await.set_current_time_us(time.timestamp_micros() as u64);
|
||||
TIME_ACCESS
|
||||
.get()
|
||||
.await
|
||||
.set_current_time_us(time.timestamp_micros() as u64);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::hal::Box;
|
||||
use crate::FatError::FatResult;
|
||||
use alloc::vec::Vec;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
@@ -24,11 +25,11 @@ use chrono::{DateTime, Utc};
|
||||
//
|
||||
#[async_trait]
|
||||
pub trait RTCModuleInteraction {
|
||||
async fn get_backup_info(&mut self) -> anyhow::Result<BackupHeader>;
|
||||
async fn get_backup_config(&mut self) -> anyhow::Result<Vec<u8>>;
|
||||
async fn backup_config(&mut self, bytes: &[u8]) -> anyhow::Result<()>;
|
||||
async fn get_rtc_time(&mut self) -> anyhow::Result<DateTime<Utc>>;
|
||||
async fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> anyhow::Result<()>;
|
||||
async fn get_backup_info(&mut self) -> FatResult<BackupHeader>;
|
||||
async fn get_backup_config(&mut self) -> FatResult<Vec<u8>>;
|
||||
async fn backup_config(&mut self, bytes: &[u8]) -> FatResult<()>;
|
||||
async fn get_rtc_time(&mut self) -> FatResult<DateTime<Utc>>;
|
||||
async fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> FatResult<()>;
|
||||
}
|
||||
//
|
||||
// const BACKUP_HEADER_MAX_SIZE: usize = 64;
|
||||
|
||||
426
rust/src/hal/v4_hal.rs
Normal file
426
rust/src/hal/v4_hal.rs
Normal file
@@ -0,0 +1,426 @@
|
||||
use crate::config::PlantControllerConfig;
|
||||
use crate::hal::battery::BatteryInteraction;
|
||||
use crate::hal::esp::Esp;
|
||||
use crate::hal::rtc::RTCModuleInteraction;
|
||||
use crate::hal::water::TankSensor;
|
||||
use crate::hal::{BoardInteraction, FreePeripherals, Sensor};
|
||||
use alloc::boxed::Box;
|
||||
use async_trait::async_trait;
|
||||
//use embedded_hal_bus::i2c::MutexDevice;
|
||||
use crate::bail;
|
||||
use crate::FatError::FatError;
|
||||
use esp_hal::gpio::{Flex, Level, Output, OutputConfig};
|
||||
use measurements::{Current, Voltage};
|
||||
// pub enum Charger<'a> {
|
||||
// SolarMpptV1 {
|
||||
// mppt_ina: SyncIna219<MutexDevice<'a, I2cDriver<'a>>, UnCalibrated>,
|
||||
// solar_is_day: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
|
||||
// charge_indicator: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||
// },
|
||||
// ErrorInit {},
|
||||
// }
|
||||
//
|
||||
// impl Charger<'_> {
|
||||
// pub(crate) fn power_save(&mut self) {
|
||||
// match self {
|
||||
// Charger::SolarMpptV1 { mppt_ina, .. } => {
|
||||
// let _ = mppt_ina
|
||||
// .set_configuration(Configuration {
|
||||
// reset: Default::default(),
|
||||
// bus_voltage_range: Default::default(),
|
||||
// shunt_voltage_range: Default::default(),
|
||||
// bus_resolution: Default::default(),
|
||||
// shunt_resolution: Default::default(),
|
||||
// operating_mode: OperatingMode::PowerDown,
|
||||
// })
|
||||
// .map_err(|e| {
|
||||
// log::info!(
|
||||
// "Error setting ina mppt configuration during deep sleep preparation{:?}",
|
||||
// e
|
||||
// );
|
||||
// });
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// fn set_charge_indicator(&mut self, charging: bool) -> anyhow::Result<()> {
|
||||
// match self {
|
||||
// Self::SolarMpptV1 {
|
||||
// charge_indicator, ..
|
||||
// } => {
|
||||
// charge_indicator.set_state(charging.into())?;
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// Ok(())
|
||||
// }
|
||||
//
|
||||
// fn is_day(&self) -> bool {
|
||||
// match self {
|
||||
// Charger::SolarMpptV1 { solar_is_day, .. } => solar_is_day.get_level().into(),
|
||||
// _ => true,
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn get_mptt_voltage(&mut self) -> anyhow::Result<Voltage> {
|
||||
// let voltage = match self {
|
||||
// Charger::SolarMpptV1 { mppt_ina, .. } => mppt_ina
|
||||
// .bus_voltage()
|
||||
// .map(|v| Voltage::from_millivolts(v.voltage_mv() as f64))?,
|
||||
// _ => {
|
||||
// bail!("hardware error during init")
|
||||
// }
|
||||
// };
|
||||
// Ok(voltage)
|
||||
// }
|
||||
//
|
||||
// fn get_mptt_current(&mut self) -> anyhow::Result<Current> {
|
||||
// let current = match self {
|
||||
// Charger::SolarMpptV1 { mppt_ina, .. } => mppt_ina.shunt_voltage().map(|v| {
|
||||
// let shunt_voltage = Voltage::from_microvolts(v.shunt_voltage_uv().abs() as f64);
|
||||
// let shut_value = Resistance::from_ohms(0.05_f64);
|
||||
// let current = shunt_voltage.as_volts() / shut_value.as_ohms();
|
||||
// Current::from_amperes(current)
|
||||
// })?,
|
||||
// _ => {
|
||||
// bail!("hardware error during init")
|
||||
// }
|
||||
// };
|
||||
// Ok(current)
|
||||
// }
|
||||
// }
|
||||
|
||||
pub struct V4<'a> {
|
||||
esp: Esp<'a>,
|
||||
tank_sensor: TankSensor<'a>,
|
||||
//charger: Charger<'a>,
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
config: PlantControllerConfig,
|
||||
|
||||
awake: Output<'a>,
|
||||
light: Output<'a>,
|
||||
general_fault: Output<'a>,
|
||||
//pump_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
||||
//pump_ina: Option<SyncIna219<MutexDevice<'a, I2cDriver<'a>>, UnCalibrated>>,
|
||||
//sensor: SensorImpl<'a>,
|
||||
extra1: Output<'a>,
|
||||
extra2: Output<'a>,
|
||||
}
|
||||
|
||||
struct InputOutput<'a> {
|
||||
pin: Flex<'a>,
|
||||
}
|
||||
|
||||
pub(crate) fn create_v4(
|
||||
peripherals: FreePeripherals<'static>,
|
||||
esp: Esp<'static>,
|
||||
config: PlantControllerConfig,
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
) -> Result<Box<dyn BoardInteraction<'static> + Send + 'static>, FatError> {
|
||||
log::info!("Start v4");
|
||||
let mut awake = Output::new(peripherals.gpio21, Level::High, OutputConfig::default());
|
||||
awake.set_high();
|
||||
|
||||
let mut general_fault = Output::new(peripherals.gpio23, Level::Low, OutputConfig::default());
|
||||
general_fault.set_low();
|
||||
|
||||
let mut extra1 = Output::new(peripherals.gpio6, Level::Low, OutputConfig::default());
|
||||
let mut extra2 = Output::new(peripherals.gpio15, Level::Low, OutputConfig::default());
|
||||
|
||||
let one_wire_pin = Flex::new(peripherals.gpio18);
|
||||
let tank_power_pin = peripherals.gpio11;
|
||||
let flow_sensor_pin = peripherals.gpio4;
|
||||
|
||||
let tank_sensor = TankSensor::create(
|
||||
one_wire_pin,
|
||||
//peripherals.adc1,
|
||||
//peripherals.gpio5,
|
||||
//tank_power_pin,
|
||||
//flow_sensor_pin,
|
||||
//peripherals.pcnt1,
|
||||
)?;
|
||||
//
|
||||
// let mut sensor_expander = Pca9535Immediate::new(MutexDevice::new(&I2C_DRIVER), 34);
|
||||
// let sensor = match sensor_expander.pin_into_output(GPIOBank::Bank0, 0) {
|
||||
// Ok(_) => {
|
||||
// log::info!("SensorExpander answered");
|
||||
// //pulse counter version
|
||||
// let mut signal_counter = PcntDriver::new(
|
||||
// peripherals.pcnt0,
|
||||
// Some(peripherals.gpio22),
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// )?;
|
||||
//
|
||||
// signal_counter.channel_config(
|
||||
// PcntChannel::Channel0,
|
||||
// PinIndex::Pin0,
|
||||
// PinIndex::Pin1,
|
||||
// &PcntChannelConfig {
|
||||
// lctrl_mode: PcntControlMode::Keep,
|
||||
// hctrl_mode: PcntControlMode::Keep,
|
||||
// pos_mode: PcntCountMode::Increment,
|
||||
// neg_mode: PcntCountMode::Hold,
|
||||
// counter_h_lim: i16::MAX,
|
||||
// counter_l_lim: 0,
|
||||
// },
|
||||
// )?;
|
||||
//
|
||||
// for pin in 0..8 {
|
||||
// let _ = sensor_expander.pin_into_output(GPIOBank::Bank0, pin);
|
||||
// let _ = sensor_expander.pin_into_output(GPIOBank::Bank1, pin);
|
||||
// let _ = sensor_expander.pin_set_low(GPIOBank::Bank0, pin);
|
||||
// let _ = sensor_expander.pin_set_low(GPIOBank::Bank1, pin);
|
||||
// }
|
||||
//
|
||||
// SensorImpl::PulseCounter {
|
||||
// signal_counter,
|
||||
// sensor_expander,
|
||||
// }
|
||||
// }
|
||||
// Err(_) => {
|
||||
// log::info!("Can bus mode ");
|
||||
// let timing = can::config::Timing::B25K;
|
||||
// let config = can::config::Config::new().timing(timing);
|
||||
// let can = can::CanDriver::new(
|
||||
// peripherals.can,
|
||||
// peripherals.gpio0,
|
||||
// peripherals.gpio2,
|
||||
// &config,
|
||||
// )
|
||||
// .unwrap();
|
||||
//
|
||||
// let frame = StandardId::new(0x042).unwrap();
|
||||
// let tx_frame = Frame::new(frame, &[0, 1, 2, 3, 4, 5, 6, 7]).unwrap();
|
||||
// can.transmit(&tx_frame, 1000).unwrap();
|
||||
//
|
||||
// if let Ok(rx_frame) = can.receive(1000) {
|
||||
// log::info!("rx {:}:", rx_frame);
|
||||
// }
|
||||
// //can bus version
|
||||
// SensorImpl::CanBus { can }
|
||||
// }
|
||||
// };
|
||||
|
||||
let mut solar_is_day = Output::new(peripherals.gpio7, Level::Low, Default::default());
|
||||
let mut light = Output::new(peripherals.gpio10, Level::Low, Default::default());
|
||||
let mut charge_indicator = Output::new(peripherals.gpio3, Level::Low, Default::default());
|
||||
|
||||
// let mut pump_expander = Pca9535Immediate::new(MutexDevice::new(&I2C_DRIVER), 32);
|
||||
// for pin in 0..8 {
|
||||
// let _ = pump_expander.pin_into_output(GPIOBank::Bank0, pin);
|
||||
// let _ = pump_expander.pin_into_output(GPIOBank::Bank1, pin);
|
||||
// let _ = pump_expander.pin_set_low(GPIOBank::Bank0, pin);
|
||||
// let _ = pump_expander.pin_set_low(GPIOBank::Bank1, pin);
|
||||
// }
|
||||
//
|
||||
// let mppt_ina = SyncIna219::new(
|
||||
// MutexDevice::new(&I2C_DRIVER),
|
||||
// Address::from_pins(Pin::Vcc, Pin::Gnd),
|
||||
// );
|
||||
//
|
||||
// let charger = match mppt_ina {
|
||||
// Ok(mut mppt_ina) => {
|
||||
// mppt_ina.set_configuration(Configuration {
|
||||
// reset: Default::default(),
|
||||
// bus_voltage_range: Default::default(),
|
||||
// shunt_voltage_range: Default::default(),
|
||||
// bus_resolution: Default::default(),
|
||||
// shunt_resolution: ina219::configuration::Resolution::Avg128,
|
||||
// operating_mode: Default::default(),
|
||||
// })?;
|
||||
//
|
||||
// Charger::SolarMpptV1 {
|
||||
// mppt_ina,
|
||||
// solar_is_day,
|
||||
// charge_indicator,
|
||||
// }
|
||||
// }
|
||||
// Err(_) => Charger::ErrorInit {},
|
||||
// };
|
||||
//
|
||||
// let pump_ina = match SyncIna219::new(
|
||||
// MutexDevice::new(&I2C_DRIVER),
|
||||
// Address::from_pins(Pin::Gnd, Pin::Sda),
|
||||
// ) {
|
||||
// Ok(pump_ina) => Some(pump_ina),
|
||||
// Err(err) => {
|
||||
// log::info!("Error creating pump ina: {:?}", err);
|
||||
// None
|
||||
// }
|
||||
// };
|
||||
|
||||
let v = V4 {
|
||||
rtc_module,
|
||||
esp,
|
||||
awake,
|
||||
tank_sensor,
|
||||
light,
|
||||
general_fault,
|
||||
//pump_ina,
|
||||
//pump_expander,
|
||||
config,
|
||||
battery_monitor,
|
||||
//charger,
|
||||
extra1,
|
||||
extra2,
|
||||
//sensor,
|
||||
};
|
||||
Ok(Box::new(v))
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
fn get_tank_sensor(&mut self) -> Result<&mut TankSensor<'a>, FatError> {
|
||||
Ok(&mut self.tank_sensor)
|
||||
}
|
||||
|
||||
fn get_esp(&mut self) -> &mut Esp<'a> {
|
||||
&mut self.esp
|
||||
}
|
||||
|
||||
fn get_config(&mut self) -> &PlantControllerConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
fn get_battery_monitor(&mut self) -> &mut Box<dyn BatteryInteraction + Send> {
|
||||
&mut self.battery_monitor
|
||||
}
|
||||
|
||||
fn get_rtc_module(&mut self) -> &mut Box<dyn RTCModuleInteraction + Send> {
|
||||
&mut self.rtc_module
|
||||
}
|
||||
|
||||
fn set_charge_indicator(&mut self, charging: bool) -> Result<(), FatError> {
|
||||
bail!("not implemented");
|
||||
// self.charger.set_charge_indicator(charging)
|
||||
}
|
||||
|
||||
async fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
|
||||
self.awake.set_low();
|
||||
//self.charger.power_save();
|
||||
self.esp.deep_sleep(duration_in_ms).await;
|
||||
}
|
||||
|
||||
fn is_day(&self) -> bool {
|
||||
false
|
||||
//self.charger.is_day()
|
||||
}
|
||||
|
||||
fn light(&mut self, enable: bool) -> Result<(), FatError> {
|
||||
bail!("not implemented");
|
||||
// unsafe { gpio_hold_dis(self.light.pin()) };
|
||||
// self.light.set_state(enable.into())?;
|
||||
// unsafe { gpio_hold_en(self.light.pin()) };
|
||||
// anyhow::Ok(())
|
||||
}
|
||||
|
||||
async fn pump(&mut self, plant: usize, enable: bool) -> Result<(), FatError> {
|
||||
bail!("not implemented");
|
||||
// if enable {
|
||||
// self.pump_expander
|
||||
// .pin_set_high(GPIOBank::Bank0, plant.try_into()?)?;
|
||||
// } else {
|
||||
// self.pump_expander
|
||||
// .pin_set_low(GPIOBank::Bank0, plant.try_into()?)?;
|
||||
// }
|
||||
// anyhow::Ok(())
|
||||
}
|
||||
|
||||
async fn pump_current(&mut self, _plant: usize) -> Result<Current, FatError> {
|
||||
bail!("not implemented");
|
||||
// //sensore is shared for all pumps, ignore plant id
|
||||
// match self.pump_ina.as_mut() {
|
||||
// None => {
|
||||
// bail!("pump current sensor not available");
|
||||
// }
|
||||
// Some(pump_ina) => {
|
||||
// let v = pump_ina.shunt_voltage().map(|v| {
|
||||
// let shunt_voltage = Voltage::from_microvolts(v.shunt_voltage_uv().abs() as f64);
|
||||
// let shut_value = Resistance::from_ohms(0.05_f64);
|
||||
// let current = shunt_voltage.as_volts() / shut_value.as_ohms();
|
||||
// Current::from_amperes(current)
|
||||
// })?;
|
||||
// Ok(v)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
async fn fault(&mut self, plant: usize, enable: bool) -> Result<(), FatError> {
|
||||
bail!("not implemented");
|
||||
// if enable {
|
||||
// self.pump_expander
|
||||
// .pin_set_high(GPIOBank::Bank1, plant.try_into()?)?
|
||||
// } else {
|
||||
// self.pump_expander
|
||||
// .pin_set_low(GPIOBank::Bank1, plant.try_into()?)?
|
||||
// }
|
||||
// anyhow::Ok(())
|
||||
}
|
||||
|
||||
async fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> Result<f32, FatError> {
|
||||
bail!("not implemented");
|
||||
//self.sensor.measure_moisture_hz(plant, sensor)
|
||||
}
|
||||
|
||||
async fn general_fault(&mut self, enable: bool) {
|
||||
//FIXME unsafe { gpio_hold_dis(self.general_fault.pin()) };
|
||||
self.general_fault.set_level(enable.into());
|
||||
//FIXME unsafe { gpio_hold_en(self.general_fault.pin()) };
|
||||
}
|
||||
|
||||
async fn test(&mut self) -> Result<(), FatError> {
|
||||
// self.general_fault(true);
|
||||
// self.esp.delay.delay_ms(100);
|
||||
// self.general_fault(false);
|
||||
// self.esp.delay.delay_ms(500);
|
||||
// self.light(true)?;
|
||||
// self.esp.delay.delay_ms(500);
|
||||
// self.light(false)?;
|
||||
// self.esp.delay.delay_ms(500);
|
||||
// for i in 0..PLANT_COUNT {
|
||||
// self.fault(i, true)?;
|
||||
// self.esp.delay.delay_ms(500);
|
||||
// self.fault(i, false)?;
|
||||
// self.esp.delay.delay_ms(500);
|
||||
// }
|
||||
// for i in 0..PLANT_COUNT {
|
||||
// self.pump(i, true)?;
|
||||
// self.esp.delay.delay_ms(100);
|
||||
// self.pump(i, false)?;
|
||||
// self.esp.delay.delay_ms(100);
|
||||
// }
|
||||
// for plant in 0..PLANT_COUNT {
|
||||
// let a = self.measure_moisture_hz(plant, Sensor::A);
|
||||
// let b = self.measure_moisture_hz(plant, Sensor::B);
|
||||
// let aa = match a {
|
||||
// OkStd(a) => a as u32,
|
||||
// Err(_) => u32::MAX,
|
||||
// };
|
||||
// let bb = match b {
|
||||
// OkStd(b) => b as u32,
|
||||
// Err(_) => u32::MAX,
|
||||
// };
|
||||
// log(LogMessage::TestSensor, aa, bb, &plant.to_string(), "");
|
||||
// }
|
||||
// self.esp.delay.delay_ms(10);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_config(&mut self, config: PlantControllerConfig) {
|
||||
self.config = config;
|
||||
}
|
||||
|
||||
async fn get_mptt_voltage(&mut self) -> Result<Voltage, FatError> {
|
||||
bail!("not implemented");
|
||||
//self.charger.get_mptt_voltage()
|
||||
}
|
||||
|
||||
async fn get_mptt_current(&mut self) -> Result<Current, FatError> {
|
||||
bail!("not implemented");
|
||||
//self.charger.get_mptt_current()
|
||||
}
|
||||
}
|
||||
172
rust/src/hal/water.rs
Normal file
172
rust/src/hal/water.rs
Normal file
@@ -0,0 +1,172 @@
|
||||
use crate::bail;
|
||||
use crate::FatError::FatError;
|
||||
use embassy_time::Timer;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Flex, OutputConfig, Pull};
|
||||
use esp_println::println;
|
||||
use onewire::{ds18b20, Device, DeviceSearch, OneWire, DS18B20};
|
||||
|
||||
pub struct TankSensor<'a> {
|
||||
one_wire_bus: OneWire<Flex<'a>>,
|
||||
// tank_channel: AdcChannelDriver<'a, Gpio5, AdcDriver<'a, ADC1>>,
|
||||
// tank_power: PinDriver<'a, AnyIOPin, InputOutput>,
|
||||
// flow_counter: PcntDriver<'a>,
|
||||
// delay: Delay,
|
||||
}
|
||||
|
||||
impl<'a> TankSensor<'a> {
|
||||
pub(crate) fn create(
|
||||
mut one_wire_pin: Flex,
|
||||
// adc1: ADC1,
|
||||
// gpio5: Gpio5,
|
||||
// tank_power_pin: AnyIOPin,
|
||||
// flow_sensor_pin: AnyIOPin,
|
||||
// pcnt1: PCNT1,
|
||||
) -> Result<TankSensor, FatError> {
|
||||
one_wire_pin.apply_output_config(&OutputConfig::default().with_pull(Pull::None));
|
||||
|
||||
// let adc_config = AdcChannelConfig {
|
||||
// attenuation: attenuation::DB_11,
|
||||
// resolution: Resolution::Resolution12Bit,
|
||||
// calibration: esp_idf_hal::adc::oneshot::config::Calibration::Curve,
|
||||
// };
|
||||
// let tank_driver = AdcDriver::new(adc1).expect("Failed to configure ADC");
|
||||
// let tank_channel = AdcChannelDriver::new(tank_driver, gpio5, &adc_config)
|
||||
// .expect("Failed to configure ADC channel");
|
||||
//
|
||||
// let mut tank_power =
|
||||
// PinDriver::input_output(tank_power_pin).expect("Failed to configure pin");
|
||||
// tank_power
|
||||
// .set_pull(Pull::Floating)
|
||||
// .expect("Failed to set pull");
|
||||
//
|
||||
|
||||
let one_wire_bus = OneWire::new(one_wire_pin, false);
|
||||
|
||||
//
|
||||
// let mut flow_counter = PcntDriver::new(
|
||||
// pcnt1,
|
||||
// Some(flow_sensor_pin),
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// Option::<AnyInputPin>::None,
|
||||
// )?;
|
||||
//
|
||||
// flow_counter.channel_config(
|
||||
// PcntChannel::Channel1,
|
||||
// PinIndex::Pin0,
|
||||
// PinIndex::Pin1,
|
||||
// &PcntChannelConfig {
|
||||
// lctrl_mode: PcntControlMode::Keep,
|
||||
// hctrl_mode: PcntControlMode::Keep,
|
||||
// pos_mode: PcntCountMode::Increment,
|
||||
// neg_mode: PcntCountMode::Hold,
|
||||
// counter_h_lim: i16::MAX,
|
||||
// counter_l_lim: 0,
|
||||
// },
|
||||
// )?;
|
||||
//
|
||||
Ok(TankSensor {
|
||||
one_wire_bus,
|
||||
// tank_channel,
|
||||
// tank_power,
|
||||
// flow_counter,
|
||||
// delay: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reset_flow_meter(&mut self) {
|
||||
// self.flow_counter.counter_pause().unwrap();
|
||||
// self.flow_counter.counter_clear().unwrap();
|
||||
}
|
||||
|
||||
pub fn start_flow_meter(&mut self) {
|
||||
//self.flow_counter.counter_resume().unwrap();
|
||||
}
|
||||
|
||||
pub fn get_flow_meter_value(&mut self) -> i16 {
|
||||
//self.flow_counter.get_counter_value().unwrap()
|
||||
5_i16
|
||||
}
|
||||
|
||||
pub fn stop_flow_meter(&mut self) -> i16 {
|
||||
//self.flow_counter.counter_pause().unwrap();
|
||||
self.get_flow_meter_value()
|
||||
}
|
||||
|
||||
pub async fn water_temperature_c(&mut self) -> Result<f32, FatError> {
|
||||
//multisample should be moved to water_temperature_c
|
||||
let mut attempt = 1;
|
||||
let mut delay = Delay::new();
|
||||
self.one_wire_bus.reset(&mut delay)?;
|
||||
let mut search = DeviceSearch::new();
|
||||
let mut water_temp_sensor: Option<Device> = None;
|
||||
while let Some(device) = self.one_wire_bus.search_next(&mut search, &mut delay)? {
|
||||
if device.address[0] == ds18b20::FAMILY_CODE {
|
||||
water_temp_sensor = Some(device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
match water_temp_sensor {
|
||||
Some(device) => {
|
||||
println!("Found one wire device: {:?}", device);
|
||||
let mut water_temp_sensor = DS18B20::new(device)?;
|
||||
|
||||
let water_temp: Result<f32, FatError> = loop {
|
||||
let temp = self
|
||||
.single_temperature_c(&mut water_temp_sensor, &mut delay)
|
||||
.await;
|
||||
match &temp {
|
||||
Ok(res) => {
|
||||
println!("Water temp is {}", res);
|
||||
break temp;
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Could not get water temp {} attempt {}", err, attempt)
|
||||
}
|
||||
}
|
||||
if attempt == 5 {
|
||||
break temp;
|
||||
}
|
||||
attempt += 1;
|
||||
};
|
||||
water_temp
|
||||
}
|
||||
None => {
|
||||
bail!("Not found any one wire Ds18b20");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn single_temperature_c(
|
||||
&mut self,
|
||||
sensor: &mut DS18B20,
|
||||
delay: &mut Delay,
|
||||
) -> Result<f32, FatError> {
|
||||
let resolution = sensor.measure_temperature(&mut self.one_wire_bus, delay)?;
|
||||
Timer::after_millis(resolution.time_ms() as u64).await;
|
||||
let temperature = sensor.read_temperature(&mut self.one_wire_bus, delay)? as f32;
|
||||
if temperature == 85_f32 {
|
||||
bail!("Ds18b20 dummy temperature returned");
|
||||
}
|
||||
Ok(temperature / 10_f32)
|
||||
}
|
||||
|
||||
pub async fn tank_sensor_voltage(&mut self) -> Result<f32, FatError> {
|
||||
// self.tank_power.set_high()?;
|
||||
// //let stabilize
|
||||
// self.delay.delay_ms(100);
|
||||
//
|
||||
// let mut store = [0_u16; TANK_MULTI_SAMPLE];
|
||||
// for multisample in 0..TANK_MULTI_SAMPLE {
|
||||
// let value = self.tank_channel.read()?;
|
||||
// store[multisample] = value;
|
||||
// }
|
||||
// self.tank_power.set_low()?;
|
||||
//
|
||||
// store.sort();
|
||||
// let median_mv = store[6] as f32 / 1000_f32;
|
||||
let median_mv = 10_f32;
|
||||
Ok(median_mv)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user