2019-03-17 14:19:52 +01:00
|
|
|
//! Module for manipulating the LED ring.
|
|
|
|
|
2019-03-07 23:03:41 +01:00
|
|
|
use hal::gpio::{Output, PushPull};
|
|
|
|
use hal::prelude::*;
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Type alias for a generic LED output.
|
2019-03-07 23:03:41 +01:00
|
|
|
pub type Led = hal::gpio::gpiod::PD<Output<PushPull>>;
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// The cycle direction of the LED ring
|
|
|
|
///
|
|
|
|
/// The direction can be interpreted as such when the mini-USB port of the board is being held
|
|
|
|
/// down.
|
2019-03-15 22:08:12 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub enum Direction {
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Cycle clockwise.
|
2019-03-07 23:03:41 +01:00
|
|
|
Clockwise,
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Cycle counter-clockwise.
|
2019-03-07 23:03:41 +01:00
|
|
|
CounterClockwise,
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:08:12 +01:00
|
|
|
impl Direction {
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Returns the flipped/reversed direction.
|
2019-03-15 22:08:12 +01:00
|
|
|
fn flip(&self) -> Direction {
|
2019-03-07 23:03:41 +01:00
|
|
|
match self {
|
2019-03-15 22:08:12 +01:00
|
|
|
Direction::Clockwise => Direction::CounterClockwise,
|
|
|
|
Direction::CounterClockwise => Direction::Clockwise,
|
2019-03-07 23:03:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// The mode the LED ring is in.
|
2019-03-15 22:08:12 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub enum Mode {
|
2019-03-17 14:19:52 +01:00
|
|
|
/// All LEDs are off.
|
2019-03-15 22:08:12 +01:00
|
|
|
Off,
|
2019-03-17 14:19:52 +01:00
|
|
|
/// The LEDs are cycling (two on at any time) following some direction.
|
2019-03-15 22:08:12 +01:00
|
|
|
Cycle,
|
2019-03-17 14:19:52 +01:00
|
|
|
/// The LEDs follow the accelerometer (shows which side of the board is pointing down).
|
2019-03-17 13:36:52 +01:00
|
|
|
Accelerometer,
|
2019-03-15 22:08:12 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// The LED ring.
|
|
|
|
///
|
|
|
|
/// The ring on this board is comprised of four LEDs. This struct provides methods
|
|
|
|
/// for animating them.
|
2019-03-15 22:08:12 +01:00
|
|
|
pub struct LedRing {
|
2019-03-17 14:19:52 +01:00
|
|
|
/// The current cycle direction.
|
|
|
|
direction: Direction,
|
|
|
|
/// The current mode.
|
|
|
|
mode: Mode,
|
|
|
|
/// The index of the current LED being lit.
|
|
|
|
index: usize,
|
|
|
|
/// The LED outputs being used to comprise the LED ring.
|
|
|
|
leds: [crate::Led; 4],
|
2019-03-07 23:03:41 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:08:12 +01:00
|
|
|
impl LedRing {
|
2019-03-17 14:19:52 +01:00
|
|
|
/// The number of cycles between LED ring updates (used by tasks).
|
2019-03-07 23:03:41 +01:00
|
|
|
pub const PERIOD: u32 = 8_000_000;
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Sets up the LED ring using using four LED GPIO outputs.
|
|
|
|
pub fn from(leds: [Led; 4]) -> LedRing {
|
2019-03-15 22:08:12 +01:00
|
|
|
LedRing {
|
|
|
|
direction: Direction::Clockwise,
|
|
|
|
mode: Mode::Cycle,
|
2019-03-07 23:03:41 +01:00
|
|
|
index: 0,
|
|
|
|
leds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Enables cycle mode.
|
2019-03-15 22:08:12 +01:00
|
|
|
pub fn enable_cycle(&mut self) {
|
|
|
|
self.mode = Mode::Cycle;
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Enables accelerometer mode.
|
2019-03-15 22:22:20 +01:00
|
|
|
pub fn enable_accel(&mut self) {
|
|
|
|
self.mode = Mode::Accelerometer;
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Disables either cycle or accelerometer mode.
|
2019-03-07 23:03:41 +01:00
|
|
|
pub fn disable(&mut self) {
|
2019-03-15 22:08:12 +01:00
|
|
|
self.mode = Mode::Off;
|
2019-03-07 23:03:41 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Returns whether the LED ring is in cycle mode.
|
2019-03-15 22:08:12 +01:00
|
|
|
pub fn is_mode_cycle(&self) -> bool {
|
|
|
|
self.mode == Mode::Cycle
|
2019-03-07 23:03:41 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Returns whether the LED ring is in acceleromter mode.
|
2019-03-15 22:22:20 +01:00
|
|
|
pub fn is_mode_accel(&self) -> bool {
|
|
|
|
self.mode == Mode::Accelerometer
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Reverses the cycle direction.
|
|
|
|
///
|
|
|
|
/// This will have no immediately visible effect if the LED ring is not in cycle mode
|
|
|
|
/// but it will be used when the cycle mode is enabled again.
|
2019-03-07 23:03:41 +01:00
|
|
|
pub fn reverse(&mut self) {
|
|
|
|
self.direction = self.direction.flip();
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Advances the cycling one step.
|
|
|
|
///
|
|
|
|
/// This will have have directly visible effect regardless of the mode the
|
|
|
|
/// LED ring is in and override what is shown at that moment.
|
2019-03-07 23:03:41 +01:00
|
|
|
pub fn advance(&mut self) {
|
|
|
|
let num_leds = self.leds.len();
|
|
|
|
|
|
|
|
self.leds[self.index].set_high();
|
|
|
|
self.leds[(self.index + 2) % num_leds].set_low();
|
|
|
|
|
|
|
|
self.index = match self.direction {
|
2019-03-15 22:08:12 +01:00
|
|
|
Direction::Clockwise => (self.index + 1) % num_leds,
|
|
|
|
Direction::CounterClockwise => (self.index + 3) % num_leds,
|
2019-03-07 23:03:41 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Turns all LEDs on.
|
|
|
|
///
|
|
|
|
/// This is done immediately, regardless of the current mode.
|
2019-03-07 23:03:41 +01:00
|
|
|
pub fn all_on(&mut self) {
|
|
|
|
for led in self.leds.iter_mut() {
|
|
|
|
led.set_high();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Turns all LEDs off.
|
|
|
|
///
|
|
|
|
/// This is done immediately, regardless of the current mode.
|
2019-03-07 23:03:41 +01:00
|
|
|
pub fn all_off(&mut self) {
|
|
|
|
for led in self.leds.iter_mut() {
|
|
|
|
led.set_low();
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 22:22:20 +01:00
|
|
|
|
2019-03-17 14:19:52 +01:00
|
|
|
/// Turns on specific LEDs based on the direction array.
|
|
|
|
///
|
|
|
|
/// When looking with the mini-USB port of the board held down (south), the directions of
|
|
|
|
/// the array can be interpreted as: `[east, south, west, north]`.
|
2019-03-15 22:22:20 +01:00
|
|
|
pub fn set_directions(&mut self, directions: [bool; 4]) {
|
2019-03-17 14:19:52 +01:00
|
|
|
for (led, on_off) in self.leds.iter_mut().zip(directions.iter()) {
|
2019-03-15 22:22:20 +01:00
|
|
|
if *on_off {
|
|
|
|
led.set_high();
|
|
|
|
} else {
|
|
|
|
led.set_low();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 23:03:41 +01:00
|
|
|
}
|