-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seeed_MPR121_driver.cpp
272 lines (226 loc) · 8.3 KB
/
Seeed_MPR121_driver.cpp
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
/*
Seeed_MPR121_driver.cpp
Driver for DIGITAL I2C HUMIDITY AND TEMPERATURE SENSOR
Copyright (c) 2018 Seeed Technology Co., Ltd.
Website : www.seeed.cc
Author : downey
Create Time: May 2018
Change Log :
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "Seeed_MPR121_driver.h"
Mpr121::Mpr121(u8 addr) {
_IIC_ADDR = addr;
}
s32 Mpr121::begin() {
s32 ret = 0;
Wire.begin();
ret = select_mode(STOP_MODE);
if (0 != ret) {
return -1;
}
delay(100);
/*FFI-6,CDC-16UA,CDT-0.5US,SFI-4,ESI-4MS*/
/**/
set_globle_param(0x2310);
/*Touch debounce =2*SFI*ESI,Realese debounce=2*SFI*ESI */
set_debounce(0X22);
/*Set touch and realese threshold 0x08.*/
//set_threshold(0x0808);
select_mode(START_PROXIMITY_DISABLE_MODE);
//select_mode(START_PROXIMITY_ENABLE_MODE);
return ret;
}
/** @brief Set sensor mode:STOP,detection,proximity.
@param mode
* */
s32 Mpr121::select_mode(sensor_mode_t mode) {
switch (mode) {
case STOP_MODE:
return sensor_stop();
break;
case START_PROXIMITY_ENABLE_MODE:
return sensor_start_proximity_enable();
break;
case START_PROXIMITY_DISABLE_MODE:
return sensor_start_proximity_disable();
break;
default:
return -1;
break;
}
}
/*debounce_touch=(bit0~bit2)*EFI*SSI debounce_release=(bit4~bit6)*ESI*SFI */
/*Default ESI=16ms,SFI=4,This two parameters is configurable,see set_globle_param()*/
/*ESI--Electrode Sample Interval,SFI--Second Filter Iterations*/
/** @brief Set debounce of touch
@param debounce
* */
void Mpr121::set_debounce(u8 debounce) {
IIC_write_byte(DEBOUNCE_REG_ADDR, debounce);
}
/* 0x5d:
bit0~bit2:ESI
bit3~bit4:SFI
bit5~bit7:CDT
0x5c:
bit0~bit5:CDC
bit6~bit7:FFI
0x5c-low byte,0x5d-high byte
*/
/** @brief set globle parameters.FFI(First Filter Iterations ),CDC(Charge Discharge Current)
@brief CDT(Charge Discharge Time),SFI(Second Filter Iterations),ESI(Electrode Sample Interval)
@param 16bit value
* */
void Mpr121::set_globle_param(u16 value) {
u8 val_l = (u8)value;
u8 val_h = (u8)(value >> 8);
IIC_write_byte(FILTER_AND_GLOBAL_CDC_CFG_ADDR, val_l);
IIC_write_byte(FILTER_AND_GLOBAL_CDT_CFG_ADDR, val_h);
}
/** @brief Set threshold
@brief Touch condition: Baseline - Electrode filtered data > Touch threshold
@brief Release condition: Baseline - Electrode filtered data < Release threshold
@brief It's not very necessary to set this param in simple use case.
@param Threshold for sensor
* */
void Mpr121::set_threshold(u16 threshold) {
u8 thres_touch = (u8)threshold;
u8 thres_realese = (u8)(threshold >> 8);
for (int i = 0; i < CHANNEL_NUM; i++) {
IIC_write_byte(THRESHOLD_REG_START_ADDR + 2 * i, thres_touch);
IIC_write_byte(THRESHOLD_REG_START_ADDR + 2 * i + 1, thres_realese);
}
}
/** @brief Total 12 channels for touch electrode,bit0-bit11 of 8+8 bits data indicate if there is a channel triggered.
@brief Bit0-bit11 correponding to channel 0-11
@return 16 bits indicate data.
* */
u16 Mpr121::check_status_register() {
u16 val = 0;
u8 val_l, val_h;
IIC_read_byte(TOUCH_STATUS_REG_ADDR_L, &val_l);
IIC_read_byte(TOUCH_STATUS_REG_ADDR_H, &val_h);
val = val_h << 8 | val_l;
return val;
}
/** @brief The sensitivity can be set. The higher the value, the higher the sensitivity
@param senvalue.You can assign 0x00~0xFF.
* */
void Mpr121::set_sensitivity(u8 senvalue) {
touch_threshold_max = senvalue;
}
/** @brief When key is pressed,The sensor not just ouput 0/1
@brief The MPR121 provides filtered electrode output data for all 13 channels,
@param elecs_stat.Judge which channel is triggered according to this param
@param elecs_filtered_data.Get corresponding channel data.
* */
void Mpr121::get_filtered_reg_data(u16* elecs_stat, u16* elecs_filtered_data) {
u16 value = 0;
u8 data_l, data_h;
for (int i = 0; i < CHANNEL_NUM; i++) {
if ((*elecs_stat) & (1 << i)) {
IIC_read_byte(FILTERED_DATA_REG_START_ADDR_L + 2 * i, &data_l);
IIC_read_byte(FILTERED_DATA_REG_START_ADDR_L + 2 * i + 1, &data_h);
elecs_filtered_data[i] = (u16)data_h << 8 | data_l;
if (elecs_filtered_data[i] > touch_threshold_max) {
(*elecs_stat) &= ~(1 << i);
}
//Serial.print("press value= 0X");
//Serial.println(elecs_filtered_data[i],HEX);
}
}
}
//use frutits as example
void Mpr121::get_fruits_data(u16* elecs_stat, u16* elecs_filtered_data) {
u8 data_l, data_h;
for (int i = 0; i < CHANNEL_NUM; i++) {
if ((*elecs_stat) & (1 << i)) {
IIC_read_byte(FILTERED_DATA_REG_START_ADDR_L + 2 * i, &data_l);
IIC_read_byte(FILTERED_DATA_REG_START_ADDR_L + 2 * i + 1, &data_h);
elecs_filtered_data[i] = (u16)data_h << 8 | data_l;
if (elecs_filtered_data[i] < 0x40) {
(*elecs_stat) &= ~(1 << i);
}
//Serial.print("press value= 0X");
//Serial.println(elecs_filtered_data[i],HEX);
}
}
}
void Mpr121::get_baseline_data(u16 elecs_stat, u8* base_line_data) {
u8 data = 0;
for (int i = 0; i < CHANNEL_NUM; i++) {
if (elecs_stat & (1 << i)) {
IIC_read_byte(BASELINE_FILTERING_CONTROL_REG_START_ADDR + i, &data);
Serial.print("base line value= 0X");
Serial.println(base_line_data[i], HEX);
}
}
}
/** @brief Set stop mode by set ELEC_CFG_REG_ADDR to 0x0
* */
s32 Mpr121::sensor_stop() {
return IIC_write_byte(ELEC_CFG_REG_ADDR, 0);
return 0;
}
/** @brief Set start mode with proximity enable by set ELEC_CFG_REG_ADDR to 0x0
* */
s32 Mpr121::sensor_start_proximity_enable() {
IIC_write_byte(ELEC_CFG_REG_ADDR, 0x3c);
return 0;
}
/** @brief Set start mode with proximity disable by set ELEC_CFG_REG_ADDR to 0x0
* */
s32 Mpr121::sensor_start_proximity_disable() {
IIC_write_byte(ELEC_CFG_REG_ADDR, 0x3c);
return 0;
}
/*****************************************IIC operation interface!!*****************************************/
/************************************************************************************************************/
s32 Mpr121::IIC_write_byte(u8 reg, u8 byte) {
Wire.beginTransmission(_IIC_ADDR);
Wire.write(reg);
Wire.write(byte);
return Wire.endTransmission();
}
void Mpr121::IIC_write_bytes(u8 reg, u8 bytes[], u32 bytes_len) {
Wire.beginTransmission(_IIC_ADDR);
Wire.write(reg);
for (int i = 0; i < bytes_len; i++) {
Wire.write(bytes[i]);
}
Wire.endTransmission();
}
void Mpr121::IIC_read_byte(u8 reg, u8* byte) {
Wire.beginTransmission(_IIC_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(_IIC_ADDR, (u8)1);
while (1 != Wire.available());
*byte = Wire.read();
}
void Mpr121::IIC_read_bytes(u8 start_reg, u8* bytes, u32 bytes_len) {
Wire.beginTransmission(_IIC_ADDR);
Wire.write(start_reg);
Wire.endTransmission(false);
Wire.requestFrom(_IIC_ADDR, (u8)bytes_len);
while (bytes_len != Wire.available());
for (int i = 0; i < bytes_len; i++) {
bytes[i] = Wire.read();
}
}