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
extern crate tessel;
use std::io;
use std::thread;
use std::time::Duration;
use std::ops::Range;
pub struct RelayArray<'a> {
pin1: tessel::Pin<'a>,
pin2: tessel::Pin<'a>,
states: [bool; 2],
}
impl<'a> RelayArray<'a> {
pub fn new<'b>(port: tessel::Port) -> RelayArray<'b> {
let (i2c, gpio) = port.i2c();
let (pin1, pin2) = gpio.pin_select((5, 6));
RelayArray {
pin1: pin1,
pin2: pin2,
states: [false, false],
}
}
pub fn connect(&mut self) -> io::Result<()> {
self.pin1.output(false);
self.pin2.output(false);
Ok(())
}
pub fn set_latch(&mut self, index: usize, value: bool) {
if index == 1 {
self.pin1.output(value);
self.states[0] = value;
} else if index == 2 {
self.pin2.output(value);
self.states[1] = value;
} else {
panic!("Invalid relay channel {:?}", index);
}
}
}