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

95 lines
2.0 KiB
Rust
Raw Normal View History

2020-09-23 00:32:49 +02: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-09-23 00:32:49 +02:00
#![feature(type_alias_impl_trait)]
2021-03-27 03:12:58 +01:00
#![allow(incomplete_features)]
2020-09-23 00:32:49 +02:00
#[path = "../example_common.rs"]
mod example_common;
use example_common::*;
use cortex_m_rt::entry;
2020-12-29 01:53:17 +01:00
use defmt::panic;
2020-09-23 00:32:49 +02:00
use embassy::executor::{task, Executor};
2020-10-31 22:37:24 +01:00
use embassy::util::Forever;
use embassy_nrf::gpio::{Input, Pull};
use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity};
use embassy_nrf::{interrupt, Peripherals};
2020-09-24 22:04:45 +02:00
#[task]
2020-09-23 00:32:49 +02:00
async fn run() {
let p = Peripherals::take().unwrap();
let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
2020-09-23 00:32:49 +02:00
info!("Starting!");
let ch1 = InputChannel::new(
g,
p.GPIOTE_CH0,
Input::new(p.P0_11, Pull::Up),
InputChannelPolarity::HiToLo,
);
let ch2 = InputChannel::new(
g,
p.GPIOTE_CH1,
Input::new(p.P0_12, Pull::Up),
InputChannelPolarity::LoToHi,
);
let ch3 = InputChannel::new(
g,
p.GPIOTE_CH2,
Input::new(p.P0_24, Pull::Up),
InputChannelPolarity::Toggle,
);
let ch4 = InputChannel::new(
g,
p.GPIOTE_CH3,
Input::new(p.P0_25, Pull::Up),
InputChannelPolarity::Toggle,
);
2020-09-23 00:32:49 +02:00
let button1 = async {
2020-09-23 00:32:49 +02:00
loop {
ch1.wait().await;
2020-09-23 00:32:49 +02:00
info!("Button 1 pressed")
}
};
let button2 = async {
loop {
ch2.wait().await;
2020-09-23 00:32:49 +02:00
info!("Button 2 released")
}
};
let button3 = async {
loop {
ch3.wait().await;
2020-09-23 00:32:49 +02:00
info!("Button 3 toggled")
}
};
let button4 = async {
loop {
ch4.wait().await;
2020-09-23 00:32:49 +02:00
info!("Button 4 toggled")
}
};
futures::join!(button1, button2, button3, button4);
}
2020-10-31 22:37:24 +01:00
static EXECUTOR: Forever<Executor> = Forever::new();
2020-09-23 00:32:49 +02:00
#[entry]
fn main() -> ! {
info!("Hello World!");
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
2020-09-23 00:32:49 +02:00
}