Increase energy efficiency by reducing polling frequency

main
trivernis 1 year ago
parent 53f4ba9a6b
commit 4dd52710c9
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

52
Cargo.lock generated

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "arrayvec" name = "arrayvec"
version = "0.7.2" version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]] [[package]]
name = "atmo-sense" name = "atmo-sense"
@ -227,9 +227,9 @@ dependencies = [
[[package]] [[package]]
name = "embedded-graphics" name = "embedded-graphics"
version = "0.7.1" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "750082c65094fbcc4baf9ba31583ce9a8bb7f52cadfb96f6164b1bc7f922f32b" checksum = "cd2a8e0250a7e1212828166b01eed0219e488ebb2599f44624a29c9bd249f397"
dependencies = [ dependencies = [
"az", "az",
"byteorder", "byteorder",
@ -240,9 +240,9 @@ dependencies = [
[[package]] [[package]]
name = "embedded-graphics-core" name = "embedded-graphics-core"
version = "0.3.3" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b1239db5f3eeb7e33e35bd10bd014e7b2537b17e071f726a09351431337cfa" checksum = "ba9ecd261f991856250d2207f6d8376946cd9f412a2165d3b75bc87a0bc7a044"
dependencies = [ dependencies = [
"az", "az",
"byteorder", "byteorder",
@ -269,18 +269,18 @@ dependencies = [
[[package]] [[package]]
name = "float-cmp" name = "float-cmp"
version = "0.8.0" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1267f4ac4f343772758f7b1bdcbe767c218bbab93bb432acbf5162bbf85a6c4" checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
dependencies = [ dependencies = [
"num-traits", "num-traits",
] ]
[[package]] [[package]]
name = "fugit" name = "fugit"
version = "0.3.6" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab17bb279def6720d058cb6c052249938e7f99260ab534879281a95367a87e5" checksum = "17186ad64927d5ac8f02c1e77ccefa08ccd9eaa314d5a4772278aa204a22f7e7"
dependencies = [ dependencies = [
"gcd", "gcd",
] ]
@ -346,9 +346,9 @@ checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.9" version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"scopeguard", "scopeguard",
@ -356,9 +356,9 @@ dependencies = [
[[package]] [[package]]
name = "micromath" name = "micromath"
version = "1.1.1" version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc4010833aea396656c2f91ee704d51a6f1329ec2ab56ffd00bfd56f7481ea94" checksum = "39617bc909d64b068dcffd0e3e31679195b5576d0c83fadc52690268cc2b2b55"
[[package]] [[package]]
name = "nb" name = "nb"
@ -466,9 +466,9 @@ checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
[[package]] [[package]]
name = "paste" name = "paste"
version = "1.0.12" version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" checksum = "b4b27ab7be369122c218afc2079489cdcb4b517c0a3fc386ff11e1fedfcc2b35"
[[package]] [[package]]
name = "pio" name = "pio"
@ -507,18 +507,18 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.56" version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.27" version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -558,9 +558,9 @@ dependencies = [
[[package]] [[package]]
name = "rp2040-hal" name = "rp2040-hal"
version = "0.8.1" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9888dd4a476bb7b25000f6497fed50ade399c38bbfebaa18d14f4fc651a62855" checksum = "1369bb84862d7f69391a96606b2f29a00bfce7f29a749e23d5f01fc3f607ada0"
dependencies = [ dependencies = [
"cortex-m", "cortex-m",
"critical-section", "critical-section",
@ -700,9 +700,9 @@ dependencies = [
[[package]] [[package]]
name = "ssd1306" name = "ssd1306"
version = "0.7.1" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f066d5458b0a27d938dd2b71a8b397d88c5776cb316b8a39f19ef49b6d290709" checksum = "e1eaea5daefc39bfa675e4e75e484af7e54628ffe4fc5300e52bde5905f6b677"
dependencies = [ dependencies = [
"display-interface", "display-interface",
"display-interface-i2c", "display-interface-i2c",
@ -741,9 +741,9 @@ dependencies = [
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.8" version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73"
[[package]] [[package]]
name = "usb-device" name = "usb-device"

@ -12,15 +12,15 @@ cortex-m-rt = "0.7.3"
embedded-hal = { version = "0.2.7", features = ["unproven"] } embedded-hal = { version = "0.2.7", features = ["unproven"] }
rp2040-boot2 = "0.3.0" rp2040-boot2 = "0.3.0"
panic-halt = "0.2.0" panic-halt = "0.2.0"
rp2040-hal = { version = "0.8.1", features = ["rt", "rom-func-cache", "critical-section-impl"] } rp2040-hal = { version = "0.8.2", features = ["rt", "rom-func-cache", "critical-section-impl"] }
fugit = "0.3.6" fugit = "0.3.7"
nb = "1.1.0" nb = "1.1.0"
cortex-m-rtic = "1.1.4" cortex-m-rtic = "1.1.4"
rtic-monotonic = "1.0.0" rtic-monotonic = "1.0.0"
systick-monotonic = "1.0.1" systick-monotonic = "1.0.1"
embedded-time = "0.12.1" embedded-time = "0.12.1"
ssd1306 = "0.7.1" ssd1306 = "0.8.0"
embedded-graphics = "0.7.1" embedded-graphics = "0.8.0"
smart-leds = "0.3.0" smart-leds = "0.3.0"
ws2812-pio = "0.6.0" ws2812-pio = "0.6.0"
waveshare-rp2040-zero = "0.6.0" waveshare-rp2040-zero = "0.6.0"

@ -33,7 +33,7 @@ use hal::{
}, },
pac::I2C1, pac::I2C1,
prelude::*, prelude::*,
timer::{Alarm, Alarm0, Alarm1}, timer::{Alarm, Alarm0, Alarm1, Alarm2},
Adc, Adc,
}; };
@ -44,8 +44,8 @@ const HEAP_SIZE: usize = 1024;
const XTAL_FREQ_HZ: u32 = 12_000_000u32; const XTAL_FREQ_HZ: u32 = 12_000_000u32;
const MVG_AVG_COUNT: u16 = 20; const MVG_AVG_COUNT: u16 = 20;
const DISPLAY_UPDATE_INT_TICKS: u32 = 500_000; const DISPLAY_UPDATE_INT_TICKS: u32 = 2_000_000;
const SENSOR_READ_INT_TICKS: u32 = 50_000; const SENSOR_READ_INT_TICKS: u32 = 1_000_000;
const AIR_QUALITY_THRESHOLD: u16 = 480; const AIR_QUALITY_THRESHOLD: u16 = 480;
const AIR_QUALITY_THRESHOLD_LOWER: u16 = 400; const AIR_QUALITY_THRESHOLD_LOWER: u16 = 400;
@ -72,8 +72,8 @@ mod app {
struct Shared { struct Shared {
alarm0: Alarm0, alarm0: Alarm0,
alarm1: Alarm1, alarm1: Alarm1,
alarm2: Alarm2,
sensor_value: u16, sensor_value: u16,
led: LED,
} }
#[local] #[local]
@ -83,6 +83,8 @@ mod app {
adc_pin: Pin<hal::gpio::bank0::Gpio29, hal::gpio::FloatingInput>, adc_pin: Pin<hal::gpio::bank0::Gpio29, hal::gpio::FloatingInput>,
watchdog: hal::Watchdog, watchdog: hal::Watchdog,
use_lower_limit: bool, use_lower_limit: bool,
led: LED,
led_color: smart_leds::RGB<u8>,
} }
#[init()] #[init()]
@ -121,6 +123,10 @@ mod app {
let _ = alarm1.schedule(MicrosDurationU32::from_ticks(SENSOR_READ_INT_TICKS)); let _ = alarm1.schedule(MicrosDurationU32::from_ticks(SENSOR_READ_INT_TICKS));
alarm1.enable_interrupt(); alarm1.enable_interrupt();
let mut alarm2 = timer.alarm_2().unwrap();
let _ = alarm2.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS));
alarm2.enable_interrupt();
let (mut pio, sm0, _, _, _) = c.device.PIO0.split(&mut resets); let (mut pio, sm0, _, _, _) = c.device.PIO0.split(&mut resets);
let mut led = ws2812_pio::Ws2812::new( let mut led = ws2812_pio::Ws2812::new(
pins.gpio16.into_mode(), pins.gpio16.into_mode(),
@ -130,8 +136,6 @@ mod app {
unsafe { core::mem::transmute(timer.count_down()) }, unsafe { core::mem::transmute(timer.count_down()) },
); );
led.write([RGB8::new(255, 255, 0)].iter().copied()).unwrap();
let scl = pins.gpio15.into_mode(); let scl = pins.gpio15.into_mode();
let sda = pins.gpio14.into_mode(); let sda = pins.gpio14.into_mode();
let i2c = i2c::I2C::i2c1( let i2c = i2c::I2C::i2c1(
@ -152,8 +156,8 @@ mod app {
Shared { Shared {
alarm0, alarm0,
alarm1, alarm1,
alarm2,
sensor_value: adc.read(&mut adc_pin).unwrap(), sensor_value: adc.read(&mut adc_pin).unwrap(),
led,
}, },
Local { Local {
display, display,
@ -161,6 +165,8 @@ mod app {
adc_pin, adc_pin,
watchdog, watchdog,
use_lower_limit: false, use_lower_limit: false,
led,
led_color: RGB8::new(255, 255, 255),
}, },
init::Monotonics(), init::Monotonics(),
) )
@ -197,7 +203,7 @@ mod app {
let text = c.shared.sensor_value.lock(|val| format!("{val}")); let text = c.shared.sensor_value.lock(|val| format!("{val}"));
let display = c.local.display; let display = c.local.display;
display.clear(); display.clear(BinaryColor::Off).unwrap();
Text::with_alignment( Text::with_alignment(
&text, &text,
display.bounding_box().center(), display.bounding_box().center(),
@ -214,14 +220,24 @@ mod app {
}); });
} }
#[task(binds = TIMER_IRQ_1, priority = 2, shared = [alarm1, sensor_value, led], local = [adc, adc_pin, watchdog, use_lower_limit])] #[task(binds = TIMER_IRQ_1, priority = 2, shared = [alarm1, sensor_value], local = [adc, adc_pin, watchdog])]
fn read_sensor(mut c: read_sensor::Context) { fn read_sensor(mut c: read_sensor::Context) {
let mut sensor_value: u16 = c.local.adc.read(c.local.adc_pin).unwrap(); let sensor_value: u16 = c.local.adc.read(c.local.adc_pin).unwrap();
sensor_value = c.shared.sensor_value.lock(|v| { c.shared.sensor_value.lock(|v| {
*v = (*v * (MVG_AVG_COUNT - 1) + sensor_value) / MVG_AVG_COUNT; *v = (*v * (MVG_AVG_COUNT - 1) + sensor_value) / MVG_AVG_COUNT;
*v
}); });
c.local.watchdog.feed();
c.shared.alarm1.lock(|a| {
a.clear_interrupt();
let _ = a.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS));
});
}
#[task(binds=TIMER_IRQ_2, priority = 1, shared = [alarm2, sensor_value], local = [led, led_color, use_lower_limit])]
fn update_led(mut c: update_led::Context) {
let sensor_value = c.shared.sensor_value.lock(|v| *v);
if sensor_value >= AIR_QUALITY_THRESHOLD { if sensor_value >= AIR_QUALITY_THRESHOLD {
*c.local.use_lower_limit = true; *c.local.use_lower_limit = true;
} else if sensor_value < AIR_QUALITY_THRESHOLD_LOWER && *c.local.use_lower_limit { } else if sensor_value < AIR_QUALITY_THRESHOLD_LOWER && *c.local.use_lower_limit {
@ -237,12 +253,12 @@ mod app {
AIR_QUALITY_THRESHOLD.. => RGB8::new(0, 255, 0), AIR_QUALITY_THRESHOLD.. => RGB8::new(0, 255, 0),
}; };
c.shared.led.lock(|l| { if *c.local.led_color != color {
l.write([color].iter().cloned()).unwrap(); c.local.led.write([color].iter().cloned()).unwrap();
}); *c.local.led_color = color;
}
c.local.watchdog.feed(); c.shared.alarm2.lock(|a| {
c.shared.alarm1.lock(|a| {
a.clear_interrupt(); a.clear_interrupt();
let _ = a.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS)); let _ = a.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS));
}); });

Loading…
Cancel
Save