From 0c3facd784d15161620dde8bdabd76e9bcdc519b Mon Sep 17 00:00:00 2001 From: Brendan Ball Date: Sun, 5 Nov 2023 07:44:22 +0100 Subject: [PATCH] add button interrupt --- Cargo.lock | 1 + README.md | 2 ++ airquamon_domain/src/lib.rs | 1 + esp32c3_nostd/Cargo.toml | 1 + esp32c3_nostd/src/main.rs | 46 ++++++++++++++++++++++++++++++++++--- sensor/src/lib.rs | 2 ++ sensor/src/mock_sensor.rs | 28 ++++++++++++++++++++++ 7 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 sensor/src/mock_sensor.rs diff --git a/Cargo.lock b/Cargo.lock index f453298..a659747 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -480,6 +480,7 @@ name = "esp32c3_nostd" version = "0.1.0" dependencies = [ "airquamon_domain", + "critical-section", "display_themes", "embedded-graphics 0.8.1", "embedded-hal 1.0.0-rc.1", diff --git a/README.md b/README.md index 7e706cf..ce01db6 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ SPI2 can be used on any GPIO pins - https://docs.rs/esp32c3-hal/latest/esp32c3_hal/i2c/index.html - https://www.allaboutcircuits.com/technical-articles/spi-serial-peripheral-interface - https://github.com/waveshareteam/Pico_ePaper_Code +- https://projects.raspberrypi.org/en/projects/button-switch-scratch-pi/1 +- https://microcontrollerslab.com/push-button-esp32-gpio-digital-input/ ## Acknowledgements - hauju for releasing https://github.com/hauju/scd4x-rs diff --git a/airquamon_domain/src/lib.rs b/airquamon_domain/src/lib.rs index fed986c..7744bf8 100644 --- a/airquamon_domain/src/lib.rs +++ b/airquamon_domain/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] +#[derive(Copy, Clone)] pub struct Data { pub co2: u16, pub temperature: f32, diff --git a/esp32c3_nostd/Cargo.toml b/esp32c3_nostd/Cargo.toml index d89c122..537f019 100644 --- a/esp32c3_nostd/Cargo.toml +++ b/esp32c3_nostd/Cargo.toml @@ -10,6 +10,7 @@ airquamon_domain = { path = "../airquamon_domain" } display_themes = { path = "../display_themes" } epd_display = { path = "../epd_display" } sensor = { path = "../sensor" } +critical-section = "1.1.1" esp32c3-hal = { version = "0.13.0", features = ["eh1"] } esp-hal-common = { version = "0.13.1", features = ["esp32c3", "eh1"] } esp-backtrace = { version = "0.8.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] } diff --git a/esp32c3_nostd/src/main.rs b/esp32c3_nostd/src/main.rs index 6a311fe..502be21 100644 --- a/esp32c3_nostd/src/main.rs +++ b/esp32c3_nostd/src/main.rs @@ -1,6 +1,8 @@ #![no_std] #![no_main] +use core::cell::RefCell; +use critical_section::Mutex; use display_themes::Theme2; use epd_display::{Display, DisplayTheme}; use epd_waveshare::{ @@ -9,10 +11,12 @@ use epd_waveshare::{ }; use esp32c3_hal::{ clock::ClockControl, - gpio::IO, + gpio::{Event, Gpio3, Gpio9, Input, PullDown, PullUp, IO}, i2c::I2C, - peripherals::Peripherals, + interrupt, + peripherals::{self, Peripherals}, prelude::*, + riscv, spi::{ master::{Spi, SpiBusController}, SpiMode, @@ -21,7 +25,10 @@ use esp32c3_hal::{ }; use esp_backtrace as _; use log::info; -use sensor::{Scd4xSensor, Sensor}; +use sensor::{MockSensor, Scd4xSensor, Sensor}; + +static BOOT_BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); +static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { @@ -45,6 +52,7 @@ fn main() -> ! { info!("Connecting to sensor"); let mut sensor = Scd4xSensor::new(i2c, delay); + // let mut sensor = MockSensor::new(500, 19f32, 69f32); let mosi = io.pins.gpio4; let sck = io.pins.gpio5; @@ -53,6 +61,21 @@ fn main() -> ! { let rst = io.pins.gpio18.into_push_pull_output(); let busy = io.pins.gpio19.into_pull_down_input(); + let mut boot_button = io.pins.gpio9.into_pull_down_input(); + boot_button.listen(Event::FallingEdge); + + let mut button = io.pins.gpio3.into_pull_up_input(); + button.listen(Event::RisingEdge); + + critical_section::with(|cs| BOOT_BUTTON.borrow_ref_mut(cs).replace(boot_button)); + critical_section::with(|cs| BUTTON.borrow_ref_mut(cs).replace(button)); + + interrupt::enable(peripherals::Interrupt::GPIO, interrupt::Priority::Priority3).unwrap(); + + unsafe { + riscv::interrupt::enable(); + } + let spi_controller = SpiBusController::from_spi(Spi::new_no_cs_no_miso( peripherals.SPI2, sck, @@ -88,3 +111,20 @@ fn main() -> ! { delay.delay_ms(60000u32); } } + +#[interrupt] +fn GPIO() { + critical_section::with(|cs| { + info!("Button was pressed"); + BOOT_BUTTON + .borrow_ref_mut(cs) + .as_mut() + .unwrap() + .clear_interrupt(); + BUTTON + .borrow_ref_mut(cs) + .as_mut() + .unwrap() + .clear_interrupt(); + }); +} diff --git a/sensor/src/lib.rs b/sensor/src/lib.rs index ae7e0a2..032a117 100644 --- a/sensor/src/lib.rs +++ b/sensor/src/lib.rs @@ -2,7 +2,9 @@ use airquamon_domain::Data; +mod mock_sensor; mod scd4x_sensor; +pub use mock_sensor::MockSensor; pub use scd4x_sensor::Scd4xSensor; pub trait Sensor { diff --git a/sensor/src/mock_sensor.rs b/sensor/src/mock_sensor.rs new file mode 100644 index 0000000..e13b9b6 --- /dev/null +++ b/sensor/src/mock_sensor.rs @@ -0,0 +1,28 @@ +use crate::Sensor; +use airquamon_domain::Data; +use embedded_hal::{delay::DelayUs, i2c::I2c}; +use scd4x::{Error, Scd4x}; + +pub struct MockSensor { + data: Data, +} + +impl MockSensor { + pub fn new(co2: u16, temperature: f32, humidity: f32) -> Self { + Self { + data: Data { + co2, + temperature, + humidity, + }, + } + } +} + +impl Sensor for MockSensor { + type Error = (); + + fn measure(&mut self) -> Result { + Ok(self.data) + } +}