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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use std::fmt;
use core::fmt::Debug;
use rppal::gpio::{Gpio, OutputPin, Level};

/// GPIO BCM pin number for the red light.
pub const GPIO_LIGHT_RED: u8 = 6;

/// GPIO BCM pin number for the green light.
pub const GPIO_LIGHT_GREEN: u8 = 19;

/// GPIO BCM pin number for the blue light.
pub const GPIO_LIGHT_BLUE: u8 = 26;

/// Light on the board.
#[derive(Debug)]
pub struct Light {

    /// GPIO pin number using the BCM pin numbering.
    pub bcm_pin: u8,

    /// Output pin to write to GPIO. Optional as not used in simulated mode.
    pin: Option<Box<OutputPin>>,

    /// State of the light: true for on, false for Off
    pub state: bool,

    /// In simulation mode, no interaction with the hardware is done to simplify testability.
    simulation: bool, 

    /// is the setup completed
    is_setup: bool,
}

impl Light {
    
    /// Creates a light for the GPIO number.
    /// # Arguments
    ///
    /// * `bcm_pin` - GPIO pin number using the BCM pin numbering.
    pub fn new(bcm_pin: u8) -> Result<Light, Error>  {     

        Ok(Self {
            bcm_pin,
            pin: None,
            state: false,
            simulation: false,
            is_setup: false,
        })
    }

    /// Initialize driver.
    pub fn setup(&mut self) -> Result <(), Error> {
        if !self.is_setup {

            // Ignore Gpio initialization if in sumulation mode
            if !self.simulation {
                let gpio = Gpio::new()?;
                let output = gpio.get(self.bcm_pin)?.into_output(); 
                self.pin = Some(Box::new(output));
            }

            self.is_setup = true;
        }
        Ok(())
    }

    /// Sets the light state to on.
    pub fn on(&mut self) {
        self.write(true);
    }

    /// Sets the light state to off.
    pub fn off(&mut self) {
        self.write(false);
    }

    /// Toggles the light state between on and off.
    pub fn toggle(&mut self) {
        self.write(!self.state);
    }

    /// Set the light state.
    /// # Arguments
    ///
    /// * `state` - State of the light: true for on, false for Off.
    pub fn write(&mut self, state: bool) {
        self.state = state;

        if !self.is_setup {
            let _result = self.setup();
        }

        // Only perform actual pin write if not in simulation mode
        if !self.simulation {

            let pin = self.pin.as_deref_mut().unwrap();

            if state {
                pin.write(Level::High);
            } else {
                pin.write(Level::Low);
            }

        }
    }
}

/// Set of lights on the board.
pub struct Lights {

    /// Red light.
    pub red : Light,

    /// Green light.
    pub green: Light,

    /// Blue light.
    pub blue: Light,
}

impl Lights {

    /// Creates a the set of Lights.
    pub fn new() -> Result<Lights, Error> {
        Ok(Self {
            red: Light::new(GPIO_LIGHT_RED)?,
            green: Light::new(GPIO_LIGHT_GREEN)?,
            blue: Light::new(GPIO_LIGHT_BLUE)?,
        })
    }

    /// Set the state for all the lights.
    /// # Arguments
    ///
    /// * `state` - State of the lights: true for on, false for Off.
    pub fn all(&mut self, state: bool) {
        self.red.write(state);
        self.green.write(state);
        self.blue.write(state);
    }

    /// Set the state for each light.
    /// # Arguments
    ///
    /// * `r` - State of the red light: true for on, false for Off.
    /// * `g` - State of the green light: true for on, false for Off.
    /// * `b` - State of the blue light: true for on, false for Off.
    pub fn rgb(&mut self, r: bool, g: bool, b: bool) {
        self.red.write(r);
        self.green.write(g);
        self.blue.write(b);
    }

    /// Enbles simulation mode.
    pub fn enable_simulation(&mut self) {
        self.red.simulation = true;
        self.green.simulation = true;
        self.blue.simulation = true;
    }
}

/// Errors that can occur.
#[derive(Debug)]
pub enum Error {

    /// Gpio error.
    Gpio(rppal::gpio::Error),
}

impl std::error::Error for Error {}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &*self {
            Error::Gpio(err) => write!(f, "Gpio error: {}", &err),
        }
    }
}

/// Converts Gpio error
impl From<rppal::gpio::Error> for Error {
    fn from(err: rppal::gpio::Error) -> Error {
        Error::Gpio(err)
    }
}


/// Unit tests
#[cfg(test)]
mod tests {
    use super::*;

    /// Tests the setup of the light.
    #[test]
    fn test_light_setup() -> Result<(), Error> {
        let mut light = Light::new(GPIO_LIGHT_RED)?;
        // enable simulation
        light.simulation = true;

        // Not setup
        assert!(light.is_setup == false);

        // Force setup
        let _result = light.setup();

        assert!(light.is_setup == true);

        Ok(())
    }

    /// Tests turning on a light.
    #[test]
    fn test_light_on() -> Result<(), Error> {
        let mut light = Light::new(GPIO_LIGHT_RED)?;

        // enable simulation
        light.simulation = true;

        // Off by default
        assert!(light.state == false);

        // Turn on
        light.on();
        assert!(light.state == true);

        Ok(())
    }

    /// Tests turning off a light.
    #[test]
    fn test_light_off() -> Result<(), Error> {
        let mut light = Light::new(GPIO_LIGHT_RED)?;

        // enable simulation
        light.simulation = true;

        // Turn on
        light.state = true;

        // Turn off
        light.off();
        assert!(light.state == false);

        Ok(())
    }

    /// Tests toggling a light.
    #[test]
    fn test_light_toggle() -> Result<(), Error> {
        let mut light = Light::new(GPIO_LIGHT_RED)?;

        // enable simulation
        light.simulation = true;

        light.toggle();
        assert!(light.state == true);

        light.toggle();
        assert!(light.state == false);

        Ok(())
    }
    
    /// Tests defining the state of each of the lights.
    #[test]
    fn test_lights_rgb() -> Result<(), Error> {
        let mut lights = Lights::new()?;

        // enable simulation
        lights.enable_simulation();

        // Test all combinations of rgb light states.
        let bool_array: [bool; 2] = [true, false];
        for red_state in &bool_array {
            for green_state in &bool_array {
                for blue_state in &bool_array {

                    // Turn on red and blue and green off and assert the state is correct.
                    lights.rgb(*red_state, *green_state, *blue_state);
                    assert!(lights.red.state == *red_state);
                    assert!(lights.green.state == *green_state);
                    assert!(lights.blue.state == *blue_state);
                }
            }
        }

        Ok(())
    }

    /// Tests turning on or off all the lights.
    #[test]
    fn test_lights_all() -> Result<(), Error> {
        let mut lights = Lights::new()?;

        // Turn on simulation
        lights.enable_simulation();

        // Test turning all lights on and off.
        lights.all(true);
        assert!(lights.red.state == true);
        assert!(lights.green.state == true);
        assert!(lights.blue.state == true);

        lights.all(false);
        assert!(lights.red.state == false);
        assert!(lights.green.state == false);
        assert!(lights.blue.state == false);

        Ok(())
    }

    /// Tests to enable the simulation.
    #[test]
    fn test_lights_enable_simulation() -> Result<(), Error> {
        let mut lights = Lights::new()?;

        // Simulation off by default
        assert!(!lights.red.simulation);
        assert!(!lights.green.simulation);
        assert!(!lights.blue.simulation);

        // Turn on simulation
        lights.enable_simulation();

        assert!(lights.red.simulation);
        assert!(lights.green.simulation);
        assert!(lights.blue.simulation);

        Ok(())
    }
}