From ef002e918cab9146142194a4bc3516dcb5f579f9 Mon Sep 17 00:00:00 2001 From: Empire Date: Sun, 26 Jan 2025 18:00:35 +0100 Subject: [PATCH] added ringbuffer --- rust/Cargo.toml | 1 + rust/src/plant_hal.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 062344c..c457648 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -81,6 +81,7 @@ eeprom24x = "0.7.2" url = "2.5.3" crc = "3.2.1" bincode = "1.3.3" +ringbuffer = "0.15.0" [patch.crates-io] diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index 1837bd1..c136503 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -30,6 +30,7 @@ use esp_idf_sys::esp_restart; use anyhow::{anyhow, Context}; use anyhow::{bail, Ok, Result}; +use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; use serde::{Deserialize, Serialize}; use std::ffi::CString; use std::fs::{self, File}; @@ -45,7 +46,7 @@ use std::time::Duration; use embedded_hal::digital::OutputPin; use esp_idf_hal::delay::Delay; -use esp_idf_hal::gpio::{AnyInputPin, Gpio18, Gpio5, IOPin, InputOutput, Level, PinDriver, Pull}; +use esp_idf_hal::gpio::{AnyInputPin, Gpio18, Gpio5, Gpio7, Gpio8, IOPin, InputOutput, Level, PinDriver, Pull}; use esp_idf_hal::pcnt::{ PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex, }; @@ -127,6 +128,12 @@ static mut LOW_VOLTAGE_DETECTED: bool = false; #[link_section = ".rtc.data"] static mut RESTART_TO_CONF: bool = false; +const BUFFER_SIZE:usize = 120; +const ENTRY_SIZE:usize = 120; +#[link_section = ".rtc.data"] +static mut BUFFER:ConstGenericRingBuffer::, BUFFER_SIZE> = ConstGenericRingBuffer::, BUFFER_SIZE>::new(); + + pub struct FileSystemSizeInfo { pub total_size: usize, pub used_size: usize, @@ -273,7 +280,21 @@ impl PlantCtrlBoard<'_> { let checksum = X25.checksum(&data_buffer); if checksum != header.crc16 { bail!("Invalid checksum, got {} but expected {}", checksum, header.crc16 ); - } + } + + unsafe { + let value = heapless::String::try_from("dummy").unwrap(); + BUFFER.push(value); + } + unsafe { + for entry in BUFFER.iter() { + let test = entry.as_bytes().to_owned(); + for p in test { + data_buffer.push(p); + } + + } + } Ok(data_buffer) } @@ -1415,6 +1436,7 @@ impl PlantHal { LAST_WATERING_TIMESTAMP = [0; PLANT_COUNT]; CONSECUTIVE_WATERING_PLANT = [0; PLANT_COUNT]; LOW_VOLTAGE_DETECTED = false; + BUFFER = ConstGenericRingBuffer::, BUFFER_SIZE>::new(); RESTART_TO_CONF = to_config_mode; }; } else { @@ -1482,7 +1504,7 @@ impl PlantHal { let tank_channel: AdcChannelDriver> = AdcChannelDriver::new(tank_driver, peripherals.pins.gpio5, &adc_config)?; - let mut solar_is_day = PinDriver::input(peripherals.pins.gpio8.downgrade())?; + let mut solar_is_day = PinDriver::input(peripherals.pins.gpio7.downgrade())?; solar_is_day.set_pull(Pull::Floating)?; let mut boot_button = PinDriver::input(peripherals.pins.gpio9.downgrade())?;