-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpr121.cpp
221 lines (165 loc) · 5.55 KB
/
mpr121.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
/*
Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
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 <mbed.h>
#include <sstream>
#include <string>
#include <list>
#include "mpr121.h"
Mpr121::Mpr121(I2C *i2c, Address i2cAddress)
{
this->i2c = i2c;
address = i2cAddress;
// Configure the MPR121 settings to default
this->configureSettings();
}
void Mpr121::configureSettings()
{
// Put the MPR into setup mode
this->write(ELE_CFG,0x00);
// Electrode filters for when data is > baseline
unsigned char gtBaseline[] = {
0x01, //MHD_R
0x01, //NHD_R
0x00, //NCL_R
0x00 //FDL_R
};
writeMany(MHD_R,gtBaseline,4);
// Electrode filters for when data is < baseline
unsigned char ltBaseline[] = {
0x01, //MHD_F
0x01, //NHD_F
0xFF, //NCL_F
0x02 //FDL_F
};
writeMany(MHD_F,ltBaseline,4);
// Electrode touch and release thresholds
unsigned char electrodeThresholds[] = {
E_THR_T, // Touch Threshhold
E_THR_R // Release Threshold
};
for(int i=0; i<12; i++){
int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
}
// Proximity Settings
unsigned char proximitySettings[] = {
0xff, //MHD_Prox_R
0xff, //NHD_Prox_R
0x00, //NCL_Prox_R
0x00, //FDL_Prox_R
0x01, //MHD_Prox_F
0x01, //NHD_Prox_F
0xFF, //NCL_Prox_F
0xff, //FDL_Prox_F
0x00, //NHD_Prox_T
0x00, //NCL_Prox_T
0x00 //NFD_Prox_T
};
writeMany(MHDPROXR,proximitySettings,11);
unsigned char proxThresh[] = {
PROX_THR_T, // Touch Threshold
PROX_THR_R // Release Threshold
};
writeMany(EPROXTTH,proxThresh,2);
this->write(FIL_CFG,0x04);
// Set the electrode config to transition to active mode
this->write(ELE_CFG,0x0c);
}
void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
if(electrode > 11) return;
// Get the current mode
unsigned char mode = this->read(ELE_CFG);
// Put the MPR into setup mode
this->write(ELE_CFG,0x00);
// Write the new threshold
this->write((ELE0_T+(electrode*2)), touch);
this->write((ELE0_T+(electrode*2)+1), release);
//Restore the operating mode
this->write(ELE_CFG, mode);
}
unsigned char Mpr121::read(int key){
unsigned char data[2];
//Start the command
i2c->start();
// Address the target (Write mode)
int ack1= i2c->write(address);
// Set the register key to read
int ack2 = i2c->write(key);
// Re-start for read of data
i2c->start();
// Re-send the target address in read mode
int ack3 = i2c->write(address+1);
// Read in the result
data[0] = i2c->read(0);
// Reset the bus
i2c->stop();
return data[0];
}
int Mpr121::write(int key, unsigned char value){
//Start the command
i2c->start();
// Address the target (Write mode)
int ack1= i2c->write(address);
// Set the register key to write
int ack2 = i2c->write(key);
// Read in the result
int ack3 = i2c->write(value);
// Reset the bus
i2c->stop();
return (ack1+ack2+ack3)-3;
}
int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
//Start the command
i2c->start();
// Address the target (Write mode)
int ack= i2c->write(address);
if(ack!=1){
return -1;
}
// Set the register key to write
ack = i2c->write(start);
if(ack!=1){
return -1;
}
// Write the date set
int count = 0;
while(ack==1 && (count < length)){
ack = i2c->write(dataSet[count]);
count++;
}
// Stop the cmd
i2c->stop();
return count;
}
bool Mpr121::getProximityMode(){
if(this->read(ELE_CFG) > 0x0c)
return true;
else
return false;
}
void Mpr121::setProximityMode(bool mode){
this->write(ELE_CFG,0x00);
if(mode){
this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
} else {
this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
}
}
int Mpr121::readTouchData(){
return this->read(0x00);
}