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

68 lines
1.7 KiB
Rust
Raw Normal View History

2020-11-08 18:00:08 +00:00
#![no_std]
#![no_main]
2021-03-17 01:48:16 +00:00
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
2020-11-08 18:00:08 +00:00
#![feature(type_alias_impl_trait)]
2021-03-27 02:12:58 +00:00
#![allow(incomplete_features)]
2020-11-08 18:00:08 +00:00
#[path = "../example_common.rs"]
mod example_common;
use core::pin::Pin;
2020-11-08 18:00:08 +00:00
use cortex_m_rt::entry;
2020-12-29 00:53:17 +00:00
use defmt::panic;
2020-11-08 18:00:08 +00:00
use embassy::executor::{task, Executor};
2021-03-01 23:32:23 +00:00
use embassy::traits::gpio::{WaitForHigh, WaitForLow};
2020-11-08 18:00:08 +00:00
use embassy::util::Forever;
2021-03-21 20:03:02 +00:00
use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull};
use embassy_nrf::gpiote::{self, PortInput};
2020-12-29 00:53:17 +00:00
use embassy_nrf::interrupt;
2021-03-21 20:58:59 +00:00
use embassy_nrf::Peripherals;
use example_common::*;
2020-11-08 18:00:08 +00:00
2021-03-24 17:31:11 +00:00
async fn button(n: usize, mut pin: PortInput<'static, AnyPin>) {
2020-11-08 18:00:08 +00:00
loop {
Pin::new(&mut pin).wait_for_low().await;
2020-11-08 18:00:08 +00:00
info!("Button {:?} pressed!", n);
Pin::new(&mut pin).wait_for_high().await;
2020-11-08 18:00:08 +00:00
info!("Button {:?} released!", n);
}
}
#[task]
async fn run() {
2021-03-21 20:58:59 +00:00
let p = Peripherals::take().unwrap();
2020-11-08 18:00:08 +00:00
2021-03-27 02:12:58 +00:00
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
2020-11-08 18:00:08 +00:00
let button1 = button(
1,
2021-03-27 02:12:58 +00:00
PortInput::new(g, Input::new(p.P0_11.degrade(), Pull::Up)),
);
let button2 = button(
2,
2021-03-27 02:12:58 +00:00
PortInput::new(g, Input::new(p.P0_12.degrade(), Pull::Up)),
);
let button3 = button(
3,
2021-03-27 02:12:58 +00:00
PortInput::new(g, Input::new(p.P0_24.degrade(), Pull::Up)),
);
let button4 = button(
4,
2021-03-27 02:12:58 +00:00
PortInput::new(g, Input::new(p.P0_25.degrade(), Pull::Up)),
);
2020-11-08 18:00:08 +00: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 18:00:08 +00:00
}