Skip to content

Instantly share code, notes, and snippets.

@therealprof
Created January 16, 2019 19:54
Show Gist options
  • Save therealprof/ebdb24e5b90262e5d8d7e062b4a49b34 to your computer and use it in GitHub Desktop.
Save therealprof/ebdb24e5b90262e5d8d7e062b4a49b34 to your computer and use it in GitHub Desktop.
//! On-board user LEDs
use crate::hal::prelude::*;
use crate::hal::gpio::gpioc::{self, *};
use crate::hal::gpio::{Output, Pin, PushPull};
/// Top LED (orange)
pub type LD3 = PC8<Output<PushPull>>;
/// Left LED (green)
pub type LD4 = PC9<Output<PushPull>>;
/// Right LED (red)
pub type LD5 = PC6<Output<PushPull>>;
/// Bottom LED (blue)
pub type LD6 = PC7<Output<PushPull>>;
/// User LED colors
pub enum LedColor {
/// Green LED / LD4
Green,
/// Orange LED / LD3
Orange,
/// Red LED / LD5
Red,
/// Blue LED / LD6
Blue,
}
// Array of the on-board user LEDs
pub struct Leds {
leds: [Led; 4],
}
#[macro_export]
macro_rules! leds {
($($gpio:ident),+) => {
$(
cortex_m::interrupt::free(|cs| {
let top = $gpio.pc8.into_push_pull_output(cs);
let left = $gpio.pc9.into_push_pull_output(cs);
let right = $gpio.pc6.into_push_pull_output(cs);
let bottom = $gpio.pc7.into_push_pull_output(cs);
Leds {
leds: [top.into(), left.into(), right.into(), bottom.into()],
}
})
)+
}
}
impl Leds {
pub fn new(gpioc: gpioc::Parts) -> Self {
cortex_m::interrupt::free(|cs| {
let top = gpioc.pc8.into_push_pull_output(cs);
let left = gpioc.pc9.into_push_pull_output(cs);
let right = gpioc.pc6.into_push_pull_output(cs);
let bottom = gpioc.pc7.into_push_pull_output(cs);
Leds {
leds: [top.into(), left.into(), right.into(), bottom.into()],
}
})
}
}
impl core::ops::Deref for Leds {
type Target = [Led];
fn deref(&self) -> &[Led] {
&self.leds
}
}
impl core::ops::DerefMut for Leds {
fn deref_mut(&mut self) -> &mut [Led] {
&mut self.leds
}
}
impl core::ops::Index<usize> for Leds {
type Output = Led;
fn index(&self, i: usize) -> &Led {
&self.leds[i]
}
}
impl core::ops::Index<LedColor> for Leds {
type Output = Led;
fn index(&self, c: LedColor) -> &Led {
&self.leds[c as usize]
}
}
impl core::ops::IndexMut<usize> for Leds {
fn index_mut(&mut self, i: usize) -> &mut Led {
&mut self.leds[i]
}
}
impl core::ops::IndexMut<LedColor> for Leds {
fn index_mut(&mut self, c: LedColor) -> &mut Led {
&mut self.leds[c as usize]
}
}
/// One of the on-board user LEDs
pub struct Led {
pin: Pin<Output<PushPull>>,
}
macro_rules! ctor {
($($ldx:ident),+) => {
$(
impl Into<Led> for $ldx {
fn into(self) -> Led {
Led {
pin: self.downgrade(),
}
}
}
)+
}
}
ctor!(LD3, LD4, LD5, LD6);
impl Led {
/// Turns the LED off
pub fn off(&mut self) {
self.pin.set_low();
}
/// Turns the LED on
pub fn on(&mut self) {
self.pin.set_high();
}
/// Toggles the LED
pub fn toggle(&mut self) {
self.pin.toggle();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment