From 53f4ba9a6bd5cd4a56e4d44b5cf306d456968eb0 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 17 May 2023 21:04:21 +0200 Subject: [PATCH] Increase upper threshold and add lower threshold with yellow light --- debug.log | 0 rust-toolchain.toml | 2 ++ src/main.rs | 27 +++++++++++++++++++++------ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 debug.log create mode 100644 rust-toolchain.toml diff --git a/debug.log b/debug.log new file mode 100644 index 0000000..e69de29 diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..271800c --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c1f1296..3bd2eae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![no_std] #![no_main] +#![feature(exclusive_range_pattern)] use embedded_alloc::Heap; use panic_halt as _; @@ -43,13 +44,14 @@ const HEAP_SIZE: usize = 1024; const XTAL_FREQ_HZ: u32 = 12_000_000u32; const MVG_AVG_COUNT: u16 = 20; +const DISPLAY_UPDATE_INT_TICKS: u32 = 500_000; +const SENSOR_READ_INT_TICKS: u32 = 50_000; +const AIR_QUALITY_THRESHOLD: u16 = 480; +const AIR_QUALITY_THRESHOLD_LOWER: u16 = 400; #[rtic::app(device = hal::pac, peripherals = true)] mod app { - const DISPLAY_UPDATE_INT_TICKS: u32 = 500_000; - const SENSOR_READ_INT_TICKS: u32 = 50_000; - use super::*; type DisplayI2C = hal::I2C< I2C1, @@ -80,6 +82,7 @@ mod app { adc: Adc, adc_pin: Pin, watchdog: hal::Watchdog, + use_lower_limit: bool, } #[init()] @@ -157,6 +160,7 @@ mod app { adc, adc_pin, watchdog, + use_lower_limit: false, }, init::Monotonics(), ) @@ -210,16 +214,27 @@ mod app { }); } - #[task(binds = TIMER_IRQ_1, priority = 2, shared = [alarm1, sensor_value, led], local = [adc, adc_pin, watchdog])] + #[task(binds = TIMER_IRQ_1, priority = 2, shared = [alarm1, sensor_value, led], local = [adc, adc_pin, watchdog, use_lower_limit])] fn read_sensor(mut c: read_sensor::Context) { let mut sensor_value: u16 = c.local.adc.read(c.local.adc_pin).unwrap(); sensor_value = c.shared.sensor_value.lock(|v| { *v = (*v * (MVG_AVG_COUNT - 1) + sensor_value) / MVG_AVG_COUNT; *v }); + + if sensor_value >= AIR_QUALITY_THRESHOLD { + *c.local.use_lower_limit = true; + } else if sensor_value < AIR_QUALITY_THRESHOLD_LOWER && *c.local.use_lower_limit { + *c.local.use_lower_limit = false; + } + let color = match sensor_value { - 0..=450 => RGB8::new(255, 0, 0), - 451.. => RGB8::new(0, 255, 0), + 0..AIR_QUALITY_THRESHOLD_LOWER => RGB8::new(255, 0, 0), + AIR_QUALITY_THRESHOLD_LOWER..AIR_QUALITY_THRESHOLD if *c.local.use_lower_limit => { + RGB8::new(200, 255, 0) + } + AIR_QUALITY_THRESHOLD_LOWER..AIR_QUALITY_THRESHOLD => RGB8::new(255, 0, 0), + AIR_QUALITY_THRESHOLD.. => RGB8::new(0, 255, 0), }; c.shared.led.lock(|l| {