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
use i2cdev::{
core::*,
linux::{LinuxI2CDevice, LinuxI2CError},
};
pub struct Mpr121 {
dev: LinuxI2CDevice,
}
pub type Mpr121Error = LinuxI2CError;
#[derive(Debug)]
pub struct Mpr121TouchStatus {
status: u16,
}
pub struct Mpr121TouchStatusIterator<'a> {
status: &'a Mpr121TouchStatus,
count: u8,
}
pub const MPR121_I2CADDR_DEFAULT: u16 = 0x5A;
pub const MPR121_TOUCH_THRESHOLD_DEFAULT: u8 = 12;
pub const MPR121_RELEASE_THRESHOLD_DEFAULT: u8 = 6;
impl Mpr121 {
const REG_TOUCHSTATUS_L: u8 = 0x00;
const REG_MHDR: u8 = 0x2B;
const REG_NHDR: u8 = 0x2C;
const REG_NCLR: u8 = 0x2D;
const REG_FDLR: u8 = 0x2E;
const REG_MHDF: u8 = 0x2F;
const REG_NHDF: u8 = 0x30;
const REG_NCLF: u8 = 0x31;
const REG_FDLF: u8 = 0x32;
const REG_NHDT: u8 = 0x33;
const REG_NCLT: u8 = 0x34;
const REG_FDLT: u8 = 0x35;
const REG_TOUCHTH_0: u8 = 0x41;
const REG_RELEASETH_0: u8 = 0x42;
const REG_DEBOUNCE: u8 = 0x5B;
const REG_CONFIG1: u8 = 0x5C;
const REG_CONFIG2: u8 = 0x5D;
const REG_ECR: u8 = 0x5E;
const REG_SOFTRESET: u8 = 0x80;
pub fn new_default(device_id: u8) -> Result<Self, Mpr121Error> {
Mpr121::new(device_id, MPR121_I2CADDR_DEFAULT)
}
pub fn new(device_id: u8, slave_addr: u16) -> Result<Self, Mpr121Error> {
let dev = LinuxI2CDevice::new(format!("/dev/i2c-{}", device_id), slave_addr)?;
Ok(Mpr121 { dev })
}
pub fn reset(&mut self) -> Result<(), Mpr121Error> {
self.reset_with_thresholds(
MPR121_TOUCH_THRESHOLD_DEFAULT,
MPR121_RELEASE_THRESHOLD_DEFAULT,
)
}
pub fn reset_with_thresholds(&mut self, touch: u8, release: u8) -> Result<(), Mpr121Error> {
self.dev
.smbus_write_byte_data(Mpr121::REG_SOFTRESET, 0x63)?;
std::thread::sleep(std::time::Duration::from_millis(1));
self.dev.smbus_write_byte_data(Mpr121::REG_ECR, 0x00)?;
if self.dev.smbus_read_byte_data(Mpr121::REG_CONFIG2)? != 0x24 {
panic!("Failed to find MPR121 in expected config state!");
}
for i in 0..12 {
self.dev
.smbus_write_byte_data(Mpr121::REG_TOUCHTH_0 + 2 * i, touch)?;
self.dev
.smbus_write_byte_data(Mpr121::REG_RELEASETH_0 + 2 * i, release)?;
}
self.dev.smbus_write_byte_data(Mpr121::REG_MHDR, 0x01)?;
self.dev.smbus_write_byte_data(Mpr121::REG_NHDR, 0x01)?;
self.dev.smbus_write_byte_data(Mpr121::REG_NCLR, 0x0E)?;
self.dev.smbus_write_byte_data(Mpr121::REG_FDLR, 0x00)?;
self.dev.smbus_write_byte_data(Mpr121::REG_MHDF, 0x01)?;
self.dev.smbus_write_byte_data(Mpr121::REG_NHDF, 0x05)?;
self.dev.smbus_write_byte_data(Mpr121::REG_NCLF, 0x01)?;
self.dev.smbus_write_byte_data(Mpr121::REG_FDLF, 0x00)?;
self.dev.smbus_write_byte_data(Mpr121::REG_NHDT, 0x00)?;
self.dev.smbus_write_byte_data(Mpr121::REG_NCLT, 0x00)?;
self.dev.smbus_write_byte_data(Mpr121::REG_FDLT, 0x00)?;
self.dev.smbus_write_byte_data(Mpr121::REG_DEBOUNCE, 0)?;
self.dev.smbus_write_byte_data(Mpr121::REG_CONFIG1, 0x10)?;
self.dev.smbus_write_byte_data(Mpr121::REG_CONFIG2, 0x20)?;
self.dev.smbus_write_byte_data(Mpr121::REG_ECR, 0x8F)?;
Ok(())
}
pub fn touch_status(&mut self) -> Result<Mpr121TouchStatus, Mpr121Error> {
let status = self.dev.smbus_read_word_data(Mpr121::REG_TOUCHSTATUS_L)?;
Ok(Mpr121TouchStatus::new(status))
}
}
impl Mpr121TouchStatus {
fn new(touch_status: u16) -> Self {
Self {
status: touch_status,
}
}
pub fn touched(&self, item: u8) -> bool {
if item <= Mpr121TouchStatus::last() {
return self.status >> item & 0x1 != 0;
}
false
}
pub fn was_touched(&self) -> bool {
self.status > 0
}
pub fn first() -> u8 {
0
}
pub fn last() -> u8 {
11
}
pub fn iter(&self) -> Mpr121TouchStatusIterator {
Mpr121TouchStatusIterator::new(self)
}
}
impl std::fmt::Display for Mpr121TouchStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Touch status: [")?;
for i in Mpr121TouchStatus::first()..=Mpr121TouchStatus::last() {
if i > 0 {
write!(f, ", ")?;
}
write!(
f,
"{} is {}",
i,
if (self.status >> i) & 0x1 != 0 {
"on"
} else {
"off"
}
)?;
}
write!(f, "]")
}
}
impl<'a> Mpr121TouchStatusIterator<'a> {
fn new(status: &'a Mpr121TouchStatus) -> Self {
Self { status, count: 0 }
}
}
impl<'a> Iterator for Mpr121TouchStatusIterator<'a> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
self.count += 1;
if self.count <= 12 {
Some(self.status.touched(self.count - 1))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn touch_status() {
assert_eq!(Mpr121TouchStatus::first(), 0);
assert_eq!(Mpr121TouchStatus::last(), 11);
{
let ts = Mpr121TouchStatus::new(0b101010101010);
let mut tsi = ts.iter();
for i in Mpr121TouchStatus::first()..=Mpr121TouchStatus::last() {
let v = tsi.next();
assert!(v.is_some());
let v = v.unwrap();
if i % 2 == 0 {
assert!(!ts.touched(i));
assert!(!v);
} else {
assert!(ts.touched(i));
assert!(v);
}
}
assert!(tsi.next().is_none());
}
{
let ts = Mpr121TouchStatus::new(0b010101010101);
let mut tsi = ts.iter();
for i in Mpr121TouchStatus::first()..=Mpr121TouchStatus::last() {
let v = tsi.next();
assert!(v.is_some());
let v = v.unwrap();
if i % 2 == 1 {
assert!(!ts.touched(i));
assert!(!v);
} else {
assert!(ts.touched(i));
assert!(v);
}
}
assert!(tsi.next().is_none());
}
}
}