-
Notifications
You must be signed in to change notification settings - Fork 3
/
pcf8575.rs
174 lines (153 loc) · 5.23 KB
/
pcf8575.rs
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
use core::cell;
use hal::blocking::i2c::Write;
pub use hal::digital::v2::OutputPin;
use super::super::pins::pcf8575;
use super::super::{Error, PinFlag, SlaveAddr};
/// PCF8575 device driver
#[derive(Debug, Default)]
pub struct Pcf8575<I2C> {
/// Device
dev: cell::RefCell<Pcf8575Data<I2C>>,
}
#[derive(Debug, Default)]
pub(crate) struct Pcf8575Data<I2C> {
/// The concrete I²C device implementation.
pub(crate) i2c: I2C,
/// The I²C device address.
pub(crate) address: u8,
/// Last status set to output pins, used to conserve its status while doing a read.
pub(crate) last_set_mask: u16,
}
impl<I2C, E> Pcf8575<I2C>
where
I2C: Write<Error = E>,
{
/// Create new instance of the PCF8575 device
pub fn new(i2c: I2C, address: SlaveAddr) -> Self {
let dev = Pcf8575Data {
i2c,
address: address.addr(0b010_0000),
last_set_mask: 0,
};
Pcf8575 {
dev: cell::RefCell::new(dev),
}
}
/// Destroy driver instance, return I²C bus instance.
pub fn destroy(self) -> I2C {
self.dev.into_inner().i2c
}
/// Set the status of all I/O pins.
pub fn set(&mut self, bits: u16) -> Result<(), Error<E>> {
self.do_on_acquired(|dev| Self::_set(dev, bits))
}
pub(crate) fn _set(mut dev: cell::RefMut<Pcf8575Data<I2C>>, bits: u16) -> Result<(), Error<E>> {
let address = dev.address;
dev.i2c
.write(address, &u16_to_u8_array(bits)[..])
.map_err(Error::I2C)?;
dev.last_set_mask = bits;
Ok(())
}
/// Set the status of all I/O pins repeatedly by looping through each array element.
/// The even elements correspond to the status of P0-P7 and the odd ones P10-P17.
/// The number of elements in the data must be even.
pub fn write_array(&mut self, data: &[u8]) -> Result<(), Error<E>> {
if !data.is_empty() {
if data.len() % 2 != 0 {
return Err(Error::InvalidInputData);
}
self.do_on_acquired(|mut dev| {
let address = dev.address;
dev.i2c.write(address, &data).map_err(Error::I2C)?;
dev.last_set_mask =
(u16::from(data[data.len() - 1]) << 8) | u16::from(data[data.len() - 2]);
Ok(())
})?;
}
Ok(())
}
/// Split device into individual pins
pub fn split<'a>(&'a self) -> pcf8575::Parts<'a, Pcf8575<I2C>, E> {
pcf8575::Parts::new(&self)
}
pub(crate) fn do_on_acquired<R>(
&self,
f: impl FnOnce(cell::RefMut<Pcf8575Data<I2C>>) -> Result<R, Error<E>>,
) -> Result<R, Error<E>> {
let dev = self
.dev
.try_borrow_mut()
.map_err(|_| Error::CouldNotAcquireDevice)?;
f(dev)
}
}
impl<I2C, E> Pcf8575<I2C>
where
I2C: hal::blocking::i2c::Read<Error = E> + Write<Error = E>,
{
/// Get the status of the selected I/O pins.
/// The mask of the pins to be read can be created with a combination of
/// `PinFlag::P0` to `PinFlag::P17`.
pub fn get(&mut self, mask: &PinFlag) -> Result<u16, Error<E>> {
self.do_on_acquired(|dev| Self::_get(dev, mask))
}
pub(crate) fn _get(
mut dev: cell::RefMut<Pcf8575Data<I2C>>,
mask: &PinFlag,
) -> Result<u16, Error<E>> {
let address = dev.address;
let mask = mask.mask | dev.last_set_mask;
// configure selected pins as inputs
dev.i2c
.write(address, &u16_to_u8_array(mask)[..])
.map_err(Error::I2C)?;
let mut bits = [0; 2];
dev.i2c
.read(address, &mut bits)
.map_err(Error::I2C)
.and(Ok(u8_array_to_u16(bits)))
}
/// Get the status of the selected I/O pins repeatedly and put them in the
/// provided array.
/// The mask of the pins to be read can be created with a combination of
/// `PinFlag::P0` to `PinFlag::P17`.
/// The even elements correspond to the status of P0-P7 and the odd ones P10-P17.
/// The number of elements in the data must be even.
pub fn read_array(&mut self, mask: &PinFlag, mut data: &mut [u8]) -> Result<(), Error<E>> {
if !data.is_empty() {
if data.len() % 2 != 0 {
return Err(Error::InvalidInputData);
}
self.do_on_acquired(|mut dev| {
let address = dev.address;
let mask = mask.mask | dev.last_set_mask;
// configure selected pins as inputs
dev.i2c
.write(address, &u16_to_u8_array(mask))
.map_err(Error::I2C)?;
dev.i2c.read(address, &mut data).map_err(Error::I2C)
})?;
}
Ok(())
}
}
fn u16_to_u8_array(input: u16) -> [u8; 2] {
[input as u8, (input >> 8) as u8]
}
fn u8_array_to_u16(input: [u8; 2]) -> u16 {
u16::from(input[0]) | (u16::from(input[1]) << 8)
}
#[cfg(test)]
mod tests {
extern crate embedded_hal_mock as hal;
use super::*;
#[test]
fn can_convert_u16_to_u8_array() {
assert_eq!([0xCD, 0xAB], u16_to_u8_array(0xABCD));
}
#[test]
fn can_convert_u8_array_to_u16() {
assert_eq!(0xABCD, u8_array_to_u16([0xCD, 0xAB]));
}
}