-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPR121.cpp
119 lines (88 loc) · 3.21 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
/*
Library to control the touch sensor MPR121
More info at https://www.sparkfun.com/datasheets/Components/MPR121.pdf
*/
#include "Arduino.h"
#include "MPR121.h"
#include <Wire.h>
MPR121::MPR121(int address, uint8_t irqPin){
_address = address;
_irqPin = irqPin;
pinMode(_irqPin, INPUT_PULLUP);
Wire.begin();
}
void MPR121::setup(){
set_register(_address, ELE_CFG, 0x00);
// Section A - Controls filtering when data is > baseline.
set_register(_address, MHD_R, 0x01);
set_register(_address, NHD_R, 0x01);
set_register(_address, NCL_R, 0x00);
set_register(_address, FDL_R, 0x00);
// Section B - Controls filtering when data is < baseline.
set_register(_address, MHD_F, 0x01);
set_register(_address, NHD_F, 0x01);
set_register(_address, NCL_F, 0xFF);
set_register(_address, FDL_F, 0x02);
// Section C - Sets touch and release thresholds for each electrode
set_register(_address, ELE0_T, TOU_THRESH);
set_register(_address, ELE0_R, REL_THRESH);
set_register(_address, ELE1_T, TOU_THRESH);
set_register(_address, ELE1_R, REL_THRESH);
set_register(_address, ELE2_T, TOU_THRESH);
set_register(_address, ELE2_R, REL_THRESH);
set_register(_address, ELE3_T, TOU_THRESH);
set_register(_address, ELE3_R, REL_THRESH);
set_register(_address, ELE4_T, TOU_THRESH);
set_register(_address, ELE4_R, REL_THRESH);
set_register(_address, ELE5_T, TOU_THRESH);
set_register(_address, ELE5_R, REL_THRESH);
set_register(_address, ELE6_T, TOU_THRESH);
set_register(_address, ELE6_R, REL_THRESH);
set_register(_address, ELE7_T, TOU_THRESH);
set_register(_address, ELE7_R, REL_THRESH);
set_register(_address, ELE8_T, TOU_THRESH);
set_register(_address, ELE8_R, REL_THRESH);
set_register(_address, ELE9_T, TOU_THRESH);
set_register(_address, ELE9_R, REL_THRESH);
set_register(_address, ELE10_T, TOU_THRESH);
set_register(_address, ELE10_R, REL_THRESH);
set_register(_address, ELE11_T, TOU_THRESH);
set_register(_address, ELE11_R, REL_THRESH);
// Section D
// Set the Filter Configuration
// Set ESI2
set_register(_address, FIL_CFG, 0x04);
// Section E
// Electrode Configuration
// Set ELE_CFG to 0x00 to return to standby mode
set_register(_address, ELE_CFG, 0x0C); // Enables all 12 Electrodes
// Section F
// Enable Auto Config and auto Reconfig
/*set_register(_address, ATO_CFG0, 0x0B);
set_register(_address, ATO_CFGU, 0xC9); // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V set_register(0x5A, ATO_CFGL, 0x82); // LSL = 0.65*USL = 0x82 @3.3V
set_register(_address, ATO_CFGT, 0xB5);*/ // Target = 0.9*USL = 0xB5 @3.3V
set_register(_address, ELE_CFG, 0x0C);
}
void MPR121::readTouchInputs(byte status[]){
if(!checkInterrupt()){
Wire.requestFrom(_address,2); //Wire.requestFrom(address,quantity in bytes)//Address from the datasheet
byte LSB = Wire.read();
byte MSB = Wire.read();
for (int i = 0; i < 12; i++){
int touched = ((MSB << 8) | LSB);
if(touched & (1<<i))
status[i] = 1;
else
status[i] = 0;
}
}
}
boolean MPR121::checkInterrupt(void){
return digitalRead(_irqPin);
}
void MPR121::set_register(int address, unsigned char r, unsigned char v){
Wire.beginTransmission(address);
Wire.write(r);
Wire.write(v);
Wire.endTransmission();
}