Add an interrupt to the user button; reverse LED cycle direction
This commit is contained in:
parent
283655e30e
commit
d0216915c2
44
src/main.rs
44
src/main.rs
|
@ -6,7 +6,7 @@
|
||||||
extern crate panic_semihosting;
|
extern crate panic_semihosting;
|
||||||
|
|
||||||
//use cortex_m_semihosting::hprintln;
|
//use cortex_m_semihosting::hprintln;
|
||||||
use hal::gpio::{ExtiPin, Output, PushPull};
|
use hal::gpio::{Edge, ExtiPin, Floating, Input, Output, PushPull};
|
||||||
use hal::prelude::*;
|
use hal::prelude::*;
|
||||||
use rtfm::app;
|
use rtfm::app;
|
||||||
|
|
||||||
|
@ -14,10 +14,18 @@ type Led = hal::gpio::gpiod::PD<Output<PushPull>>;
|
||||||
|
|
||||||
const PERIOD: u32 = 8_000_000;
|
const PERIOD: u32 = 8_000_000;
|
||||||
|
|
||||||
|
pub enum LedDirection {
|
||||||
|
Clockwise,
|
||||||
|
CounterClockwise,
|
||||||
|
}
|
||||||
|
|
||||||
#[app(device = hal::stm32)]
|
#[app(device = hal::stm32)]
|
||||||
const APP: () = {
|
const APP: () = {
|
||||||
static mut index: usize = ();
|
static mut button: hal::gpio::gpioa::PA0<Input<Floating>> = ();
|
||||||
static mut leds: [Led; 4] = ();
|
static mut leds: [Led; 4] = ();
|
||||||
|
static mut led_cycle_direction: LedDirection = LedDirection::Clockwise;
|
||||||
|
static mut led_index: usize = 0;
|
||||||
|
static mut exti: hal::stm32::EXTI = ();
|
||||||
|
|
||||||
#[init(spawn = [switch_leds])]
|
#[init(spawn = [switch_leds])]
|
||||||
fn init() -> init::LateResources {
|
fn init() -> init::LateResources {
|
||||||
|
@ -31,21 +39,41 @@ const APP: () = {
|
||||||
];
|
];
|
||||||
spawn.switch_leds().unwrap();
|
spawn.switch_leds().unwrap();
|
||||||
|
|
||||||
|
// Set up the EXTI0 interrup for the user button.
|
||||||
|
let mut exti = device.EXTI;
|
||||||
|
let gpioa = device.GPIOA.split();
|
||||||
|
let mut button = gpioa.pa0.into_floating_input();
|
||||||
|
button.enable_interrupt(&mut exti);
|
||||||
|
button.trigger_on_edge(&mut exti, Edge::RISING);
|
||||||
|
|
||||||
init::LateResources { index: 0, leds }
|
init::LateResources { button, exti, leds }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(schedule = [switch_leds], resources = [index, leds])]
|
#[task(schedule = [switch_leds],
|
||||||
|
resources = [led_index, led_cycle_direction, leds])]
|
||||||
fn switch_leds() {
|
fn switch_leds() {
|
||||||
let index = *resources.index;
|
let led_index = *resources.led_index;
|
||||||
let num_leds = resources.leds.len();
|
let num_leds = resources.leds.len();
|
||||||
resources.leds[index].set_high();
|
|
||||||
resources.leds[(index + 2) % num_leds].set_low();
|
resources.leds[led_index].set_high();
|
||||||
*resources.index = (index + 1) % 4;
|
resources.leds[(led_index + 2) % num_leds].set_low();
|
||||||
|
*resources.led_index = match *resources.led_cycle_direction {
|
||||||
|
LedDirection::Clockwise => (led_index + 1) % num_leds,
|
||||||
|
LedDirection::CounterClockwise => (led_index + 3) % num_leds,
|
||||||
|
};
|
||||||
|
|
||||||
schedule.switch_leds(scheduled + PERIOD.cycles()).unwrap();
|
schedule.switch_leds(scheduled + PERIOD.cycles()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[interrupt(binds = EXTI0, resources = [button, exti, led_cycle_direction])]
|
||||||
|
fn button_pressed() {
|
||||||
|
*resources.led_cycle_direction = match *resources.led_cycle_direction {
|
||||||
|
LedDirection::Clockwise => LedDirection::CounterClockwise,
|
||||||
|
LedDirection::CounterClockwise => LedDirection::Clockwise,
|
||||||
|
};
|
||||||
|
resources.button.clear_interrupt_pending_bit(resources.exti);
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn UART4();
|
fn UART4();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue