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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Keyboard LED management

use embedded_hal::digital::v2::OutputPin;

/// "Disabled" Led implementatin
pub struct Disabled;

/// An active-low LED pin
pub struct ActiveLow<G>(pub G);

/// An active-high LED pin
pub struct ActiveHigh<G>(pub G);

/// An LED
pub trait Led {
    /// Turn the LED on
    fn set_on(&mut self);
    /// Turn the LED off
    fn set_off(&mut self);
}

impl Led for Disabled {
    fn set_on(&mut self) {}
    fn set_off(&mut self) {}
}

impl<G> Led for ActiveHigh<G>
where
    G: OutputPin,
{
    fn set_on(&mut self) {
        if self.0.set_high().is_err() {
            panic!("error setting led high");
        }
    }
    fn set_off(&mut self) {
        if self.0.set_low().is_err() {
            panic!("error setting led low");
        }
    }
}

impl<G> Led for ActiveLow<G>
where
    G: OutputPin,
{
    fn set_on(&mut self) {
        if self.0.set_low().is_err() {
            panic!("error setting led low");
        }
    }
    fn set_off(&mut self) {
        if self.0.set_high().is_err() {
            panic!("error setting led high");
        }
    }
}

/// Structure for LED outputs.
pub struct Leds<N, C, S, P, K> {
    /// NumLock
    pub num: N,
    /// CapsLock
    pub caps: C,
    /// ScrollLock
    pub scroll: S,
    /// ComposeLock
    pub compose: P,
    /// Kana
    pub kana: K,
}

/// Default "no leds" collection
pub type NoLeds = Leds<Disabled, Disabled, Disabled, Disabled, Disabled>;

impl Default for NoLeds {
    fn default() -> Self {
        Leds {
            num: Disabled,
            caps: Disabled,
            scroll: Disabled,
            compose: Disabled,
            kana: Disabled,
        }
    }
}

impl Leds<Disabled, Disabled, Disabled, Disabled, Disabled> {
    /// Create a new empty Leds collection
    pub fn new() -> NoLeds {
        Self::default()
    }
}

impl<N, C, S, P, K> Leds<N, C, S, P, K> {
    /// Add a caps led
    pub fn caps<T>(self, caps: T) -> Leds<N, T, S, P, K> {
        Leds {
            num: self.num,
            caps,
            scroll: self.scroll,
            compose: self.compose,
            kana: self.kana,
        }
    }

    /// Add a num led
    pub fn num<T>(self, num: T) -> Leds<T, C, S, P, K> {
        Leds {
            num,
            caps: self.caps,
            scroll: self.scroll,
            compose: self.compose,
            kana: self.kana,
        }
    }

    /// Add a scroll led
    pub fn scroll<T>(self, scroll: T) -> Leds<N, C, T, P, K> {
        Leds {
            num: self.num,
            caps: self.caps,
            scroll,
            compose: self.compose,
            kana: self.kana,
        }
    }

    /// Add a compose led
    pub fn compose<T>(self, compose: T) -> Leds<N, C, S, T, K> {
        Leds {
            num: self.num,
            caps: self.caps,
            scroll: self.scroll,
            compose,
            kana: self.kana,
        }
    }

    /// Add a kana led
    pub fn kana<T>(self, kana: T) -> Leds<N, C, S, P, T> {
        Leds {
            num: self.num,
            caps: self.caps,
            scroll: self.scroll,
            compose: self.compose,
            kana,
        }
    }
}

impl<N, C, S, P, K> LedReport for Leds<N, C, S, P, K>
where
    C: Led,
    N: Led,
    S: Led,
    P: Led,
    K: Led,
{
    /// Set the Led state from a report byte
    fn from_report_byte(&mut self, report: u8) {
        let leds: &mut [&mut dyn Led] = &mut [
            &mut self.num,
            &mut self.caps,
            &mut self.scroll,
            &mut self.compose,
            &mut self.kana,
        ];
        for (bit, led) in leds.iter_mut().enumerate() {
            if report & (0x01 << bit) != 0 {
                led.set_on();
            } else {
                led.set_off();
            }
        }
    }
}

/// A type that can control LEDs via an output report
pub trait LedReport {
    /// Set the Led state from the report byte
    fn from_report_byte(&mut self, report: u8);
}