Skip to content

Commit

Permalink
add button interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanBall committed Nov 5, 2023
1 parent 75b012e commit 0c3facd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions airquamon_domain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]

#[derive(Copy, Clone)]
pub struct Data {
pub co2: u16,
pub temperature: f32,
Expand Down
1 change: 1 addition & 0 deletions esp32c3_nostd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
46 changes: 43 additions & 3 deletions esp32c3_nostd/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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,
Expand All @@ -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<RefCell<Option<Gpio9<Input<PullDown>>>>> = Mutex::new(RefCell::new(None));
static BUTTON: Mutex<RefCell<Option<Gpio3<Input<PullUp>>>>> = Mutex::new(RefCell::new(None));

#[entry]
fn main() -> ! {
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
});
}
2 changes: 2 additions & 0 deletions sensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions sensor/src/mock_sensor.rs
Original file line number Diff line number Diff line change
@@ -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<Data, Self::Error> {
Ok(self.data)
}
}

0 comments on commit 0c3facd

Please sign in to comment.