split rtc module out to reduce repreated exactly same code
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
use crate::config::PlantControllerConfig;
|
||||
use crate::hal::battery::BatteryInteraction;
|
||||
use crate::hal::esp::Esp;
|
||||
use crate::hal::rtc::RTCModuleInteraction;
|
||||
use crate::hal::{
|
||||
deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT,
|
||||
REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE, X25,
|
||||
deep_sleep, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT,
|
||||
REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE,
|
||||
};
|
||||
use crate::log::{log, LogMessage};
|
||||
use anyhow::{anyhow, bail};
|
||||
use chrono::{DateTime, Utc};
|
||||
use ds18b20::Ds18b20;
|
||||
use ds323x::{DateTimeAccess, Ds323x};
|
||||
use eeprom24x::{Eeprom24x, Eeprom24xTrait, SlaveAddr};
|
||||
use eeprom24x::{Eeprom24x, SlaveAddr};
|
||||
use embedded_hal::digital::OutputPin;
|
||||
use embedded_hal_bus::i2c::MutexDevice;
|
||||
use esp_idf_hal::adc::oneshot::config::AdcChannelConfig;
|
||||
@@ -111,6 +111,7 @@ impl Charger<'_> {
|
||||
pub struct V4<'a> {
|
||||
esp: Esp<'a>,
|
||||
charger: Charger<'a>,
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
config: PlantControllerConfig,
|
||||
tank_channel: AdcChannelDriver<'a, Gpio5, AdcDriver<'a, esp_idf_hal::adc::ADC1>>,
|
||||
@@ -119,17 +120,11 @@ pub struct V4<'a> {
|
||||
light: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||
tank_power: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||
one_wire_bus: OneWire<PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>>,
|
||||
rtc:
|
||||
Ds323x<ds323x::interface::I2cInterface<MutexDevice<'a, I2cDriver<'a>>>, ds323x::ic::DS3231>,
|
||||
eeprom: Eeprom24x<
|
||||
MutexDevice<'a, I2cDriver<'a>>,
|
||||
eeprom24x::page_size::B32,
|
||||
eeprom24x::addr_size::TwoBytes,
|
||||
eeprom24x::unique_serial::No,
|
||||
>,
|
||||
general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||
pump_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
||||
sensor_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
||||
extra1: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, Output>,
|
||||
extra2: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, Output>,
|
||||
}
|
||||
|
||||
pub(crate) fn create_v4(
|
||||
@@ -137,6 +132,7 @@ pub(crate) fn create_v4(
|
||||
esp: Esp<'static>,
|
||||
config: PlantControllerConfig,
|
||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||
) -> anyhow::Result<Box<dyn BoardInteraction<'static> + Send + 'static>> {
|
||||
let mut awake = PinDriver::output(peripherals.gpio21.downgrade())?;
|
||||
awake.set_high()?;
|
||||
@@ -160,7 +156,7 @@ pub(crate) fn create_v4(
|
||||
extra1.set_high()?;
|
||||
|
||||
let mut extra2 = PinDriver::output(peripherals.gpio15.downgrade())?;
|
||||
extra1.set_high()?;
|
||||
extra2.set_high()?;
|
||||
|
||||
let mut one_wire_pin = PinDriver::input_output_od(peripherals.gpio18.downgrade())?;
|
||||
one_wire_pin.set_pull(Pull::Floating)?;
|
||||
@@ -231,7 +227,6 @@ pub(crate) fn create_v4(
|
||||
charge_indicator.set_low()?;
|
||||
|
||||
let mut pump_expander = Pca9535Immediate::new(MutexDevice::new(&I2C_DRIVER), 32);
|
||||
|
||||
//todo error handing if init error
|
||||
for pin in 0..8 {
|
||||
let _ = pump_expander.pin_into_output(GPIOBank::Bank0, pin);
|
||||
@@ -278,6 +273,7 @@ pub(crate) fn create_v4(
|
||||
};
|
||||
|
||||
let v = V4 {
|
||||
rtc_module,
|
||||
esp,
|
||||
awake,
|
||||
tank_channel,
|
||||
@@ -285,14 +281,14 @@ pub(crate) fn create_v4(
|
||||
light,
|
||||
tank_power,
|
||||
one_wire_bus,
|
||||
rtc,
|
||||
eeprom,
|
||||
general_fault,
|
||||
pump_expander,
|
||||
sensor_expander,
|
||||
config,
|
||||
battery_monitor,
|
||||
charger,
|
||||
extra1,
|
||||
extra2,
|
||||
};
|
||||
Ok(Box::new(v))
|
||||
}
|
||||
@@ -310,6 +306,10 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
&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) -> anyhow::Result<()> {
|
||||
self.charger.set_charge_indicator(charging)
|
||||
}
|
||||
@@ -320,102 +320,6 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
deep_sleep(duration_in_ms);
|
||||
}
|
||||
|
||||
fn get_backup_info(&mut self) -> anyhow::Result<BackupHeader> {
|
||||
let store = bincode::serialize(&BackupHeader::default())?.len();
|
||||
let mut header_page_buffer = vec![0_u8; store];
|
||||
|
||||
self.eeprom
|
||||
.read_data(0, &mut header_page_buffer)
|
||||
.map_err(|err| anyhow!("Error reading eeprom header {:?}", err))?;
|
||||
|
||||
println!("Raw header is {:?} with size {}", header_page_buffer, store);
|
||||
let header: BackupHeader = bincode::deserialize(&header_page_buffer)?;
|
||||
anyhow::Ok(header)
|
||||
}
|
||||
|
||||
fn get_backup_config(&mut self) -> anyhow::Result<Vec<u8>> {
|
||||
let store = bincode::serialize(&BackupHeader::default())?.len();
|
||||
let mut header_page_buffer = vec![0_u8; store];
|
||||
|
||||
self.eeprom
|
||||
.read_data(0, &mut header_page_buffer)
|
||||
.map_err(|err| anyhow!("Error reading eeprom header {:?}", err))?;
|
||||
|
||||
let header: BackupHeader = bincode::deserialize(&header_page_buffer)?;
|
||||
|
||||
//skip page 0, used by the header
|
||||
let data_start_address = self.eeprom.page_size() as u32;
|
||||
let mut data_buffer = vec![0_u8; header.size];
|
||||
self.eeprom
|
||||
.read_data(data_start_address, &mut data_buffer)
|
||||
.map_err(|err| anyhow!("Error reading eeprom data {:?}", err))?;
|
||||
|
||||
let checksum = X25.checksum(&data_buffer);
|
||||
if checksum != header.crc16 {
|
||||
bail!(
|
||||
"Invalid checksum, got {} but expected {}",
|
||||
checksum,
|
||||
header.crc16
|
||||
);
|
||||
}
|
||||
|
||||
anyhow::Ok(data_buffer)
|
||||
}
|
||||
|
||||
fn backup_config(&mut self, bytes: &[u8]) -> anyhow::Result<()> {
|
||||
let time = self.get_rtc_time()?.timestamp_millis();
|
||||
|
||||
let delay = Delay::new_default();
|
||||
|
||||
let checksum = X25.checksum(bytes);
|
||||
let page_size = self.eeprom.page_size();
|
||||
|
||||
let header = BackupHeader {
|
||||
crc16: checksum,
|
||||
timestamp: time,
|
||||
size: bytes.len(),
|
||||
};
|
||||
|
||||
let encoded = bincode::serialize(&header)?;
|
||||
if encoded.len() > page_size {
|
||||
bail!(
|
||||
"Size limit reached header is {}, but firest page is only {}",
|
||||
encoded.len(),
|
||||
page_size
|
||||
)
|
||||
}
|
||||
let as_u8: &[u8] = &encoded;
|
||||
|
||||
match self.eeprom.write_page(0, as_u8) {
|
||||
OkStd(_) => {}
|
||||
Err(err) => bail!("Error writing eeprom {:?}", err),
|
||||
};
|
||||
delay.delay_ms(5);
|
||||
|
||||
let to_write = bytes.chunks(page_size);
|
||||
|
||||
let mut lastiter = 0;
|
||||
let mut current_page = 1;
|
||||
for chunk in to_write {
|
||||
let address = current_page * page_size as u32;
|
||||
self.eeprom
|
||||
.write_page(address, chunk)
|
||||
.map_err(|err| anyhow!("Error writing eeprom {:?}", err))?;
|
||||
current_page += 1;
|
||||
|
||||
let iter = (current_page % 8) as usize;
|
||||
if iter != lastiter {
|
||||
for i in 0..PLANT_COUNT {
|
||||
let _ = self.fault(i, iter == i);
|
||||
}
|
||||
lastiter = iter;
|
||||
}
|
||||
|
||||
delay.delay_ms(5);
|
||||
}
|
||||
anyhow::Ok(())
|
||||
}
|
||||
|
||||
fn is_day(&self) -> bool {
|
||||
self.charger.is_day()
|
||||
}
|
||||
@@ -575,35 +479,6 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
unsafe { gpio_hold_en(self.general_fault.pin()) };
|
||||
}
|
||||
|
||||
fn factory_reset(&mut self) -> anyhow::Result<()> {
|
||||
println!("factory resetting");
|
||||
self.esp.delete_config()?;
|
||||
//destroy backup header
|
||||
let dummy: [u8; 0] = [];
|
||||
self.backup_config(&dummy)?;
|
||||
|
||||
anyhow::Ok(())
|
||||
}
|
||||
|
||||
fn get_rtc_time(&mut self) -> anyhow::Result<DateTime<Utc>> {
|
||||
match self.rtc.datetime() {
|
||||
OkStd(rtc_time) => anyhow::Ok(rtc_time.and_utc()),
|
||||
Err(err) => {
|
||||
bail!("Error getting rtc time {:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> anyhow::Result<()> {
|
||||
let naive_time = time.naive_utc();
|
||||
match self.rtc.set_datetime(&naive_time) {
|
||||
OkStd(_) => anyhow::Ok(()),
|
||||
Err(err) => {
|
||||
bail!("Error getting rtc time {:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_pump(&mut self, plant: usize) -> anyhow::Result<()> {
|
||||
self.pump(plant, true)?;
|
||||
self.esp.delay.delay_ms(30000);
|
||||
|
Reference in New Issue
Block a user