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
#![no_std]
#![allow(dead_code)]
extern crate embedded_hal as hal;
use hal::blocking::i2c;
pub struct At42qt2120<I2C> {
i2c: I2C,
address: u8,
}
pub enum Error<E> {
I2c(E),
WrongDeviceID,
InvalidThreshold,
}
#[allow(dead_code)]
#[allow(non_camel_case_types)]
enum Register {
CHIP_ID = 0x00,
STATUS = 0x02,
KEY_STATUS1 = 0x03,
KEY_STATUS2 = 0x04,
SLIDER_POSITION = 0x05,
SLIDER_OPTIONS = 0x0E,
KEY_THRESHOLD_START = 0x10,
KEY_CONTROL_START = 0x1C,
KEY_SIGNAL_START = 0x34,
}
impl<I2C, E> At42qt2120<I2C>
where
I2C: i2c::WriteRead<Error = E> + i2c::Write<Error = E>,
{
pub fn new(i2c: I2C) -> Self{
let address: u8 = 0x1C;
let at42qt2120 = At42qt2120 {
i2c: i2c,
address: address,
};
at42qt2120
}
pub fn setup(&mut self) -> Result<(), Error<E>> {
if (self.read_register(Register::CHIP_ID)?) != 0x3E {
return Err(Error::WrongDeviceID);
}
Ok(())
}
pub fn setup_slider(&mut self, slider: bool, enabled: bool) -> Result<(), Error<E>> {
let mut settings: u8 = 0;
if slider {
settings += 1 << 6;
}
if enabled {
settings += 1 << 7;
}
self.write_register(Register::SLIDER_OPTIONS, settings)?;
Ok(())
}
pub fn setup_key(&mut self, key: u8, threshold: u8, enabled: bool) -> Result<(), Error<E>> {
let mut command : [u8;2] = [0;2];
if threshold == 0 {
return Err(Error::InvalidThreshold);
}
else {
command[0] = Register::KEY_THRESHOLD_START as u8 + key;
command[1] = threshold;
self.i2c.write(self.address, &command).map_err(Error::I2c)?;
}
let mut settings: u8 = 0;
if enabled {
settings += 1 << 0;
}
command[0] = Register::KEY_CONTROL_START as u8 + key;
command[1] = settings;
self.i2c.write(self.address, &command).map_err(Error::I2c)?;
Ok(())
}
pub fn keys_pressed(&mut self) -> Result<bool, Error<E>> {
let status = self.read_register(Register::STATUS)?;
if status & (1 << 0) == (1 << 0) {
Ok(true)
}
else {
Ok(false)
}
}
pub fn slider_pressed(&mut self) -> Result<bool, Error<E>> {
let status = self.read_register(Register::STATUS)?;
if status & (1 << 1) == (1 << 1) {
Ok(true)
}
else {
Ok(false)
}
}
pub fn read_status(&mut self) -> Result<u8, Error<E>> {
self.read_register(Register::STATUS)
}
pub fn read_slider(&mut self,) -> Result<u8, Error<E>> {
self.read_register(Register::SLIDER_POSITION)
}
pub fn read_key_value(&mut self, key: u8,) -> Result<u16, E> {
let command : [u8;1] = [((Register::KEY_SIGNAL_START as u8) + key*2) ;1];
let mut buffer : [u8;1] = [0;1];
self.i2c.write_read(self.address, &command, &mut buffer)?;
let mut result:u16 = (buffer[0] as u16) << 8;
let command : [u8;1] = [((Register::KEY_SIGNAL_START as u8) + key*2) + 1 ;1];
self.i2c.write_read(self.address, &command, &mut buffer)?;
result += buffer[0] as u16;
Ok(result)
}
pub fn read_keys(&mut self,) -> Result<u16, Error<E>> {
let mut result:u16 = (self.read_register(Register::KEY_STATUS2)? as u16) << 8;
result += self.read_register(Register::KEY_STATUS1)? as u16;
Ok(result)
}
pub fn read_key(&mut self, key: u8,) -> Result<bool, Error<E>> {
let result = self.read_keys()?;
Ok(0 != (result & (1 << key)))
}
fn read_register(&mut self, register: Register) -> Result<u8, Error<E>> {
let command : [u8;1] = [register as u8;1];
let mut buffer : [u8;1] = [0;1];
self.i2c.write_read(self.address, &command, &mut buffer).map_err(Error::I2c)?;
let result = buffer[0];
Ok(result)
}
fn write_register(&mut self, register: Register, data: u8) -> Result<(), Error<E>> {
let mut command : [u8;2] = [0;2];
command[0] = register as u8;
command[1] = data;
self.i2c.write(self.address, &command).map_err(Error::I2c)?;
Ok(())
}
}