1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Interface for LEDs that abstract away polarity and pin.
//!
//!  Author: Philip Levis <pal@cs.stanford.edu>
//!  Date: July 31, 2015
//!

use hil::gpio;

pub trait Led {
    fn init(&mut self);
    fn on(&mut self);
    fn off(&mut self);
    fn toggle(&mut self);
    fn read(&self) -> bool;
}

/// For LEDs in which on is when GPIO is high.
pub struct LedHigh {
    pub pin: &'static mut gpio::Pin,
}

/// For LEDs in which on is when GPIO is low.
pub struct LedLow {
    pub pin: &'static mut gpio::Pin,
}

impl LedHigh {
    pub fn new(p: &'static mut gpio::Pin) -> LedHigh {
        LedHigh { pin: p }
    }
}

impl LedLow {
    pub fn new(p: &'static mut gpio::Pin) -> LedLow {
        LedLow { pin: p }
    }
}

impl Led for LedHigh {
    fn init(&mut self) {
        self.pin.make_output();
    }

    fn on(&mut self) {
        self.pin.set();
    }

    fn off(&mut self) {
        self.pin.clear();
    }

    fn toggle(&mut self) {
        self.pin.toggle();
    }

    fn read(&self) -> bool {
        self.pin.read()
    }
}

impl Led for LedLow {
    fn init(&mut self) {
        self.pin.make_output();
    }

    fn on(&mut self) {
        self.pin.clear();
    }

    fn off(&mut self) {
        self.pin.set();
    }

    fn toggle(&mut self) {
        self.pin.toggle();
    }

    fn read(&self) -> bool {
        !self.pin.read()
    }
}