From b2b52ff4cdb6a3092e1d5af66f0938a507e54cbc Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 12 May 2023 17:08:58 +0200 Subject: [PATCH] Add led status indicator for when to open a window --- Cargo.lock | 160 +++++++++++++++++++++++++++++++++++++--------------- Cargo.toml | 14 +++-- src/main.rs | 114 ++++++++++++++++++++++++------------- 3 files changed, 198 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 332fde7..404466b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,15 +19,17 @@ dependencies = [ "embedded-hal", "embedded-time", "fugit", - "nb 1.0.0", + "nb 1.1.0", "numtoa", "panic-halt", - "rp2040-boot2", + "rp2040-boot2 0.3.0", "rp2040-hal", "rtic-monotonic", - "sparkfun-pro-micro-rp2040", + "smart-leds", "ssd1306", "systick-monotonic", + "waveshare-rp2040-zero", + "ws2812-pio", ] [[package]] @@ -78,6 +80,12 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0a5e3906bcbf133e33c1d4d95afc664ad37fbdb9f6568d8043e7ea8c27d93d3" +[[package]] +name = "bytemuck" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17febce684fd15d89027105661fec94afb475cb995fbc59d2865198446ba2eea" + [[package]] name = "byteorder" version = "1.4.3" @@ -98,9 +106,9 @@ dependencies = [ [[package]] name = "cortex-m-rt" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6d3328b8b5534f0c90acd66b68950f2763b37e0173cac4d8b4937c4a80761f9" +checksum = "ee84e813d593101b1723e13ec38b6ab6abbdbaaa4546553f5395ed274079ddb1" dependencies = [ "cortex-m-rt-macros", ] @@ -118,9 +126,9 @@ dependencies = [ [[package]] name = "cortex-m-rtic" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6b82f1c39acd6c3a35c2013b6110c20f5bc534522791fabadeed49ccada2dce" +checksum = "d696ae7390bdb9f7978f71ca7144256a2c4616240a6df9002da3c451f9fc8f02" dependencies = [ "bare-metal 1.0.0", "cortex-m", @@ -133,9 +141,9 @@ dependencies = [ [[package]] name = "cortex-m-rtic-macros" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8e9645ef54bec1cf70ac33e9bf9566e6507ab5b41ae6baf3735662194e8607" +checksum = "eefb40b1ca901c759d29526e5c8a0a1b246c20caaa5b4cc5d0f0b94debecd4c7" dependencies = [ "proc-macro-error", "proc-macro2", @@ -198,6 +206,15 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "embedded-dma" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "994f7e5b5cb23521c22304927195f236813053eb9c065dd2226a32ba64695446" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "embedded-graphics" version = "0.7.1" @@ -294,9 +311,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.9.2" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown", @@ -333,14 +350,14 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" dependencies = [ - "nb 1.0.0", + "nb 1.1.0", ] [[package]] name = "nb" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "546c37ac5d9e56f55e73b677106873d9d9f5190605e41a856503623648488cae" +checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" [[package]] name = "num" @@ -407,18 +424,18 @@ dependencies = [ [[package]] name = "num_enum" -version = "0.5.9" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d829733185c1ca374f17e52b762f24f535ec625d2cc1f070e34c8a9068f341b" +checksum = "1f646caf906c20226733ed5b1374287eb97e3c2a5c227ce668c1f2ce20ae57c9" dependencies = [ "num_enum_derive", ] [[package]] name = "num_enum_derive" -version = "0.5.9" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2be1598bf1c313dcdd12092e3f1920f463462525a21b7b4e11b4168353d0123e" +checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799" dependencies = [ "proc-macro2", "quote", @@ -439,9 +456,9 @@ checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812" [[package]] name = "paste" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" +checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" [[package]] name = "pio" @@ -480,18 +497,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.51" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.23" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" dependencies = [ "proc-macro2", ] @@ -502,6 +519,15 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rgb" +version = "0.8.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20ec2d3e3fc7a92ced357df9cebd5a10b6fb2aa1ee797bf7e9ce2f17dffc8f59" +dependencies = [ + "bytemuck", +] + [[package]] name = "rp2040-boot2" version = "0.2.1" @@ -511,18 +537,28 @@ dependencies = [ "crc-any", ] +[[package]] +name = "rp2040-boot2" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c92f344f63f950ee36cf4080050e4dce850839b9175da38f9d2ffb69b4dbb21" +dependencies = [ + "crc-any", +] + [[package]] name = "rp2040-hal" -version = "0.7.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ecf1b975581f0cac465247c464e7d2b8d93c7a5fceb4eb13b7b8517f4f85f6d" +checksum = "9888dd4a476bb7b25000f6497fed50ade399c38bbfebaa18d14f4fc651a62855" dependencies = [ "cortex-m", "critical-section", + "embedded-dma", "embedded-hal", "fugit", "itertools", - "nb 1.0.0", + "nb 1.1.0", "paste", "pio", "rand_core", @@ -570,9 +606,9 @@ checksum = "fb8b0b822d1a366470b9cea83a1d4e788392db763539dc4ba022bcc787fece82" [[package]] name = "rtic-syntax" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ad3ae243dd8d0a1b064615f664d4fa7e63929939074c564cbe5efdc4c503065" +checksum = "5f5e215601dc467752c2bddc6284a622c6f3d2bab569d992adcd5ab7e4cb9478" dependencies = [ "indexmap", "proc-macro2", @@ -595,7 +631,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ - "semver 1.0.16", + "semver 1.0.17", ] [[package]] @@ -615,9 +651,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a" +checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" [[package]] name = "semver-parser" @@ -626,23 +662,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] -name = "sparkfun-pro-micro-rp2040" -version = "0.5.0" +name = "smart-leds" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a38e3e0ef1201e3e1fe3e40c2facd95fa1235dda391e002724544a0b885bbe07" +checksum = "38dd45fa275f70b4110eac5f5182611ad384f88bb22b68b9a9c3cafd7015290b" dependencies = [ - "cortex-m", - "cortex-m-rt", - "embedded-hal", - "rp2040-boot2", - "rp2040-hal", + "smart-leds-trait", +] + +[[package]] +name = "smart-leds-trait" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebf6d833fa93f16a1c1874e62c2aebe8567e5bdd436d59bf543ed258b6f7a8e3" +dependencies = [ + "rgb", ] [[package]] name = "spin" -version = "0.9.5" +version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dccf47db1b41fa1573ed27ccf5e08e3ca771cb994f776668c5ebda893b248fc" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" dependencies = [ "lock_api", ] @@ -668,9 +709,9 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "syn" -version = "1.0.107" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", "quote", @@ -690,9 +731,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.6" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" [[package]] name = "usb-device" @@ -726,3 +767,30 @@ checksum = "9ee8f19f9d74293faf70901bc20ad067dc1ad390d2cbf1e3f75f721ffee908b6" dependencies = [ "vcell", ] + +[[package]] +name = "waveshare-rp2040-zero" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e36889ce533ed216200dc2e2179d1fba8f9ba432da2d69406dc2cfe1edc7e89" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "rp2040-boot2 0.2.1", + "rp2040-hal", +] + +[[package]] +name = "ws2812-pio" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d219e3b43c1e14305b36363060c0348d560314e235d999cf492bbbab1f38e8d" +dependencies = [ + "cortex-m", + "embedded-hal", + "fugit", + "nb 1.1.0", + "pio", + "rp2040-hal", + "smart-leds-trait", +] diff --git a/Cargo.toml b/Cargo.toml index 2708cfb..a200ddb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,18 +8,20 @@ license = "CNPLv4+" [dependencies] cortex-m = "0.7.7" -cortex-m-rt = "0.7.2" +cortex-m-rt = "0.7.3" embedded-hal = { version = "0.2.7", features = ["unproven"] } -rp2040-boot2 = "0.2.1" +rp2040-boot2 = "0.3.0" panic-halt = "0.2.0" -rp2040-hal = { version = "0.7.0", features = ["rt", "rom-func-cache", "critical-section-impl"] } +rp2040-hal = { version = "0.8.1", features = ["rt", "rom-func-cache", "critical-section-impl"] } fugit = "0.3.6" -nb = "1.0.0" -cortex-m-rtic = "1.1.3" +nb = "1.1.0" +cortex-m-rtic = "1.1.4" rtic-monotonic = "1.0.0" systick-monotonic = "1.0.1" -sparkfun-pro-micro-rp2040 = "0.5.0" embedded-time = "0.12.1" ssd1306 = "0.7.1" embedded-graphics = "0.7.1" numtoa = "0.2.4" +smart-leds = "0.3.0" +ws2812-pio = "0.6.0" +waveshare-rp2040-zero = "0.6.0" diff --git a/src/main.rs b/src/main.rs index 2d47bc6..4740731 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,8 @@ use fugit::RateExtU32; use cortex_m::prelude::_embedded_hal_watchdog_Watchdog; use embedded_hal::adc::OneShot; use hal::i2c; -pub use sparkfun_pro_micro_rp2040::hal; use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306}; +pub use waveshare_rp2040_zero::hal; use embedded_graphics::text::Text; use embedded_graphics::{ @@ -18,43 +18,51 @@ use embedded_graphics::{ prelude::*, text::Alignment, }; +use smart_leds::{SmartLedsWrite, RGB8}; use fugit::MicrosDurationU32; -use hal::gpio::bank0::{Gpio14, Gpio15}; -use hal::gpio::Pin; -use hal::pac::I2C1; -use hal::timer::{Alarm, Alarm0, Alarm1}; -use hal::Adc; +use hal::{ + gpio::{ + bank0::{Gpio14, Gpio15, Gpio16}, + Pin, + }, + pac::I2C1, + prelude::*, + timer::{Alarm, Alarm0, Alarm1}, + Adc, +}; const XTAL_FREQ_HZ: u32 = 12_000_000u32; const MVG_AVG_COUNT: u16 = 20; -#[rtic::app(device = sparkfun_pro_micro_rp2040::hal::pac, peripherals = true)] +#[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, + ( + Pin>, + Pin>, + ), + >; type Display = Ssd1306< - I2CInterface< - hal::I2C< - I2C1, - ( - Pin>, - Pin>, - ), - >, - >, + I2CInterface, DisplaySize128x64, ssd1306::mode::BufferedGraphicsMode, >; + type LED = + ws2812_pio::Ws2812, Gpio16>; #[shared] struct Shared { alarm0: Alarm0, alarm1: Alarm1, sensor_value: u16, + led: LED, } #[local] @@ -71,7 +79,7 @@ mod app { let sio = hal::Sio::new(c.device.SIO); let mut watchdog = hal::Watchdog::new(c.device.WATCHDOG); - let _clocks = hal::clocks::init_clocks_and_plls( + let clocks = hal::clocks::init_clocks_and_plls( XTAL_FREQ_HZ, c.device.XOSC, c.device.CLOCKS, @@ -89,6 +97,7 @@ mod app { sio.gpio_bank0, &mut resets, ); + let mut timer = hal::Timer::new(c.device.TIMER, &mut resets); let mut alarm0 = timer.alarm_0().unwrap(); let _ = alarm0.schedule(MicrosDurationU32::from_ticks(DISPLAY_UPDATE_INT_TICKS)); @@ -98,6 +107,17 @@ mod app { let _ = alarm1.schedule(MicrosDurationU32::from_ticks(SENSOR_READ_INT_TICKS)); alarm1.enable_interrupt(); + let (mut pio, sm0, _, _, _) = c.device.PIO0.split(&mut resets); + let mut led = ws2812_pio::Ws2812::new( + pins.gpio16.into_mode(), + &mut pio, + sm0, + clocks.peripheral_clock.freq(), + unsafe { core::mem::transmute(timer.count_down()) }, + ); + + led.write([RGB8::new(255, 255, 0)].iter().copied()).unwrap(); + let scl = pins.gpio15.into_mode(); let sda = pins.gpio14.into_mode(); let i2c = i2c::I2C::i2c1( @@ -108,6 +128,30 @@ mod app { &mut resets, 125_000_000.Hz(), ); + let display = init_display(i2c); + + let mut adc = Adc::new(c.device.ADC, &mut resets); + let mut adc_pin = pins.gpio29.into_floating_input(); + led.write([RGB8::new(0, 255, 255)].iter().cloned()).unwrap(); + + ( + Shared { + alarm0, + alarm1, + sensor_value: adc.read(&mut adc_pin).unwrap(), + led, + }, + Local { + display, + adc, + adc_pin, + watchdog, + }, + init::Monotonics(), + ) + } + + fn init_display(i2c: DisplayI2C) -> Display { let interface = I2CDisplayInterface::new(i2c); let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0) .into_buffered_graphics_mode(); @@ -123,23 +167,8 @@ mod app { .unwrap(); display.flush().unwrap(); - let adc = Adc::new(c.device.ADC, &mut resets); - let adc_pin = pins.gpio29.into_floating_input(); - ( - Shared { - alarm0, - alarm1, - sensor_value: 0, - }, - Local { - display, - adc, - adc_pin, - watchdog, - }, - init::Monotonics(), - ) + display } #[task(binds = TIMER_IRQ_0, priority = 1, shared = [alarm0, sensor_value], local = [display])] @@ -169,12 +198,21 @@ mod app { }); } - #[task(binds = TIMER_IRQ_1, priority = 2, shared = [alarm1, sensor_value], local = [adc, adc_pin, watchdog])] + #[task(binds = TIMER_IRQ_1, priority = 2, shared = [alarm1, sensor_value, led], local = [adc, adc_pin, watchdog])] fn read_sensor(mut c: read_sensor::Context) { - let sensor_value: u16 = c.local.adc.read(c.local.adc_pin).unwrap(); - c.shared - .sensor_value - .lock(|v| *v = (*v * (MVG_AVG_COUNT - 1) + sensor_value) / MVG_AVG_COUNT); + 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 + }); + let color = match sensor_value { + 0..=450 => RGB8::new(255, 0, 0), + 451.. => RGB8::new(0, 255, 0), + }; + + c.shared.led.lock(|l| { + l.write([color].iter().cloned()).unwrap(); + }); c.local.watchdog.feed(); c.shared.alarm1.lock(|a| {