|
|
|
@ -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<hal::gpio::bank0::Gpio29, hal::gpio::FloatingInput>,
|
|
|
|
|
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| {
|
|
|
|
|