extract rtc module, extract tank module, fix backupview refresh, switch to embedded storage for eeprom
This commit is contained in:
parent
5fb8705d9a
commit
6b711e29fc
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"board": {
|
"board": {
|
||||||
"active_layer": 2,
|
"active_layer": 5,
|
||||||
"active_layer_preset": "All Layers",
|
"active_layer_preset": "All Layers",
|
||||||
"auto_track_width": false,
|
"auto_track_width": false,
|
||||||
"hidden_netclasses": [],
|
"hidden_netclasses": [],
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
<component name="InspectionProjectProfileManager">
|
<component name="InspectionProjectProfileManager">
|
||||||
<profile version="1.0">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<Languages>
|
||||||
|
<language minSize="102" name="Rust" />
|
||||||
|
</Languages>
|
||||||
|
</inspection_tool>
|
||||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
</profile>
|
</profile>
|
||||||
</component>
|
</component>
|
@ -14,11 +14,8 @@ debug = true
|
|||||||
overflow-checks = true
|
overflow-checks = true
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
incremental = true
|
incremental = true
|
||||||
opt-level = "s"
|
opt-level = 2
|
||||||
|
|
||||||
[profile.dev.build-override]
|
|
||||||
opt-level = 1
|
|
||||||
incremental = true
|
|
||||||
|
|
||||||
[package.metadata.cargo_runner]
|
[package.metadata.cargo_runner]
|
||||||
# The string `$TARGET_FILE` will be replaced with the path from cargo.
|
# The string `$TARGET_FILE` will be replaced with the path from cargo.
|
||||||
@ -86,6 +83,7 @@ esp-ota = { version = "0.2.2", features = ["log"] }
|
|||||||
unit-enum = "1.4.1"
|
unit-enum = "1.4.1"
|
||||||
pca9535 = { version = "2.0.0", features = ["std"] }
|
pca9535 = { version = "2.0.0", features = ["std"] }
|
||||||
ina219 = { version = "0.2.0", features = ["std"] }
|
ina219 = { version = "0.2.0", features = ["std"] }
|
||||||
|
embedded-storage = "=0.3.1"
|
||||||
|
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::hal::esp::Esp;
|
use crate::hal::esp::Esp;
|
||||||
use crate::hal::rtc::{BackupHeader, RTCModuleInteraction};
|
use crate::hal::rtc::{BackupHeader, RTCModuleInteraction};
|
||||||
|
use crate::hal::water::TankSensor;
|
||||||
use crate::hal::{deep_sleep, BoardInteraction, FreePeripherals, Sensor};
|
use crate::hal::{deep_sleep, BoardInteraction, FreePeripherals, Sensor};
|
||||||
use crate::{
|
use crate::{
|
||||||
config::PlantControllerConfig,
|
config::PlantControllerConfig,
|
||||||
@ -68,6 +69,10 @@ pub(crate) fn create_initial_board(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BoardInteraction<'a> for Initial<'a> {
|
impl<'a> BoardInteraction<'a> for Initial<'a> {
|
||||||
|
fn get_tank_sensor(&mut self) -> Option<&mut TankSensor<'a>> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn get_esp(&mut self) -> &mut Esp<'a> {
|
fn get_esp(&mut self) -> &mut Esp<'a> {
|
||||||
&mut self.esp
|
&mut self.esp
|
||||||
}
|
}
|
||||||
@ -91,20 +96,13 @@ impl<'a> BoardInteraction<'a> for Initial<'a> {
|
|||||||
fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
|
fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
|
||||||
deep_sleep(duration_in_ms)
|
deep_sleep(duration_in_ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_day(&self) -> bool {
|
fn is_day(&self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
fn water_temperature_c(&mut self) -> Result<f32> {
|
|
||||||
bail!("Please configure board revision")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tank_sensor_voltage(&mut self) -> Result<f32> {
|
|
||||||
bail!("Please configure board revision")
|
|
||||||
}
|
|
||||||
fn light(&mut self, _enable: bool) -> Result<()> {
|
fn light(&mut self, _enable: bool) -> Result<()> {
|
||||||
bail!("Please configure board revision")
|
bail!("Please configure board revision")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pump(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
fn pump(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
||||||
bail!("Please configure board revision")
|
bail!("Please configure board revision")
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,10 @@ mod initial_hal;
|
|||||||
mod rtc;
|
mod rtc;
|
||||||
mod v3_hal;
|
mod v3_hal;
|
||||||
mod v4_hal;
|
mod v4_hal;
|
||||||
|
mod water;
|
||||||
|
|
||||||
use crate::hal::rtc::{DS3231Module, RTCModuleInteraction};
|
use crate::hal::rtc::{DS3231Module, RTCModuleInteraction};
|
||||||
|
use crate::hal::water::TankSensor;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig},
|
config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig},
|
||||||
hal::{
|
hal::{
|
||||||
@ -18,7 +20,7 @@ use anyhow::{Ok, Result};
|
|||||||
use battery::BQ34Z100G1;
|
use battery::BQ34Z100G1;
|
||||||
use bq34z100::Bq34z100g1Driver;
|
use bq34z100::Bq34z100g1Driver;
|
||||||
use ds323x::{DateTimeAccess, Ds323x};
|
use ds323x::{DateTimeAccess, Ds323x};
|
||||||
use eeprom24x::{Eeprom24x, SlaveAddr};
|
use eeprom24x::{Eeprom24x, SlaveAddr, Storage};
|
||||||
use embedded_hal_bus::i2c::MutexDevice;
|
use embedded_hal_bus::i2c::MutexDevice;
|
||||||
use esp_idf_hal::{
|
use esp_idf_hal::{
|
||||||
adc::ADC1,
|
adc::ADC1,
|
||||||
@ -85,6 +87,7 @@ pub struct HAL<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait BoardInteraction<'a> {
|
pub trait BoardInteraction<'a> {
|
||||||
|
fn get_tank_sensor(&mut self) -> Option<&mut TankSensor<'a>>;
|
||||||
fn get_esp(&mut self) -> &mut Esp<'a>;
|
fn get_esp(&mut self) -> &mut Esp<'a>;
|
||||||
fn get_config(&mut self) -> &PlantControllerConfig;
|
fn get_config(&mut self) -> &PlantControllerConfig;
|
||||||
fn get_battery_monitor(&mut self) -> &mut Box<dyn BatteryInteraction + Send>;
|
fn get_battery_monitor(&mut self) -> &mut Box<dyn BatteryInteraction + Send>;
|
||||||
@ -94,9 +97,6 @@ pub trait BoardInteraction<'a> {
|
|||||||
|
|
||||||
fn is_day(&self) -> bool;
|
fn is_day(&self) -> bool;
|
||||||
//should be multsampled
|
//should be multsampled
|
||||||
fn water_temperature_c(&mut self) -> Result<f32>;
|
|
||||||
/// return median tank sensor value in milli volt
|
|
||||||
fn tank_sensor_voltage(&mut self) -> Result<f32>;
|
|
||||||
fn light(&mut self, enable: bool) -> Result<()>;
|
fn light(&mut self, enable: bool) -> Result<()>;
|
||||||
fn pump(&mut self, plant: usize, enable: bool) -> Result<()>;
|
fn pump(&mut self, plant: usize, enable: bool) -> Result<()>;
|
||||||
fn fault(&mut self, plant: usize, enable: bool) -> Result<()>;
|
fn fault(&mut self, plant: usize, enable: bool) -> Result<()>;
|
||||||
@ -265,7 +265,7 @@ impl PlantHal {
|
|||||||
let mut rtc = Ds323x::new_ds3231(MutexDevice::new(&I2C_DRIVER));
|
let mut rtc = Ds323x::new_ds3231(MutexDevice::new(&I2C_DRIVER));
|
||||||
|
|
||||||
println!("Init rtc eeprom driver");
|
println!("Init rtc eeprom driver");
|
||||||
let mut eeprom = {
|
let eeprom = {
|
||||||
Eeprom24x::new_24x32(
|
Eeprom24x::new_24x32(
|
||||||
MutexDevice::new(&I2C_DRIVER),
|
MutexDevice::new(&I2C_DRIVER),
|
||||||
SlaveAddr::Alternative(true, true, true),
|
SlaveAddr::Alternative(true, true, true),
|
||||||
@ -280,17 +280,10 @@ impl PlantHal {
|
|||||||
println!("Rtc Module could not be read {:?}", err);
|
println!("Rtc Module could not be read {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match eeprom.read_byte(0) {
|
|
||||||
OkStd(byte) => {
|
|
||||||
println!("Read first byte with status {}", byte);
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
println!("Eeprom could not read first byte {:?}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
let storage = Storage::new(eeprom, Delay::new(1000));
|
||||||
let rtc_module: Box<dyn RTCModuleInteraction + Send> =
|
let rtc_module: Box<dyn RTCModuleInteraction + Send> =
|
||||||
Box::new(DS3231Module { rtc, eeprom }) as Box<dyn RTCModuleInteraction + Send>;
|
Box::new(DS3231Module { rtc, storage }) as Box<dyn RTCModuleInteraction + Send>;
|
||||||
|
|
||||||
let hal = match config {
|
let hal = match config {
|
||||||
Result::Ok(config) => {
|
Result::Ok(config) => {
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
use anyhow::{anyhow, bail};
|
use anyhow::{anyhow, bail};
|
||||||
|
use bincode::config::Configuration;
|
||||||
use bincode::{config, Decode, Encode};
|
use bincode::{config, Decode, Encode};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use ds323x::{DateTimeAccess, Ds323x};
|
use ds323x::{DateTimeAccess, Ds323x};
|
||||||
use eeprom24x::{Eeprom24x, Eeprom24xTrait};
|
use eeprom24x::addr_size::TwoBytes;
|
||||||
|
use eeprom24x::page_size::B32;
|
||||||
|
use eeprom24x::unique_serial::No;
|
||||||
|
use eeprom24x::Storage;
|
||||||
use embedded_hal_bus::i2c::MutexDevice;
|
use embedded_hal_bus::i2c::MutexDevice;
|
||||||
|
use embedded_storage::ReadStorage as embedded_storage_ReadStorage;
|
||||||
|
use embedded_storage::Storage as embedded_storage_Storage;
|
||||||
use esp_idf_hal::delay::Delay;
|
use esp_idf_hal::delay::Delay;
|
||||||
use esp_idf_hal::i2c::I2cDriver;
|
use esp_idf_hal::i2c::I2cDriver;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::result::Result::Ok as OkStd;
|
use std::result::Result::Ok as OkStd;
|
||||||
|
|
||||||
const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC);
|
const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC);
|
||||||
|
const CONFIG: Configuration = config::standard();
|
||||||
|
|
||||||
pub trait RTCModuleInteraction {
|
pub trait RTCModuleInteraction {
|
||||||
fn get_backup_info(&mut self) -> anyhow::Result<BackupHeader>;
|
fn get_backup_info(&mut self) -> anyhow::Result<BackupHeader>;
|
||||||
@ -19,57 +26,49 @@ pub trait RTCModuleInteraction {
|
|||||||
fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> anyhow::Result<()>;
|
fn set_rtc_time(&mut self, time: &DateTime<Utc>) -> anyhow::Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const BACKUP_HEADER_MAX_SIZE: usize = 64;
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Encode, Decode)]
|
#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Encode, Decode)]
|
||||||
pub struct BackupHeader {
|
pub struct BackupHeader {
|
||||||
pub timestamp: i64,
|
pub timestamp: i64,
|
||||||
crc16: u16,
|
crc16: u16,
|
||||||
pub size: usize,
|
pub size: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DS3231Module<'a> {
|
pub struct DS3231Module<'a> {
|
||||||
pub(crate) rtc:
|
pub(crate) rtc:
|
||||||
Ds323x<ds323x::interface::I2cInterface<MutexDevice<'a, I2cDriver<'a>>>, ds323x::ic::DS3231>,
|
Ds323x<ds323x::interface::I2cInterface<MutexDevice<'a, I2cDriver<'a>>>, ds323x::ic::DS3231>,
|
||||||
pub(crate) eeprom: Eeprom24x<
|
|
||||||
MutexDevice<'a, I2cDriver<'a>>,
|
pub(crate) storage: Storage<MutexDevice<'a, I2cDriver<'a>>, B32, TwoBytes, No, Delay>,
|
||||||
eeprom24x::page_size::B32,
|
|
||||||
eeprom24x::addr_size::TwoBytes,
|
|
||||||
eeprom24x::unique_serial::No,
|
|
||||||
>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RTCModuleInteraction for DS3231Module<'_> {
|
impl RTCModuleInteraction for DS3231Module<'_> {
|
||||||
fn get_backup_info(&mut self) -> anyhow::Result<BackupHeader> {
|
fn get_backup_info(&mut self) -> anyhow::Result<BackupHeader> {
|
||||||
let config = config::standard();
|
let mut header_page_buffer = [0_u8; BACKUP_HEADER_MAX_SIZE];
|
||||||
let store = bincode::encode_to_vec(&BackupHeader::default(), config)?.len();
|
|
||||||
let mut header_page_buffer = vec![0_u8; store];
|
|
||||||
|
|
||||||
self.eeprom
|
self.storage
|
||||||
.read_data(0, &mut header_page_buffer)
|
.read(0, &mut header_page_buffer)
|
||||||
.map_err(|err| anyhow!("Error reading eeprom header {:?}", err))?;
|
.map_err(|err| anyhow!("Error reading eeprom header {:?}", err))?;
|
||||||
|
|
||||||
println!("Raw header is {:?} with size {}", header_page_buffer, store);
|
let (header, len): (BackupHeader, usize) =
|
||||||
let (header, _len): (BackupHeader, usize) =
|
bincode::decode_from_slice(&header_page_buffer[..], CONFIG)?;
|
||||||
bincode::decode_from_slice(&header_page_buffer[..], config)?;
|
|
||||||
|
println!("Raw header is {:?} with size {}", header_page_buffer, len);
|
||||||
anyhow::Ok(header)
|
anyhow::Ok(header)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_backup_config(&mut self) -> anyhow::Result<Vec<u8>> {
|
fn get_backup_config(&mut self) -> anyhow::Result<Vec<u8>> {
|
||||||
let config = config::standard();
|
let mut header_page_buffer = [0_u8; BACKUP_HEADER_MAX_SIZE];
|
||||||
let store = bincode::encode_to_vec(&BackupHeader::default(), config)?.len();
|
|
||||||
let mut header_page_buffer = vec![0_u8; store];
|
|
||||||
|
|
||||||
self.eeprom
|
self.storage
|
||||||
.read_data(0, &mut header_page_buffer)
|
.read(0, &mut header_page_buffer)
|
||||||
.map_err(|err| anyhow!("Error reading eeprom header {:?}", err))?;
|
.map_err(|err| anyhow!("Error reading eeprom header {:?}", err))?;
|
||||||
|
let (header, _header_size): (BackupHeader, usize) =
|
||||||
|
bincode::decode_from_slice(&header_page_buffer[..], CONFIG)?;
|
||||||
|
|
||||||
let (header, _len): (BackupHeader, usize) =
|
let mut data_buffer = vec![0_u8; header.size as usize];
|
||||||
bincode::decode_from_slice(&header_page_buffer[..], config)?;
|
//read the specified number of bytes after the header
|
||||||
|
self.storage
|
||||||
//skip page 0, used by the header
|
.read(BACKUP_HEADER_MAX_SIZE as u32, &mut data_buffer)
|
||||||
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))?;
|
.map_err(|err| anyhow!("Error reading eeprom data {:?}", err))?;
|
||||||
|
|
||||||
let checksum = X25.checksum(&data_buffer);
|
let checksum = X25.checksum(&data_buffer);
|
||||||
@ -84,55 +83,31 @@ impl RTCModuleInteraction for DS3231Module<'_> {
|
|||||||
anyhow::Ok(data_buffer)
|
anyhow::Ok(data_buffer)
|
||||||
}
|
}
|
||||||
fn backup_config(&mut self, bytes: &[u8]) -> anyhow::Result<()> {
|
fn backup_config(&mut self, bytes: &[u8]) -> anyhow::Result<()> {
|
||||||
|
let mut header_page_buffer = [0_u8; BACKUP_HEADER_MAX_SIZE];
|
||||||
|
|
||||||
let time = self.get_rtc_time()?.timestamp_millis();
|
let time = self.get_rtc_time()?.timestamp_millis();
|
||||||
|
|
||||||
let delay = Delay::new_default();
|
|
||||||
|
|
||||||
let checksum = X25.checksum(bytes);
|
let checksum = X25.checksum(bytes);
|
||||||
let page_size = self.eeprom.page_size();
|
|
||||||
|
|
||||||
let header = BackupHeader {
|
let header = BackupHeader {
|
||||||
crc16: checksum,
|
crc16: checksum,
|
||||||
timestamp: time,
|
timestamp: time,
|
||||||
size: bytes.len(),
|
size: bytes.len() as u16,
|
||||||
};
|
};
|
||||||
let config = config::standard();
|
let config = config::standard();
|
||||||
let encoded = bincode::encode_to_vec(&header, config)?;
|
let encoded = bincode::encode_into_slice(&header, &mut header_page_buffer, config)?;
|
||||||
if encoded.len() > page_size {
|
println!(
|
||||||
bail!(
|
"Raw header is {:?} with size {}",
|
||||||
"Size limit reached header is {}, but firest page is only {}",
|
header_page_buffer, encoded
|
||||||
encoded.len(),
|
);
|
||||||
page_size
|
self.storage
|
||||||
)
|
.write(0, &header_page_buffer)
|
||||||
}
|
.map_err(|err| anyhow!("Error writing header {:?}", err))?;
|
||||||
let as_u8: &[u8] = &encoded;
|
|
||||||
|
|
||||||
match self.eeprom.write_page(0, as_u8) {
|
//write rest after the header
|
||||||
OkStd(_) => {}
|
self.storage
|
||||||
Err(err) => bail!("Error writing eeprom {:?}", err),
|
.write(BACKUP_HEADER_MAX_SIZE as u32, &bytes)
|
||||||
};
|
.map_err(|err| anyhow!("Error writing body {:?}", 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 {
|
|
||||||
//todo we want to call progress here, how to do this?
|
|
||||||
//target.progress();
|
|
||||||
lastiter = iter;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay.delay_ms(5);
|
|
||||||
}
|
|
||||||
anyhow::Ok(())
|
anyhow::Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,28 +1,21 @@
|
|||||||
use crate::hal::rtc::RTCModuleInteraction;
|
use crate::hal::rtc::RTCModuleInteraction;
|
||||||
|
use crate::hal::water::TankSensor;
|
||||||
use crate::hal::{
|
use crate::hal::{
|
||||||
deep_sleep, BoardInteraction, FreePeripherals, Sensor, PLANT_COUNT, REPEAT_MOIST_MEASURE,
|
deep_sleep, BoardInteraction, FreePeripherals, Sensor, PLANT_COUNT, REPEAT_MOIST_MEASURE,
|
||||||
TANK_MULTI_SAMPLE,
|
|
||||||
};
|
};
|
||||||
use crate::log::{log, LogMessage};
|
use crate::log::{log, LogMessage};
|
||||||
use crate::{
|
use crate::{
|
||||||
config::PlantControllerConfig,
|
config::PlantControllerConfig,
|
||||||
hal::{battery::BatteryInteraction, esp::Esp},
|
hal::{battery::BatteryInteraction, esp::Esp},
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, bail, Ok, Result};
|
use anyhow::{bail, Ok, Result};
|
||||||
use ds18b20::Ds18b20;
|
|
||||||
use embedded_hal::digital::OutputPin;
|
use embedded_hal::digital::OutputPin;
|
||||||
use esp_idf_hal::{
|
use esp_idf_hal::{
|
||||||
adc::{
|
gpio::{AnyInputPin, IOPin, InputOutput, PinDriver, Pull},
|
||||||
attenuation,
|
|
||||||
oneshot::{config::AdcChannelConfig, AdcChannelDriver, AdcDriver},
|
|
||||||
Resolution,
|
|
||||||
},
|
|
||||||
gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, PinDriver, Pull},
|
|
||||||
pcnt::{PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex},
|
pcnt::{PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex},
|
||||||
};
|
};
|
||||||
use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
|
use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, vTaskDelay};
|
||||||
use measurements::{Current, Voltage};
|
use measurements::{Current, Voltage};
|
||||||
use one_wire_bus::OneWire;
|
|
||||||
use plant_ctrl2::sipo::ShiftRegister40;
|
use plant_ctrl2::sipo::ShiftRegister40;
|
||||||
use std::result::Result::Ok as OkStd;
|
use std::result::Result::Ok as OkStd;
|
||||||
|
|
||||||
@ -83,14 +76,12 @@ pub struct V3<'a> {
|
|||||||
>,
|
>,
|
||||||
_shift_register_enable_invert:
|
_shift_register_enable_invert:
|
||||||
PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Output>,
|
PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Output>,
|
||||||
tank_channel: AdcChannelDriver<'a, Gpio5, AdcDriver<'a, esp_idf_hal::adc::ADC1>>,
|
tank_sensor: TankSensor<'a>,
|
||||||
solar_is_day: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
|
solar_is_day: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
|
||||||
light: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
light: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||||
main_pump: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
main_pump: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||||
tank_power: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
|
||||||
general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||||
signal_counter: PcntDriver<'a>,
|
signal_counter: PcntDriver<'a>,
|
||||||
one_wire_bus: OneWire<PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn create_v3(
|
pub(crate) fn create_v3(
|
||||||
@ -130,8 +121,15 @@ pub(crate) fn create_v3(
|
|||||||
let ms4 = &mut shift_register.decompose()[MS_4];
|
let ms4 = &mut shift_register.decompose()[MS_4];
|
||||||
ms4.set_high()?;
|
ms4.set_high()?;
|
||||||
|
|
||||||
let mut one_wire_pin = PinDriver::input_output_od(peripherals.gpio18.downgrade())?;
|
let one_wire_pin = peripherals.gpio18.downgrade();
|
||||||
one_wire_pin.set_pull(Pull::Floating)?;
|
let tank_power_pin = peripherals.gpio11.downgrade();
|
||||||
|
|
||||||
|
let tank_sensor = TankSensor::create(
|
||||||
|
one_wire_pin,
|
||||||
|
peripherals.adc1,
|
||||||
|
peripherals.gpio5,
|
||||||
|
tank_power_pin,
|
||||||
|
);
|
||||||
|
|
||||||
let mut signal_counter = PcntDriver::new(
|
let mut signal_counter = PcntDriver::new(
|
||||||
peripherals.pcnt0,
|
peripherals.pcnt0,
|
||||||
@ -155,15 +153,6 @@ pub(crate) fn create_v3(
|
|||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
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(peripherals.adc1)?;
|
|
||||||
let tank_channel: AdcChannelDriver<Gpio5, AdcDriver<esp_idf_hal::adc::ADC1>> =
|
|
||||||
AdcChannelDriver::new(tank_driver, peripherals.gpio5, &adc_config)?;
|
|
||||||
|
|
||||||
let mut solar_is_day = PinDriver::input(peripherals.gpio7.downgrade())?;
|
let mut solar_is_day = PinDriver::input(peripherals.gpio7.downgrade())?;
|
||||||
solar_is_day.set_pull(Pull::Floating)?;
|
solar_is_day.set_pull(Pull::Floating)?;
|
||||||
|
|
||||||
@ -173,15 +162,11 @@ pub(crate) fn create_v3(
|
|||||||
let mut main_pump = PinDriver::input_output(peripherals.gpio2.downgrade())?;
|
let mut main_pump = PinDriver::input_output(peripherals.gpio2.downgrade())?;
|
||||||
main_pump.set_pull(Pull::Floating)?;
|
main_pump.set_pull(Pull::Floating)?;
|
||||||
main_pump.set_low()?;
|
main_pump.set_low()?;
|
||||||
let mut tank_power = PinDriver::input_output(peripherals.gpio11.downgrade())?;
|
|
||||||
tank_power.set_pull(Pull::Floating)?;
|
|
||||||
let mut general_fault = PinDriver::input_output(peripherals.gpio6.downgrade())?;
|
let mut general_fault = PinDriver::input_output(peripherals.gpio6.downgrade())?;
|
||||||
general_fault.set_pull(Pull::Floating)?;
|
general_fault.set_pull(Pull::Floating)?;
|
||||||
general_fault.set_low()?;
|
general_fault.set_low()?;
|
||||||
|
|
||||||
let one_wire_bus = OneWire::new(one_wire_pin)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
|
|
||||||
let mut shift_register_enable_invert = PinDriver::output(peripherals.gpio21.downgrade())?;
|
let mut shift_register_enable_invert = PinDriver::output(peripherals.gpio21.downgrade())?;
|
||||||
|
|
||||||
unsafe { gpio_hold_dis(shift_register_enable_invert.pin()) };
|
unsafe { gpio_hold_dis(shift_register_enable_invert.pin()) };
|
||||||
@ -195,18 +180,20 @@ pub(crate) fn create_v3(
|
|||||||
esp,
|
esp,
|
||||||
shift_register,
|
shift_register,
|
||||||
_shift_register_enable_invert: shift_register_enable_invert,
|
_shift_register_enable_invert: shift_register_enable_invert,
|
||||||
tank_channel,
|
tank_sensor,
|
||||||
solar_is_day,
|
solar_is_day,
|
||||||
light,
|
light,
|
||||||
main_pump,
|
main_pump,
|
||||||
tank_power,
|
|
||||||
general_fault,
|
general_fault,
|
||||||
signal_counter,
|
signal_counter,
|
||||||
one_wire_bus,
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BoardInteraction<'a> for V3<'a> {
|
impl<'a> BoardInteraction<'a> for V3<'a> {
|
||||||
|
fn get_tank_sensor(&mut self) -> Option<&mut TankSensor<'a>> {
|
||||||
|
Some(&mut self.tank_sensor)
|
||||||
|
}
|
||||||
|
|
||||||
fn get_esp(&mut self) -> &mut Esp<'a> {
|
fn get_esp(&mut self) -> &mut Esp<'a> {
|
||||||
&mut self.esp
|
&mut self.esp
|
||||||
}
|
}
|
||||||
@ -235,51 +222,6 @@ impl<'a> BoardInteraction<'a> for V3<'a> {
|
|||||||
self.solar_is_day.get_level().into()
|
self.solar_is_day.get_level().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn water_temperature_c(&mut self) -> Result<f32> {
|
|
||||||
self.one_wire_bus
|
|
||||||
.reset(&mut self.esp.delay)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
let first = self.one_wire_bus.devices(false, &mut self.esp.delay).next();
|
|
||||||
if first.is_none() {
|
|
||||||
bail!("Not found any one wire Ds18b20");
|
|
||||||
}
|
|
||||||
let device_address = first
|
|
||||||
.unwrap()
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
|
|
||||||
let water_temp_sensor = Ds18b20::new::<EspError>(device_address)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
|
|
||||||
water_temp_sensor
|
|
||||||
.start_temp_measurement(&mut self.one_wire_bus, &mut self.esp.delay)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
ds18b20::Resolution::Bits12.delay_for_measurement_time(&mut self.esp.delay);
|
|
||||||
let sensor_data = water_temp_sensor
|
|
||||||
.read_data(&mut self.one_wire_bus, &mut self.esp.delay)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
if sensor_data.temperature == 85_f32 {
|
|
||||||
bail!("Ds18b20 dummy temperature returned");
|
|
||||||
}
|
|
||||||
Ok(sensor_data.temperature / 10_f32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tank_sensor_voltage(&mut self) -> Result<f32> {
|
|
||||||
self.tank_power.set_high()?;
|
|
||||||
//let stabilize
|
|
||||||
self.esp.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;
|
|
||||||
Ok(median_mv)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn light(&mut self, enable: bool) -> Result<()> {
|
fn light(&mut self, enable: bool) -> Result<()> {
|
||||||
unsafe { gpio_hold_dis(self.light.pin()) };
|
unsafe { gpio_hold_dis(self.light.pin()) };
|
||||||
self.light.set_state(enable.into())?;
|
self.light.set_state(enable.into())?;
|
||||||
|
@ -2,33 +2,26 @@ use crate::config::PlantControllerConfig;
|
|||||||
use crate::hal::battery::BatteryInteraction;
|
use crate::hal::battery::BatteryInteraction;
|
||||||
use crate::hal::esp::Esp;
|
use crate::hal::esp::Esp;
|
||||||
use crate::hal::rtc::RTCModuleInteraction;
|
use crate::hal::rtc::RTCModuleInteraction;
|
||||||
|
use crate::hal::water::TankSensor;
|
||||||
use crate::hal::{
|
use crate::hal::{
|
||||||
deep_sleep, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT,
|
deep_sleep, BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT,
|
||||||
REPEAT_MOIST_MEASURE, TANK_MULTI_SAMPLE,
|
REPEAT_MOIST_MEASURE,
|
||||||
};
|
};
|
||||||
use crate::log::{log, LogMessage};
|
use crate::log::{log, LogMessage};
|
||||||
use anyhow::{anyhow, bail};
|
|
||||||
use ds18b20::Ds18b20;
|
|
||||||
use ds323x::{DateTimeAccess, Ds323x};
|
|
||||||
use eeprom24x::{Eeprom24x, SlaveAddr};
|
|
||||||
use embedded_hal::digital::OutputPin;
|
use embedded_hal::digital::OutputPin;
|
||||||
use embedded_hal_bus::i2c::MutexDevice;
|
use embedded_hal_bus::i2c::MutexDevice;
|
||||||
use esp_idf_hal::adc::oneshot::config::AdcChannelConfig;
|
|
||||||
use esp_idf_hal::adc::oneshot::{AdcChannelDriver, AdcDriver};
|
|
||||||
use esp_idf_hal::adc::{attenuation, Resolution};
|
|
||||||
use esp_idf_hal::delay::Delay;
|
use esp_idf_hal::delay::Delay;
|
||||||
use esp_idf_hal::gpio::{AnyInputPin, Gpio5, IOPin, InputOutput, Output, PinDriver, Pull};
|
use esp_idf_hal::gpio::{AnyInputPin, IOPin, InputOutput, Output, PinDriver, Pull};
|
||||||
use esp_idf_hal::i2c::I2cDriver;
|
use esp_idf_hal::i2c::I2cDriver;
|
||||||
use esp_idf_hal::pcnt::{
|
use esp_idf_hal::pcnt::{
|
||||||
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
|
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
|
||||||
};
|
};
|
||||||
use esp_idf_sys::{gpio_hold_dis, gpio_hold_en, EspError};
|
use esp_idf_sys::{gpio_hold_dis, gpio_hold_en};
|
||||||
use ina219::address::{Address, Pin};
|
use ina219::address::{Address, Pin};
|
||||||
use ina219::calibration::UnCalibrated;
|
use ina219::calibration::UnCalibrated;
|
||||||
use ina219::configuration::{Configuration, OperatingMode};
|
use ina219::configuration::{Configuration, OperatingMode};
|
||||||
use ina219::SyncIna219;
|
use ina219::SyncIna219;
|
||||||
use measurements::{Current, Resistance, Voltage};
|
use measurements::{Current, Resistance, Voltage};
|
||||||
use one_wire_bus::OneWire;
|
|
||||||
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
||||||
use std::result::Result::Ok as OkStd;
|
use std::result::Result::Ok as OkStd;
|
||||||
|
|
||||||
@ -110,16 +103,14 @@ impl Charger<'_> {
|
|||||||
|
|
||||||
pub struct V4<'a> {
|
pub struct V4<'a> {
|
||||||
esp: Esp<'a>,
|
esp: Esp<'a>,
|
||||||
|
tank_sensor: TankSensor<'a>,
|
||||||
charger: Charger<'a>,
|
charger: Charger<'a>,
|
||||||
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
rtc_module: Box<dyn RTCModuleInteraction + Send>,
|
||||||
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
battery_monitor: Box<dyn BatteryInteraction + Send>,
|
||||||
config: PlantControllerConfig,
|
config: PlantControllerConfig,
|
||||||
tank_channel: AdcChannelDriver<'a, Gpio5, AdcDriver<'a, esp_idf_hal::adc::ADC1>>,
|
|
||||||
signal_counter: PcntDriver<'a>,
|
signal_counter: PcntDriver<'a>,
|
||||||
awake: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, Output>,
|
awake: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, Output>,
|
||||||
light: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
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>>,
|
|
||||||
general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
||||||
pump_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
pump_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
||||||
sensor_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
sensor_expander: Pca9535Immediate<MutexDevice<'a, I2cDriver<'a>>>,
|
||||||
@ -141,46 +132,21 @@ pub(crate) fn create_v4(
|
|||||||
general_fault.set_pull(Pull::Floating)?;
|
general_fault.set_pull(Pull::Floating)?;
|
||||||
general_fault.set_low()?;
|
general_fault.set_low()?;
|
||||||
|
|
||||||
println!("Init rtc driver");
|
|
||||||
let mut rtc = Ds323x::new_ds3231(MutexDevice::new(&I2C_DRIVER));
|
|
||||||
|
|
||||||
println!("Init rtc eeprom driver");
|
|
||||||
let mut eeprom = {
|
|
||||||
Eeprom24x::new_24x32(
|
|
||||||
MutexDevice::new(&I2C_DRIVER),
|
|
||||||
SlaveAddr::Alternative(true, true, true),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut extra1 = PinDriver::output(peripherals.gpio6.downgrade())?;
|
let mut extra1 = PinDriver::output(peripherals.gpio6.downgrade())?;
|
||||||
extra1.set_high()?;
|
extra1.set_low()?;
|
||||||
|
|
||||||
let mut extra2 = PinDriver::output(peripherals.gpio15.downgrade())?;
|
let mut extra2 = PinDriver::output(peripherals.gpio15.downgrade())?;
|
||||||
extra2.set_high()?;
|
extra2.set_low()?;
|
||||||
|
|
||||||
let mut one_wire_pin = PinDriver::input_output_od(peripherals.gpio18.downgrade())?;
|
let one_wire_pin = peripherals.gpio18.downgrade();
|
||||||
one_wire_pin.set_pull(Pull::Floating)?;
|
let tank_power_pin = peripherals.gpio11.downgrade();
|
||||||
|
|
||||||
let one_wire_bus = OneWire::new(one_wire_pin)
|
let tank_sensor = TankSensor::create(
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
one_wire_pin,
|
||||||
|
peripherals.adc1,
|
||||||
let rtc_time = rtc.datetime();
|
peripherals.gpio5,
|
||||||
match rtc_time {
|
tank_power_pin,
|
||||||
OkStd(tt) => {
|
);
|
||||||
println!("Rtc Module reports time at UTC {}", tt);
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
println!("Rtc Module could not be read {:?}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match eeprom.read_byte(0) {
|
|
||||||
OkStd(byte) => {
|
|
||||||
println!("Read first byte with status {}", byte);
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
println!("Eeprom could not read first byte {:?}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut signal_counter = PcntDriver::new(
|
let mut signal_counter = PcntDriver::new(
|
||||||
peripherals.pcnt0,
|
peripherals.pcnt0,
|
||||||
@ -204,28 +170,15 @@ pub(crate) fn create_v4(
|
|||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
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(peripherals.adc1)?;
|
|
||||||
let tank_channel: AdcChannelDriver<Gpio5, AdcDriver<esp_idf_hal::adc::ADC1>> =
|
|
||||||
AdcChannelDriver::new(tank_driver, peripherals.gpio5, &adc_config)?;
|
|
||||||
|
|
||||||
let mut solar_is_day = PinDriver::input(peripherals.gpio7.downgrade())?;
|
let mut solar_is_day = PinDriver::input(peripherals.gpio7.downgrade())?;
|
||||||
solar_is_day.set_pull(Pull::Floating)?;
|
solar_is_day.set_pull(Pull::Floating)?;
|
||||||
|
|
||||||
let mut light = PinDriver::input_output(peripherals.gpio10.downgrade())?;
|
let mut light = PinDriver::input_output(peripherals.gpio10.downgrade())?;
|
||||||
light.set_pull(Pull::Floating)?;
|
light.set_pull(Pull::Floating)?;
|
||||||
|
|
||||||
let mut tank_power = PinDriver::input_output(peripherals.gpio11.downgrade())?;
|
|
||||||
tank_power.set_pull(Pull::Floating)?;
|
|
||||||
|
|
||||||
let mut charge_indicator = PinDriver::input_output(peripherals.gpio3.downgrade())?;
|
let mut charge_indicator = PinDriver::input_output(peripherals.gpio3.downgrade())?;
|
||||||
charge_indicator.set_pull(Pull::Floating)?;
|
charge_indicator.set_pull(Pull::Floating)?;
|
||||||
charge_indicator.set_low()?;
|
charge_indicator.set_low()?;
|
||||||
|
|
||||||
let mut pump_expander = Pca9535Immediate::new(MutexDevice::new(&I2C_DRIVER), 32);
|
let mut pump_expander = Pca9535Immediate::new(MutexDevice::new(&I2C_DRIVER), 32);
|
||||||
//todo error handing if init error
|
//todo error handing if init error
|
||||||
for pin in 0..8 {
|
for pin in 0..8 {
|
||||||
@ -276,11 +229,9 @@ pub(crate) fn create_v4(
|
|||||||
rtc_module,
|
rtc_module,
|
||||||
esp,
|
esp,
|
||||||
awake,
|
awake,
|
||||||
tank_channel,
|
tank_sensor,
|
||||||
signal_counter,
|
signal_counter,
|
||||||
light,
|
light,
|
||||||
tank_power,
|
|
||||||
one_wire_bus,
|
|
||||||
general_fault,
|
general_fault,
|
||||||
pump_expander,
|
pump_expander,
|
||||||
sensor_expander,
|
sensor_expander,
|
||||||
@ -294,6 +245,10 @@ pub(crate) fn create_v4(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BoardInteraction<'a> for V4<'a> {
|
impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||||
|
fn get_tank_sensor(&mut self) -> Option<&mut TankSensor<'a>> {
|
||||||
|
Some(&mut self.tank_sensor)
|
||||||
|
}
|
||||||
|
|
||||||
fn get_esp(&mut self) -> &mut Esp<'a> {
|
fn get_esp(&mut self) -> &mut Esp<'a> {
|
||||||
&mut self.esp
|
&mut self.esp
|
||||||
}
|
}
|
||||||
@ -324,51 +279,6 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
|||||||
self.charger.is_day()
|
self.charger.is_day()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn water_temperature_c(&mut self) -> anyhow::Result<f32> {
|
|
||||||
self.one_wire_bus
|
|
||||||
.reset(&mut self.esp.delay)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
let first = self.one_wire_bus.devices(false, &mut self.esp.delay).next();
|
|
||||||
if first.is_none() {
|
|
||||||
bail!("Not found any one wire Ds18b20");
|
|
||||||
}
|
|
||||||
let device_address = first
|
|
||||||
.unwrap()
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
|
|
||||||
let water_temp_sensor = Ds18b20::new::<EspError>(device_address)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
|
|
||||||
water_temp_sensor
|
|
||||||
.start_temp_measurement(&mut self.one_wire_bus, &mut self.esp.delay)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
ds18b20::Resolution::Bits12.delay_for_measurement_time(&mut self.esp.delay);
|
|
||||||
let sensor_data = water_temp_sensor
|
|
||||||
.read_data(&mut self.one_wire_bus, &mut self.esp.delay)
|
|
||||||
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
|
||||||
if sensor_data.temperature == 85_f32 {
|
|
||||||
bail!("Ds18b20 dummy temperature returned");
|
|
||||||
}
|
|
||||||
anyhow::Ok(sensor_data.temperature / 10_f32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tank_sensor_voltage(&mut self) -> anyhow::Result<f32> {
|
|
||||||
self.tank_power.set_high()?;
|
|
||||||
//let stabilize
|
|
||||||
self.esp.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;
|
|
||||||
anyhow::Ok(median_mv)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn light(&mut self, enable: bool) -> anyhow::Result<()> {
|
fn light(&mut self, enable: bool) -> anyhow::Result<()> {
|
||||||
unsafe { gpio_hold_dis(self.light.pin()) };
|
unsafe { gpio_hold_dis(self.light.pin()) };
|
||||||
self.light.set_state(enable.into())?;
|
self.light.set_state(enable.into())?;
|
||||||
|
124
rust/src/hal/water.rs
Normal file
124
rust/src/hal/water.rs
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
use crate::hal::TANK_MULTI_SAMPLE;
|
||||||
|
use anyhow::{anyhow, bail};
|
||||||
|
use ds18b20::Ds18b20;
|
||||||
|
use esp_idf_hal::adc::oneshot::config::AdcChannelConfig;
|
||||||
|
use esp_idf_hal::adc::oneshot::{AdcChannelDriver, AdcDriver};
|
||||||
|
use esp_idf_hal::adc::{attenuation, Resolution, ADC1};
|
||||||
|
use esp_idf_hal::delay::Delay;
|
||||||
|
use esp_idf_hal::gpio::{AnyIOPin, Gpio5, InputOutput, PinDriver, Pull};
|
||||||
|
use esp_idf_sys::EspError;
|
||||||
|
use one_wire_bus::OneWire;
|
||||||
|
|
||||||
|
pub struct TankSensor<'a> {
|
||||||
|
one_wire_bus: OneWire<PinDriver<'a, AnyIOPin, InputOutput>>,
|
||||||
|
tank_channel: AdcChannelDriver<'a, Gpio5, AdcDriver<'a, ADC1>>,
|
||||||
|
tank_power: PinDriver<'a, AnyIOPin, InputOutput>,
|
||||||
|
delay: Delay,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TankSensor<'a> {
|
||||||
|
pub(crate) fn create(
|
||||||
|
one_wire_pin: AnyIOPin,
|
||||||
|
adc1: ADC1,
|
||||||
|
gpio5: Gpio5,
|
||||||
|
tank_power_pin: AnyIOPin,
|
||||||
|
) -> TankSensor<'a> {
|
||||||
|
let mut one_wire_pin =
|
||||||
|
PinDriver::input_output_od(one_wire_pin).expect("Failed to configure pin");
|
||||||
|
one_wire_pin
|
||||||
|
.set_pull(Pull::Floating)
|
||||||
|
.expect("Failed to set pull");
|
||||||
|
|
||||||
|
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).expect("OneWire bus did not pull up after release");
|
||||||
|
|
||||||
|
TankSensor {
|
||||||
|
one_wire_bus,
|
||||||
|
tank_channel,
|
||||||
|
tank_power,
|
||||||
|
delay: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn water_temperature_c(&mut self) -> anyhow::Result<f32> {
|
||||||
|
//multisample should be moved to water_temperature_c
|
||||||
|
let mut attempt = 1;
|
||||||
|
let water_temp: Result<f32, anyhow::Error> = loop {
|
||||||
|
let temp = self.single_temperature_c();
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
fn single_temperature_c(&mut self) -> anyhow::Result<f32> {
|
||||||
|
self.one_wire_bus
|
||||||
|
.reset(&mut self.delay)
|
||||||
|
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
||||||
|
let first = self.one_wire_bus.devices(false, &mut self.delay).next();
|
||||||
|
if first.is_none() {
|
||||||
|
bail!("Not found any one wire Ds18b20");
|
||||||
|
}
|
||||||
|
let device_address = first
|
||||||
|
.unwrap()
|
||||||
|
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
||||||
|
|
||||||
|
let water_temp_sensor = Ds18b20::new::<EspError>(device_address)
|
||||||
|
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
||||||
|
|
||||||
|
water_temp_sensor
|
||||||
|
.start_temp_measurement(&mut self.one_wire_bus, &mut self.delay)
|
||||||
|
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
||||||
|
ds18b20::Resolution::Bits12.delay_for_measurement_time(&mut self.delay);
|
||||||
|
let sensor_data = water_temp_sensor
|
||||||
|
.read_data(&mut self.one_wire_bus, &mut self.delay)
|
||||||
|
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
|
||||||
|
if sensor_data.temperature == 85_f32 {
|
||||||
|
bail!("Ds18b20 dummy temperature returned");
|
||||||
|
}
|
||||||
|
anyhow::Ok(sensor_data.temperature / 10_f32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tank_sensor_voltage(&mut self) -> anyhow::Result<f32> {
|
||||||
|
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;
|
||||||
|
anyhow::Ok(median_mv)
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ use crate::{
|
|||||||
hal::{PlantHal, HAL, PLANT_COUNT},
|
hal::{PlantHal, HAL, PLANT_COUNT},
|
||||||
webserver::httpd,
|
webserver::httpd,
|
||||||
};
|
};
|
||||||
use anyhow::bail;
|
use anyhow::{bail, Context};
|
||||||
use chrono::{DateTime, Datelike, Timelike, Utc};
|
use chrono::{DateTime, Datelike, Timelike, Utc};
|
||||||
use chrono_tz::Tz::{self, UTC};
|
use chrono_tz::Tz::{self, UTC};
|
||||||
use esp_idf_hal::delay::Delay;
|
use esp_idf_hal::delay::Delay;
|
||||||
@ -40,6 +40,12 @@ enum WaitType {
|
|||||||
MqttConfig,
|
MqttConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
|
struct Solar {
|
||||||
|
current_ma: u32,
|
||||||
|
voltage_ma: u32,
|
||||||
|
}
|
||||||
|
|
||||||
impl WaitType {
|
impl WaitType {
|
||||||
fn blink_pattern(&self) -> u32 {
|
fn blink_pattern(&self) -> u32 {
|
||||||
match self {
|
match self {
|
||||||
@ -258,6 +264,7 @@ fn safe_main() -> anyhow::Result<()> {
|
|||||||
timezone_time,
|
timezone_time,
|
||||||
);
|
);
|
||||||
publish_battery_state(&mut board);
|
publish_battery_state(&mut board);
|
||||||
|
let _ = publish_mppt_state(&mut board);
|
||||||
}
|
}
|
||||||
|
|
||||||
log(
|
log(
|
||||||
@ -326,8 +333,12 @@ fn safe_main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut water_frozen = false;
|
let mut water_frozen = false;
|
||||||
|
let water_temp = board
|
||||||
|
.board_hal
|
||||||
|
.get_tank_sensor()
|
||||||
|
.context("no sensor")
|
||||||
|
.and_then(|f| f.water_temperature_c());
|
||||||
|
|
||||||
let water_temp = obtain_tank_temperature(&mut board);
|
|
||||||
if let Ok(res) = water_temp {
|
if let Ok(res) = water_temp {
|
||||||
if res < WATER_FROZEN_THRESH {
|
if res < WATER_FROZEN_THRESH {
|
||||||
water_frozen = true;
|
water_frozen = true;
|
||||||
@ -565,28 +576,6 @@ fn update_charge_indicator(board: &mut MutexGuard<HAL>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn obtain_tank_temperature(board: &mut MutexGuard<HAL>) -> anyhow::Result<f32> {
|
|
||||||
//multisample should be moved to water_temperature_c
|
|
||||||
let mut attempt = 1;
|
|
||||||
let water_temp: Result<f32, anyhow::Error> = loop {
|
|
||||||
let temp = board.board_hal.water_temperature_c();
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
fn publish_tank_state(
|
fn publish_tank_state(
|
||||||
board: &mut MutexGuard<HAL>,
|
board: &mut MutexGuard<HAL>,
|
||||||
tank_state: &TankState,
|
tank_state: &TankState,
|
||||||
@ -743,6 +732,24 @@ fn pump_info(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn publish_mppt_state(board: &mut MutexGuard<'_, HAL<'_>>) -> anyhow::Result<()> {
|
||||||
|
let current = board.board_hal.get_mptt_current()?;
|
||||||
|
let voltage = board.board_hal.get_mptt_voltage()?;
|
||||||
|
let solar_state = Solar {
|
||||||
|
current_ma: current.as_milliamperes() as u32,
|
||||||
|
voltage_ma: voltage.as_millivolts() as u32,
|
||||||
|
};
|
||||||
|
if let Ok(serialized_solar_state_bytes) =
|
||||||
|
serde_json::to_string(&solar_state).map(|s| s.into_bytes())
|
||||||
|
{
|
||||||
|
let _ = board
|
||||||
|
.board_hal
|
||||||
|
.get_esp()
|
||||||
|
.mqtt_publish("/mppt", &serialized_solar_state_bytes);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn publish_battery_state(board: &mut MutexGuard<'_, HAL<'_>>) {
|
fn publish_battery_state(board: &mut MutexGuard<'_, HAL<'_>>) {
|
||||||
let state = board.board_hal.get_battery_monitor().get_battery_state();
|
let state = board.board_hal.get_battery_monitor().get_battery_state();
|
||||||
if let Ok(serialized_battery_state_bytes) =
|
if let Ok(serialized_battery_state_bytes) =
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use crate::{config::TankConfig, hal::HAL};
|
use crate::{config::TankConfig, hal::HAL};
|
||||||
|
use anyhow::Context;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
const OPEN_TANK_VOLTAGE: f32 = 3.0;
|
const OPEN_TANK_VOLTAGE: f32 = 3.0;
|
||||||
@ -151,7 +152,12 @@ impl TankState {
|
|||||||
|
|
||||||
pub fn determine_tank_state(board: &mut std::sync::MutexGuard<'_, HAL<'_>>) -> TankState {
|
pub fn determine_tank_state(board: &mut std::sync::MutexGuard<'_, HAL<'_>>) -> TankState {
|
||||||
if board.board_hal.get_config().tank.tank_sensor_enabled {
|
if board.board_hal.get_config().tank.tank_sensor_enabled {
|
||||||
match board.board_hal.tank_sensor_voltage() {
|
match board
|
||||||
|
.board_hal
|
||||||
|
.get_tank_sensor()
|
||||||
|
.context("no sensor")
|
||||||
|
.and_then(|f| f.tank_sensor_voltage())
|
||||||
|
{
|
||||||
Ok(raw_sensor_value_mv) => TankState::Present(raw_sensor_value_mv),
|
Ok(raw_sensor_value_mv) => TankState::Present(raw_sensor_value_mv),
|
||||||
Err(err) => TankState::Error(TankError::BoardError(err.to_string())),
|
Err(err) => TankState::Error(TankError::BoardError(err.to_string())),
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ use crate::{
|
|||||||
plant_state::{MoistureSensorState, PlantState},
|
plant_state::{MoistureSensorState, PlantState},
|
||||||
BOARD_ACCESS,
|
BOARD_ACCESS,
|
||||||
};
|
};
|
||||||
use anyhow::bail;
|
use anyhow::{bail, Context};
|
||||||
use chrono::DateTime;
|
use chrono::DateTime;
|
||||||
use core::result::Result::Ok;
|
use core::result::Result::Ok;
|
||||||
use embedded_svc::http::Method;
|
use embedded_svc::http::Method;
|
||||||
@ -60,7 +60,7 @@ pub struct TestPump {
|
|||||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
pub struct WebBackupHeader {
|
pub struct WebBackupHeader {
|
||||||
timestamp: std::string::String,
|
timestamp: std::string::String,
|
||||||
size: usize,
|
size: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -208,10 +208,9 @@ fn backup_info(
|
|||||||
};
|
};
|
||||||
serde_json::to_string(&wbh)?
|
serde_json::to_string(&wbh)?
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(err) => {
|
||||||
//TODO make better
|
|
||||||
let wbh = WebBackupHeader {
|
let wbh = WebBackupHeader {
|
||||||
timestamp: "no backup".to_owned(),
|
timestamp: err.to_string(),
|
||||||
size: 0,
|
size: 0,
|
||||||
};
|
};
|
||||||
serde_json::to_string(&wbh)?
|
serde_json::to_string(&wbh)?
|
||||||
@ -286,7 +285,12 @@ fn tank_info(
|
|||||||
let mut board = BOARD_ACCESS.lock().unwrap();
|
let mut board = BOARD_ACCESS.lock().unwrap();
|
||||||
let tank_info = determine_tank_state(&mut board);
|
let tank_info = determine_tank_state(&mut board);
|
||||||
//should be multsampled
|
//should be multsampled
|
||||||
let water_temp = board.board_hal.water_temperature_c();
|
|
||||||
|
let water_temp = board
|
||||||
|
.board_hal
|
||||||
|
.get_tank_sensor()
|
||||||
|
.context("no sensor")
|
||||||
|
.and_then(|f| f.water_temperature_c());
|
||||||
Ok(Some(serde_json::to_string(&tank_info.as_mqtt_info(
|
Ok(Some(serde_json::to_string(&tank_info.as_mqtt_info(
|
||||||
&board.board_hal.get_config().tank,
|
&board.board_hal.get_config().tank,
|
||||||
&water_temp,
|
&water_temp,
|
||||||
|
@ -56,6 +56,7 @@ export class Controller {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loadLog() {
|
loadLog() {
|
||||||
return fetch(PUBLIC_URL + "/log")
|
return fetch(PUBLIC_URL + "/log")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
@ -67,6 +68,7 @@ export class Controller {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getBackupInfo(): Promise<void> {
|
getBackupInfo(): Promise<void> {
|
||||||
return fetch(PUBLIC_URL + "/backup_info")
|
return fetch(PUBLIC_URL + "/backup_info")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
@ -100,6 +102,7 @@ export class Controller {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadFile(file: File, name: string) {
|
uploadFile(file: File, name: string) {
|
||||||
var current = 0;
|
var current = 0;
|
||||||
var max = 100;
|
var max = 100;
|
||||||
@ -127,6 +130,7 @@ export class Controller {
|
|||||||
ajax.open("POST", PUBLIC_URL + "/file?filename=" + name);
|
ajax.open("POST", PUBLIC_URL + "/file?filename=" + name);
|
||||||
ajax.send(file);
|
ajax.send(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteFile(name: string) {
|
deleteFile(name: string) {
|
||||||
controller.progressview.addIndeterminate("file_delete", "Deleting " + name);
|
controller.progressview.addIndeterminate("file_delete", "Deleting " + name);
|
||||||
var ajax = new XMLHttpRequest();
|
var ajax = new XMLHttpRequest();
|
||||||
@ -161,7 +165,8 @@ export class Controller {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
updateBatteryData() {
|
|
||||||
|
updateBatteryData(): Promise<void> {
|
||||||
return fetch(PUBLIC_URL + "/battery")
|
return fetch(PUBLIC_URL + "/battery")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(json => json as BatteryState)
|
.then(json => json as BatteryState)
|
||||||
@ -173,7 +178,8 @@ export class Controller {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
updateSolarData() {
|
|
||||||
|
updateSolarData(): Promise<void> {
|
||||||
return fetch(PUBLIC_URL + "/solar")
|
return fetch(PUBLIC_URL + "/solar")
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(json => json as SolarState)
|
.then(json => json as SolarState)
|
||||||
@ -185,6 +191,7 @@ export class Controller {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadNewFirmware(file: File) {
|
uploadNewFirmware(file: File) {
|
||||||
var current = 0;
|
var current = 0;
|
||||||
var max = 100;
|
var max = 100;
|
||||||
@ -210,6 +217,7 @@ export class Controller {
|
|||||||
ajax.open("POST", PUBLIC_URL + "/ota");
|
ajax.open("POST", PUBLIC_URL + "/ota");
|
||||||
ajax.send(file);
|
ajax.send(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
version(): Promise<void> {
|
version(): Promise<void> {
|
||||||
controller.progressview.addIndeterminate("version", "Getting buildVersion")
|
controller.progressview.addIndeterminate("version", "Getting buildVersion")
|
||||||
return fetch(PUBLIC_URL + "/version")
|
return fetch(PUBLIC_URL + "/version")
|
||||||
@ -242,9 +250,11 @@ export class Controller {
|
|||||||
controller.configChanged();
|
controller.configChanged();
|
||||||
controller.progressview.removeProgress("get_config");
|
controller.progressview.removeProgress("get_config");
|
||||||
}
|
}
|
||||||
|
|
||||||
setInitialConfig(currentConfig: PlantControllerConfig) {
|
setInitialConfig(currentConfig: PlantControllerConfig) {
|
||||||
this.initialConfig = currentConfig
|
this.initialConfig = currentConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadConfig(json: string, statusCallback: (status: string) => void) {
|
uploadConfig(json: string, statusCallback: (status: string) => void) {
|
||||||
controller.progressview.addIndeterminate("set_config", "Uploading Config")
|
controller.progressview.addIndeterminate("set_config", "Uploading Config")
|
||||||
fetch(PUBLIC_URL + "/set_config", {
|
fetch(PUBLIC_URL + "/set_config", {
|
||||||
@ -258,22 +268,20 @@ export class Controller {
|
|||||||
controller.downloadConfig()
|
controller.downloadConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
backupConfig(json: string, statusCallback: (status: string) => void) {
|
backupConfig(json: string): Promise<string> {
|
||||||
controller.progressview.addIndeterminate("backup_config", "Backingup Config")
|
return fetch(PUBLIC_URL + "/backup_config", {
|
||||||
fetch(PUBLIC_URL + "/backup_config", {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: json,
|
body: json,
|
||||||
})
|
})
|
||||||
.then(response => response.text())
|
.then(response => response.text());
|
||||||
.then(text => statusCallback(text))
|
|
||||||
controller.progressview.removeProgress("backup_config")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
syncRTCFromBrowser() {
|
syncRTCFromBrowser() {
|
||||||
controller.progressview.addIndeterminate("write_rtc", "Writing RTC")
|
controller.progressview.addIndeterminate("write_rtc", "Writing RTC")
|
||||||
var value: SetTime = {
|
const value: SetTime = {
|
||||||
time: new Date().toISOString()
|
time: new Date().toISOString()
|
||||||
}
|
};
|
||||||
var pretty = JSON.stringify(value, undefined, 1);
|
const pretty = JSON.stringify(value, undefined, 1);
|
||||||
fetch(PUBLIC_URL + "/time", {
|
fetch(PUBLIC_URL + "/time", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: pretty
|
body: pretty
|
||||||
@ -318,12 +326,14 @@ export class Controller {
|
|||||||
controller.progressview.addProgress("test_pump", counter / limit * 100, "Testing pump " + (plantId + 1) + " for " + (limit - counter) + "s")
|
controller.progressview.addProgress("test_pump", counter / limit * 100, "Testing pump " + (plantId + 1) + " for " + (limit - counter) + "s")
|
||||||
|
|
||||||
let timerId: string | number | NodeJS.Timeout | undefined
|
let timerId: string | number | NodeJS.Timeout | undefined
|
||||||
|
|
||||||
function updateProgress() {
|
function updateProgress() {
|
||||||
counter++;
|
counter++;
|
||||||
controller.progressview.addProgress("test_pump", counter / limit * 100, "Testing pump " + (plantId + 1) + " for " + (limit - counter) + "s")
|
controller.progressview.addProgress("test_pump", counter / limit * 100, "Testing pump " + (plantId + 1) + " for " + (limit - counter) + "s")
|
||||||
timerId = setTimeout(updateProgress, 1000);
|
timerId = setTimeout(updateProgress, 1000);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timerId = setTimeout(updateProgress, 1000);
|
timerId = setTimeout(updateProgress, 1000);
|
||||||
|
|
||||||
var body: TestPump = {
|
var body: TestPump = {
|
||||||
@ -361,12 +371,14 @@ export class Controller {
|
|||||||
controller.progressview.addProgress("scan_ssid", counter / limit * 100, "Scanning for SSIDs for " + (limit - counter) + "s")
|
controller.progressview.addProgress("scan_ssid", counter / limit * 100, "Scanning for SSIDs for " + (limit - counter) + "s")
|
||||||
|
|
||||||
let timerId: string | number | NodeJS.Timeout | undefined
|
let timerId: string | number | NodeJS.Timeout | undefined
|
||||||
|
|
||||||
function updateProgress() {
|
function updateProgress() {
|
||||||
counter++;
|
counter++;
|
||||||
controller.progressview.addProgress("scan_ssid", counter / limit * 100, "Scanning for SSIDs for " + (limit - counter) + "s")
|
controller.progressview.addProgress("scan_ssid", counter / limit * 100, "Scanning for SSIDs for " + (limit - counter) + "s")
|
||||||
timerId = setTimeout(updateProgress, 1000);
|
timerId = setTimeout(updateProgress, 1000);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timerId = setTimeout(updateProgress, 1000);
|
timerId = setTimeout(updateProgress, 1000);
|
||||||
|
|
||||||
|
|
||||||
@ -403,12 +415,14 @@ export class Controller {
|
|||||||
controller.progressview.addProgress("measure_moisture", counter / limit * 100, "Measure Moisture " + (limit - counter) + "s")
|
controller.progressview.addProgress("measure_moisture", counter / limit * 100, "Measure Moisture " + (limit - counter) + "s")
|
||||||
|
|
||||||
let timerId: string | number | NodeJS.Timeout | undefined
|
let timerId: string | number | NodeJS.Timeout | undefined
|
||||||
|
|
||||||
function updateProgress() {
|
function updateProgress() {
|
||||||
counter++;
|
counter++;
|
||||||
controller.progressview.addProgress("measure_moisture", counter / limit * 100, "Measure Moisture " + (limit - counter) + "s")
|
controller.progressview.addProgress("measure_moisture", counter / limit * 100, "Measure Moisture " + (limit - counter) + "s")
|
||||||
timerId = setTimeout(updateProgress, 1000);
|
timerId = setTimeout(updateProgress, 1000);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timerId = setTimeout(updateProgress, 1000);
|
timerId = setTimeout(updateProgress, 1000);
|
||||||
|
|
||||||
|
|
||||||
@ -482,6 +496,7 @@ export class Controller {
|
|||||||
readonly solarView: SolarView;
|
readonly solarView: SolarView;
|
||||||
readonly fileview: FileView;
|
readonly fileview: FileView;
|
||||||
readonly logView: LogView
|
readonly logView: LogView
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.timeView = new TimeView(this)
|
this.timeView = new TimeView(this)
|
||||||
this.plantViews = new PlantViews(this)
|
this.plantViews = new PlantViews(this)
|
||||||
@ -506,6 +521,7 @@ export class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const controller = new Controller();
|
const controller = new Controller();
|
||||||
controller.progressview.removeProgress("rebooting");
|
controller.progressview.removeProgress("rebooting");
|
||||||
|
|
||||||
|
@ -30,8 +30,12 @@ export class SubmitView {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.backupBtn.onclick = () => {
|
this.backupBtn.onclick = () => {
|
||||||
controller.backupConfig(this.json.textContent as string, (status: string) => {
|
controller.progressview.addIndeterminate("backup", "Backup to EEPROM running")
|
||||||
this.submit_status.innerHTML = status;
|
controller.backupConfig(this.json.textContent as string).then(saveStatus => {
|
||||||
|
controller.getBackupInfo().then(r => {
|
||||||
|
controller.progressview.removeProgress("backup")
|
||||||
|
this.submit_status.innerHTML = saveStatus;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.restoreBackupBtn.onclick = () => {
|
this.restoreBackupBtn.onclick = () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user