switch to bulk measurements
This commit is contained in:
		@@ -1,9 +1,9 @@
 | 
			
		||||
use crate::hal::Moistures;
 | 
			
		||||
use crate::{
 | 
			
		||||
    config::PlantConfig,
 | 
			
		||||
    hal::{Sensor, HAL},
 | 
			
		||||
    hal::HAL,
 | 
			
		||||
    in_time_range,
 | 
			
		||||
};
 | 
			
		||||
use alloc::string::{String, ToString};
 | 
			
		||||
use chrono::{DateTime, TimeDelta, Utc};
 | 
			
		||||
use chrono_tz::Tz;
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
@@ -15,7 +15,6 @@ const MOIST_SENSOR_MIN_FREQUENCY: f32 = 150.; // this is really, really dry, thi
 | 
			
		||||
pub enum MoistureSensorError {
 | 
			
		||||
    ShortCircuit { hz: f32, max: f32 },
 | 
			
		||||
    OpenLoop { hz: f32, min: f32 },
 | 
			
		||||
    BoardError(String),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, PartialEq, Serialize)]
 | 
			
		||||
@@ -116,15 +115,11 @@ fn map_range_moisture(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl PlantState {
 | 
			
		||||
    pub async fn read_hardware_state(plant_id: usize, board: &mut HAL<'_>) -> Self {
 | 
			
		||||
    pub async fn read_hardware_state(moistures: Moistures, plant_id: usize, board: &mut HAL<'_>) -> Self {
 | 
			
		||||
        let sensor_a = if board.board_hal.get_config().plants[plant_id].sensor_a {
 | 
			
		||||
            match board
 | 
			
		||||
                .board_hal
 | 
			
		||||
                .measure_moisture_hz(plant_id, Sensor::A)
 | 
			
		||||
                .await
 | 
			
		||||
            {
 | 
			
		||||
                Ok(raw) => match map_range_moisture(
 | 
			
		||||
                    raw,
 | 
			
		||||
            let raw = moistures.sensor_a_hz[plant_id];
 | 
			
		||||
            match map_range_moisture(
 | 
			
		||||
                raw,
 | 
			
		||||
                    board.board_hal.get_config().plants[plant_id].moisture_sensor_min_frequency,
 | 
			
		||||
                    board.board_hal.get_config().plants[plant_id].moisture_sensor_max_frequency,
 | 
			
		||||
                ) {
 | 
			
		||||
@@ -133,35 +128,23 @@ impl PlantState {
 | 
			
		||||
                        moisture_percent,
 | 
			
		||||
                    },
 | 
			
		||||
                    Err(err) => MoistureSensorState::SensorError(err),
 | 
			
		||||
                },
 | 
			
		||||
                Err(err) => MoistureSensorState::SensorError(MoistureSensorError::BoardError(
 | 
			
		||||
                    err.to_string(),
 | 
			
		||||
                )),
 | 
			
		||||
            }
 | 
			
		||||
                }
 | 
			
		||||
        } else {
 | 
			
		||||
            MoistureSensorState::Disabled
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let sensor_b = if board.board_hal.get_config().plants[plant_id].sensor_b {
 | 
			
		||||
            match board
 | 
			
		||||
                .board_hal
 | 
			
		||||
                .measure_moisture_hz(plant_id, Sensor::B)
 | 
			
		||||
                .await
 | 
			
		||||
            {
 | 
			
		||||
                Ok(raw) => match map_range_moisture(
 | 
			
		||||
                    raw,
 | 
			
		||||
                    board.board_hal.get_config().plants[plant_id].moisture_sensor_min_frequency,
 | 
			
		||||
                    board.board_hal.get_config().plants[plant_id].moisture_sensor_max_frequency,
 | 
			
		||||
                ) {
 | 
			
		||||
                    Ok(moisture_percent) => MoistureSensorState::MoistureValue {
 | 
			
		||||
                        raw_hz: raw,
 | 
			
		||||
                        moisture_percent,
 | 
			
		||||
                    },
 | 
			
		||||
                    Err(err) => MoistureSensorState::SensorError(err),
 | 
			
		||||
            let raw = moistures.sensor_b_hz[plant_id];
 | 
			
		||||
            match map_range_moisture(
 | 
			
		||||
                raw,
 | 
			
		||||
                board.board_hal.get_config().plants[plant_id].moisture_sensor_min_frequency,
 | 
			
		||||
                board.board_hal.get_config().plants[plant_id].moisture_sensor_max_frequency,
 | 
			
		||||
            ) {
 | 
			
		||||
                Ok(moisture_percent) => MoistureSensorState::MoistureValue {
 | 
			
		||||
                    raw_hz: raw,
 | 
			
		||||
                    moisture_percent,
 | 
			
		||||
                },
 | 
			
		||||
                Err(err) => MoistureSensorState::SensorError(MoistureSensorError::BoardError(
 | 
			
		||||
                    err.to_string(),
 | 
			
		||||
                )),
 | 
			
		||||
                Err(err) => MoistureSensorState::SensorError(err),
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            MoistureSensorState::Disabled
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user