we should be feature compatible with main version now!
This commit is contained in:
@@ -7,6 +7,7 @@ use embassy_embedded_hal::shared_bus::I2cDeviceError;
|
||||
use embassy_executor::SpawnError;
|
||||
use embassy_sync::mutex::TryLockError;
|
||||
use esp_hal::i2c::master::ConfigError;
|
||||
use esp_hal::pcnt::unit::{InvalidHighLimit, InvalidLowLimit};
|
||||
use esp_wifi::wifi::WifiError;
|
||||
use ina219::errors::{BusVoltageReadError, ShuntVoltageReadError};
|
||||
use littlefs2_core::PathError;
|
||||
@@ -263,3 +264,18 @@ impl From<Infallible> for FatError {
|
||||
panic!("Infallible error: {:?}", value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<InvalidLowLimit> for FatError {
|
||||
fn from(value: InvalidLowLimit) -> Self {
|
||||
FatError::String {
|
||||
error: format!("{:?}", value),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<InvalidHighLimit> for FatError {
|
||||
fn from(value: InvalidHighLimit) -> Self {
|
||||
FatError::String {
|
||||
error: format!("{:?}", value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,6 +19,10 @@ use embassy_sync::mutex::Mutex;
|
||||
use embassy_time::Timer;
|
||||
use embedded_hal::digital::OutputPin as _;
|
||||
use esp_hal::gpio::{Flex, Input, InputConfig, Level, Output, OutputConfig, Pull};
|
||||
use esp_hal::pcnt::channel::CtrlMode::Keep;
|
||||
use esp_hal::pcnt::channel::EdgeMode;
|
||||
use esp_hal::pcnt::channel::EdgeMode::{Hold, Increment};
|
||||
use esp_hal::pcnt::unit::Unit;
|
||||
use measurements::{Current, Voltage};
|
||||
|
||||
const PUMP8_BIT: usize = 0;
|
||||
@@ -81,6 +85,7 @@ pub struct V3<'a> {
|
||||
light: Output<'a>,
|
||||
main_pump: Output<'a>,
|
||||
general_fault: Output<'a>,
|
||||
pub signal_counter: Unit<'static, 0>,
|
||||
}
|
||||
|
||||
pub(crate) fn create_v3(
|
||||
@@ -139,6 +144,18 @@ pub(crate) fn create_v3(
|
||||
Output::new(peripherals.gpio21, Level::Low, OutputConfig::default());
|
||||
shift_register_enable_invert.set_low();
|
||||
|
||||
let signal_counter = peripherals.pcnt0;
|
||||
|
||||
signal_counter.set_low_limit(Some(0))?;
|
||||
signal_counter.set_high_limit(Some(i16::MAX))?;
|
||||
|
||||
let ch0 = &signal_counter.channel0;
|
||||
let edge_pin = Input::new(peripherals.gpio22, InputConfig::default());
|
||||
ch0.set_edge_signal(edge_pin.peripheral_input());
|
||||
ch0.set_input_mode(Hold, Increment);
|
||||
ch0.set_ctrl_mode(Keep, Keep);
|
||||
signal_counter.listen();
|
||||
|
||||
Ok(Box::new(V3 {
|
||||
config,
|
||||
battery_monitor,
|
||||
@@ -151,6 +168,7 @@ pub(crate) fn create_v3(
|
||||
light,
|
||||
main_pump,
|
||||
general_fault,
|
||||
signal_counter,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -262,8 +280,8 @@ impl<'a> BoardInteraction<'a> for V3<'a> {
|
||||
async fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> Result<f32, FatError> {
|
||||
let mut results = [0_f32; REPEAT_MOIST_MEASURE];
|
||||
for repeat in 0..REPEAT_MOIST_MEASURE {
|
||||
//self.signal_counter.counter_pause()?;
|
||||
//self.signal_counter.counter_clear()?;
|
||||
self.signal_counter.pause();
|
||||
self.signal_counter.clear();
|
||||
//Disable all
|
||||
{
|
||||
let shift_register = self.shift_register.lock().await;
|
||||
@@ -331,16 +349,16 @@ impl<'a> BoardInteraction<'a> for V3<'a> {
|
||||
|
||||
//give some time to stabilize
|
||||
Timer::after_millis(10).await;
|
||||
//self.signal_counter.counter_resume()?;
|
||||
self.signal_counter.resume();
|
||||
Timer::after_millis(measurement).await;
|
||||
//self.signal_counter.counter_pause()?;
|
||||
self.signal_counter.pause();
|
||||
{
|
||||
let shift_register = self.shift_register.lock().await;
|
||||
shift_register.decompose()[MS_4].set_high()?;
|
||||
shift_register.decompose()[SENSOR_ON].set_low()?;
|
||||
}
|
||||
Timer::after_millis(10).await;
|
||||
let unscaled = 1337; //self.signal_counter.get_counter_value()? as i32;
|
||||
let unscaled = self.signal_counter.value();
|
||||
let hz = unscaled as f32 * factor;
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
|
@@ -18,6 +18,8 @@ use crate::hal::v4_sensor::{SensorImpl, SensorInteraction};
|
||||
use crate::log::{LogMessage, LOG_ACCESS};
|
||||
use esp_hal::gpio::{Flex, Input, InputConfig, Level, Output, OutputConfig, Pull};
|
||||
use esp_hal::i2c::master::I2c;
|
||||
use esp_hal::pcnt::channel::CtrlMode::Keep;
|
||||
use esp_hal::pcnt::channel::EdgeMode::{Hold, Increment};
|
||||
use esp_hal::pcnt::Pcnt;
|
||||
use esp_hal::twai::{EspTwaiFrame, StandardId, TwaiMode};
|
||||
use esp_println::println;
|
||||
@@ -174,28 +176,18 @@ pub(crate) async fn create_v4(
|
||||
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,
|
||||
// },
|
||||
// )?;
|
||||
|
||||
let signal_counter = peripherals.pcnt0;
|
||||
|
||||
signal_counter.set_low_limit(Some(0))?;
|
||||
signal_counter.set_high_limit(Some(i16::MAX))?;
|
||||
|
||||
let ch0 = &signal_counter.channel0;
|
||||
let edge_pin = Input::new(peripherals.gpio22, InputConfig::default());
|
||||
ch0.set_edge_signal(edge_pin.peripheral_input());
|
||||
ch0.set_input_mode(Hold, Increment);
|
||||
ch0.set_ctrl_mode(Keep, Keep);
|
||||
signal_counter.listen();
|
||||
|
||||
for pin in 0..8 {
|
||||
let _ = sensor_expander.pin_into_output(GPIOBank::Bank0, pin);
|
||||
@@ -205,7 +197,7 @@ pub(crate) async fn create_v4(
|
||||
}
|
||||
|
||||
SensorImpl::PulseCounter {
|
||||
// signal_counter,
|
||||
signal_counter,
|
||||
sensor_expander,
|
||||
}
|
||||
}
|
||||
@@ -300,7 +292,6 @@ pub(crate) async fn create_v4(
|
||||
tank_sensor,
|
||||
light,
|
||||
general_fault,
|
||||
//pump_ina,
|
||||
pump_expander,
|
||||
config,
|
||||
battery_monitor,
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use crate::fat_error::{FatError, FatResult};
|
||||
use crate::hal::Box;
|
||||
use crate::hal::Sensor;
|
||||
use crate::log::{LogMessage, LOG_ACCESS};
|
||||
use crate::fat_error::{FatError, FatResult};
|
||||
use alloc::format;
|
||||
use alloc::string::ToString;
|
||||
use async_trait::async_trait;
|
||||
@@ -9,6 +9,7 @@ use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_time::Timer;
|
||||
use esp_hal::i2c::master::I2c;
|
||||
use esp_hal::pcnt::unit::Unit;
|
||||
use esp_hal::twai::Twai;
|
||||
use esp_hal::Blocking;
|
||||
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
||||
@@ -29,7 +30,7 @@ const SENSOR_ON: u8 = 5_u8;
|
||||
|
||||
pub enum SensorImpl {
|
||||
PulseCounter {
|
||||
//signal_counter: PcntDriver<'a>,
|
||||
signal_counter: Unit<'static, 0>,
|
||||
sensor_expander:
|
||||
Pca9535Immediate<I2cDevice<'static, CriticalSectionRawMutex, I2c<'static, Blocking>>>,
|
||||
},
|
||||
@@ -43,14 +44,14 @@ impl SensorInteraction for SensorImpl {
|
||||
async fn measure_moisture_hz(&mut self, plant: usize, sensor: Sensor) -> FatResult<f32> {
|
||||
match self {
|
||||
SensorImpl::PulseCounter {
|
||||
//signal_counter,
|
||||
signal_counter,
|
||||
sensor_expander,
|
||||
..
|
||||
} => {
|
||||
let mut results = [0_f32; REPEAT_MOIST_MEASURE];
|
||||
for repeat in 0..REPEAT_MOIST_MEASURE {
|
||||
//signal_counter.counter_pause()?;
|
||||
//signal_counter.counter_clear()?;
|
||||
signal_counter.pause();
|
||||
signal_counter.clear();
|
||||
|
||||
//Disable all
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?;
|
||||
@@ -90,9 +91,9 @@ impl SensorInteraction for SensorImpl {
|
||||
|
||||
//give some time to stabilize
|
||||
Timer::after_millis(10).await;
|
||||
//signal_counter.counter_resume()?;
|
||||
signal_counter.resume();
|
||||
Timer::after_millis(measurement).await;
|
||||
//signal_counter.counter_pause()?;
|
||||
signal_counter.pause();
|
||||
sensor_expander.pin_set_high(GPIOBank::Bank0, MS4)?;
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, SENSOR_ON)?;
|
||||
sensor_expander.pin_set_low(GPIOBank::Bank0, MS0)?;
|
||||
@@ -121,9 +122,9 @@ impl SensorInteraction for SensorImpl {
|
||||
let median = results[mid];
|
||||
Ok(median)
|
||||
}
|
||||
SensorImpl::CanBus { twai } => {
|
||||
Err(FatError::String {error: "Not yet implemented".to_string()})
|
||||
}
|
||||
SensorImpl::CanBus { twai } => Err(FatError::String {
|
||||
error: "Not yet implemented".to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -763,60 +763,6 @@ async fn update_charge_indicator(
|
||||
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
|
||||
) {
|
||||
//we have mppt controller, ask it for charging current
|
||||
let tank_state = determine_tank_state(board).await;
|
||||
|
||||
if tank_state.is_enabled() {
|
||||
if let Some(err) = tank_state.got_error(&board.board_hal.get_config().tank) {
|
||||
match err {
|
||||
TankError::SensorDisabled => { /* unreachable */ }
|
||||
TankError::SensorMissing(raw_value_mv) => {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::TankSensorMissing,
|
||||
raw_value_mv as u32,
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.await
|
||||
}
|
||||
TankError::SensorValueError { value, min, max } => {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
LogMessage::TankSensorValueRangeError,
|
||||
min as u32,
|
||||
max as u32,
|
||||
&format!("{}", value),
|
||||
"",
|
||||
)
|
||||
.await
|
||||
}
|
||||
TankError::BoardError(err) => {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(LogMessage::TankSensorBoardError, 0, 0, "", &err.to_string())
|
||||
.await
|
||||
}
|
||||
}
|
||||
// disabled cannot trigger this because of wrapping if is_enabled
|
||||
board.board_hal.general_fault(true).await;
|
||||
} else if tank_state
|
||||
.warn_level(&board.board_hal.get_config().tank)
|
||||
.is_ok_and(|warn| warn)
|
||||
{
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(LogMessage::TankWaterLevelLow, 0, 0, "", "")
|
||||
.await;
|
||||
board.board_hal.general_fault(true).await;
|
||||
}
|
||||
}
|
||||
if let Ok(current) = board.board_hal.get_mptt_current().await {
|
||||
let _ = board
|
||||
.board_hal
|
||||
|
Reference in New Issue
Block a user