embassy/embassy-nrf-examples/src/bin/gpiote_port.rs

69 lines
1.7 KiB
Rust
Raw Normal View History

2020-11-08 19:00:08 +01:00
#![no_std]
#![no_main]
2021-03-17 02:48:16 +01:00
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
2020-11-08 19:00:08 +01:00
#![feature(type_alias_impl_trait)]
#[path = "../example_common.rs"]
mod example_common;
2021-03-20 03:09:42 +01:00
use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull};
2020-11-08 19:00:08 +01:00
use example_common::*;
2021-03-21 21:01:06 +01:00
use gpiote::PortInput;
2020-11-08 19:00:08 +01:00
use core::pin::Pin;
2020-11-08 19:00:08 +01:00
use cortex_m_rt::entry;
2020-12-29 01:53:17 +01:00
use defmt::panic;
2020-11-08 19:00:08 +01:00
use nrf52840_hal::gpio;
use embassy::executor::{task, Executor};
2021-03-02 00:32:23 +01:00
use embassy::traits::gpio::{WaitForHigh, WaitForLow};
2020-11-08 19:00:08 +01:00
use embassy::util::Forever;
2021-03-20 03:09:42 +01:00
use embassy_nrf::gpiote;
2020-12-29 01:53:17 +01:00
use embassy_nrf::interrupt;
2020-11-08 19:00:08 +01:00
2021-03-21 21:01:06 +01:00
async fn button(n: usize, mut pin: PortInput<AnyPin>) {
2020-11-08 19:00:08 +01:00
loop {
Pin::new(&mut pin).wait_for_low().await;
2020-11-08 19:00:08 +01:00
info!("Button {:?} pressed!", n);
Pin::new(&mut pin).wait_for_high().await;
2020-11-08 19:00:08 +01:00
info!("Button {:?} released!", n);
}
}
#[task]
async fn run() {
2021-03-20 03:09:42 +01:00
let p = unsafe { embassy_nrf::peripherals::Peripherals::steal() };
2020-11-08 19:00:08 +01:00
2021-03-20 03:09:42 +01:00
let g = gpiote::initialize(p.gpiote, interrupt::take!(GPIOTE));
2020-11-08 19:00:08 +01:00
let button1 = button(
1,
2021-03-21 21:01:06 +01:00
PortInput::new(g, Input::new(p.p0_11.degrade(), Pull::Up)),
);
let button2 = button(
2,
2021-03-21 21:01:06 +01:00
PortInput::new(g, Input::new(p.p0_12.degrade(), Pull::Up)),
);
let button3 = button(
3,
2021-03-21 21:01:06 +01:00
PortInput::new(g, Input::new(p.p0_24.degrade(), Pull::Up)),
);
let button4 = button(
4,
2021-03-21 21:01:06 +01:00
PortInput::new(g, Input::new(p.p0_25.degrade(), Pull::Up)),
);
2020-11-08 19:00:08 +01:00
futures::join!(button1, button2, button3, button4);
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
2020-11-08 19:00:08 +01:00
}