127 lines
3.7 KiB
Rust
127 lines
3.7 KiB
Rust
use crate::config::{BoardHardware, PlantControllerConfig};
|
|
use crate::hal::battery::{BatteryInteraction, NoBatteryMonitor};
|
|
use crate::hal::esp::ESP;
|
|
use crate::hal::{deep_sleep, BackupHeader, BoardInteraction, FreePeripherals, Sensor};
|
|
use anyhow::{bail, Result};
|
|
use chrono::{DateTime, Utc};
|
|
use embedded_hal::digital::OutputPin;
|
|
use esp_idf_hal::gpio::{IOPin, Pull};
|
|
use esp_idf_hal::gpio::{InputOutput, PinDriver};
|
|
|
|
pub struct Initial<'a> {
|
|
pub(crate) general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
|
|
pub(crate) esp: ESP<'a>,
|
|
pub(crate) config: PlantControllerConfig,
|
|
pub(crate) battery: Box<dyn BatteryInteraction + Send>,
|
|
}
|
|
|
|
pub(crate) fn create_initial_board(
|
|
free_pins: FreePeripherals,
|
|
fs_mount_error: bool,
|
|
config: PlantControllerConfig,
|
|
esp: ESP<'static>,
|
|
) -> Result<Box<dyn BoardInteraction<'static> + Send>> {
|
|
let mut general_fault = PinDriver::input_output(free_pins.gpio6.downgrade())?;
|
|
general_fault.set_pull(Pull::Floating)?;
|
|
general_fault.set_low()?;
|
|
|
|
if fs_mount_error {
|
|
general_fault.set_high()?
|
|
}
|
|
let v = Initial {
|
|
general_fault,
|
|
config,
|
|
esp,
|
|
battery: Box::new(NoBatteryMonitor {}),
|
|
};
|
|
Ok(Box::new(v))
|
|
}
|
|
|
|
impl<'a> BoardInteraction<'a> for Initial<'a> {
|
|
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
|
|
}
|
|
|
|
fn set_charge_indicator(&mut self, _charging: bool) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
|
|
deep_sleep(duration_in_ms)
|
|
}
|
|
|
|
fn get_backup_info(&mut self) -> Result<BackupHeader> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn get_backup_config(&mut self) -> Result<Vec<u8>> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn backup_config(&mut self, _bytes: &[u8]) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn is_day(&self) -> bool {
|
|
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<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn pump(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
fn fault(&mut self, _plant: usize, _enable: bool) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
fn measure_moisture_hz(&mut self, _plant: usize, _sensor: Sensor) -> Result<f32> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn general_fault(&mut self, enable: bool) {
|
|
let _ = self.general_fault.set_state(enable.into());
|
|
}
|
|
|
|
fn factory_reset(&mut self) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn get_rtc_time(&mut self) -> Result<DateTime<Utc>> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn set_rtc_time(&mut self, _time: &DateTime<Utc>) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
fn test_pump(&mut self, _plant: usize) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn test(&mut self) -> Result<()> {
|
|
bail!("Please configure board revision")
|
|
}
|
|
|
|
fn set_config(&mut self, config: PlantControllerConfig) -> anyhow::Result<()> {
|
|
self.config = config;
|
|
self.esp.save_config(&self.config)?;
|
|
anyhow::Ok(())
|
|
}
|
|
}
|