From b6abebd0120306c675a55effc6cffb4ac29c4558 Mon Sep 17 00:00:00 2001 From: Empire Phoenix Date: Tue, 14 May 2024 21:57:11 +0200 Subject: [PATCH] switch to oneshoot adc, as that seems to work --- rust/src/plant_hal.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index 03d9842..39b0e3e 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -4,6 +4,9 @@ use chrono_tz::Europe::Berlin; use embedded_svc::wifi::{ AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration, }; +use esp_idf_hal::adc::attenuation; +use esp_idf_hal::adc::oneshot::config::AdcChannelConfig; +use esp_idf_hal::adc::oneshot::{AdcChannelDriver, AdcDriver}; use esp_idf_hal::i2c::{APBTickType, I2cConfig, I2cDriver, I2cError}; use esp_idf_hal::units::FromValueType; use esp_idf_svc::eventloop::EspSystemEventLoop; @@ -32,7 +35,6 @@ use std::sync::{Arc, Mutex}; use std::time::Duration; use embedded_hal::digital::OutputPin; -use esp_idf_hal::adc::{attenuation, AdcChannelDriver, AdcDriver}; use esp_idf_hal::delay::Delay; use esp_idf_hal::gpio::{AnyInputPin, Gpio18, Gpio5, IOPin, InputOutput, Level, PinDriver, Pull}; use esp_idf_hal::pcnt::{ @@ -161,8 +163,7 @@ pub struct PlantCtrlBoard<'a> { PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>, >, - tank_driver: AdcDriver<'a, esp_idf_hal::adc::ADC1>, - tank_channel: esp_idf_hal::adc::AdcChannelDriver<'a, { attenuation::DB_11 }, Gpio5>, + tank_channel: AdcChannelDriver<'a, Gpio5, AdcDriver<'a, esp_idf_hal::adc::ADC1>>, solar_is_day: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>, boot_button: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>, signal_counter: PcntDriver<'a>, @@ -222,7 +223,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { let mut store = [0_u16; TANK_MULTI_SAMPLE]; for multisample in 0..TANK_MULTI_SAMPLE { - let value = self.tank_driver.read(&mut self.tank_channel)?; + let value = self.tank_channel.read()?; store[multisample] = value; } store.sort(); @@ -696,9 +697,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { .store(false, std::sync::atomic::Ordering::Relaxed); println!("Mqtt error"); } - _ => { - - } + _ => {} } })?; @@ -950,7 +949,6 @@ impl CreatePlantHal<'_> for PlantHal { fn create() -> Result>> { let peripherals = Peripherals::take()?; - let i2c = peripherals.i2c0; let config = I2cConfig::new() .scl_enable_pullup(true) @@ -961,10 +959,10 @@ impl CreatePlantHal<'_> for PlantHal { let sda = peripherals.pins.gpio20.downgrade(); let driver = I2cDriver::new(i2c, sda, scl, &config).unwrap(); - + let i2c_port = driver.port(); - let mut timeout:i32 = 0; - esp!(unsafe { esp_idf_sys::i2c_get_timeout(i2c_port, &mut timeout)}).unwrap(); + let mut timeout: i32 = 0; + esp!(unsafe { esp_idf_sys::i2c_get_timeout(i2c_port, &mut timeout) }).unwrap(); println!("init i2c timeout is {}", timeout); //esp!(unsafe { esp_idf_sys::i2c_set_timeout(i2c_port, 22)}).unwrap(); @@ -1075,13 +1073,14 @@ impl CreatePlantHal<'_> for PlantHal { let nvs = EspDefaultNvsPartition::take()?; let wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs))?; - let adc_config = esp_idf_hal::adc::config::Config { + let adc_config = AdcChannelConfig { + attenuation: attenuation::DB_11, resolution: esp_idf_hal::adc::config::Resolution::Resolution12Bit, - calibration: true + calibration: true, }; - let tank_driver = AdcDriver::new(peripherals.adc1, &adc_config)?; - let tank_channel: AdcChannelDriver<'_, { attenuation::DB_11 }, Gpio5> = - AdcChannelDriver::new(peripherals.pins.gpio5)?; + let tank_driver = AdcDriver::new(peripherals.adc1)?; + let tank_channel: AdcChannelDriver> = + AdcChannelDriver::new(tank_driver, peripherals.pins.gpio5, &adc_config)?; let mut solar_is_day = PinDriver::input(peripherals.pins.gpio8.downgrade())?; solar_is_day.set_pull(Pull::Floating)?; @@ -1113,7 +1112,6 @@ impl CreatePlantHal<'_> for PlantHal { } let rv = Mutex::new(PlantCtrlBoard { shift_register, - tank_driver, tank_channel, solar_is_day, boot_button, @@ -1126,7 +1124,7 @@ impl CreatePlantHal<'_> for PlantHal { wifi_driver, mqtt_client: None, //battery_driver: None, - battery_driver: Some(battery_driver) + battery_driver: Some(battery_driver), }); Ok(rv) }