-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpr121_esp32.ino
146 lines (116 loc) · 3.67 KB
/
mpr121_esp32.ino
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
#include <Wire.h>
#include "mpr121.h"
const unsigned int sdaPin = 26;
const unsigned int sclPin = 27;
const unsigned int irqPin = 25;
boolean touchStates[12] = {0};
volatile bool isInterrupt = false;
void newInterrupt() { isInterrupt = true; }
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("Badge MPR121 Test");
if (!initializeMPR()) {
Serial.println("MPR121 Initialization failed!");
}
}
void loop() {
if (isInterrupt) {
byte state = readRegister(MPR121_TOUCHSTATUS_L);
for (int i = 0; i < 8; i++) {
boolean pinState = (state & (1 << i));
if (touchStates[i] != pinState) {
// Pin state changed
if (pinState) {
Serial.print("Pin ");
Serial.print(i);
Serial.println(" touched!");
} else {
Serial.print("Pin ");
Serial.print(i);
Serial.println(" released!");
}
touchStates[i] = pinState;
}
}
isInterrupt = false;
}
}
boolean initializeMPR() {
pinMode(irqPin, INPUT);
Wire.begin(sdaPin, sclPin);
// Soft reset MPR121
setRegister(MPR121_SRST, 0x63);
// Disable touch pins, put MPR121 into stop mode
byte result = setRegister(MPR121_ECR, 0x0);
if (result) {
Serial.print("I2C Error, result ");
char resultChar[3];
sprintf(resultChar, "%02d", result);
Serial.println(resultChar);
}
// Set baseline filters
setRegister(MPR121_MHDR, 0x01);
setRegister(MPR121_NHDR, 0x01);
setRegister(MPR121_NCLR, 0x0E);
setRegister(MPR121_FDLR, 0x00);
setRegister(MPR121_MHDF, 0x01);
setRegister(MPR121_NHDF, 0x05);
setRegister(MPR121_NCLF, 0x01);
setRegister(MPR121_FDLF, 0x00);
setRegister(MPR121_NHDT, 0x00);
setRegister(MPR121_NCLT, 0x00);
setRegister(MPR121_FDLT, 0x00);
setRegister(MPR121_DEBOUNCE, 0x00);
setRegister(MPR121_CONFIG1, 0x10); // default, 16µA charge current
setRegister(MPR121_CONFIG2, 0x20); // 0x5µs encoding, 1ms period
// Set touch & release thresholds
setThresholds(0x30, 6);
// Enable touch 0-7, put MPR into run mode
setRegister(MPR121_ECR, 0x88); // baseline tracking & initialize enable
attachInterrupt(irqPin, newInterrupt, FALLING);
return true;
}
void setThresholds(uint8_t touch, uint8_t release) {
for (uint8_t i = 0; i < 12; i++) {
setRegister(MPR121_TOUCHTH_0 + 2*i, touch);
setRegister(MPR121_RELEASETH_0 + 2 * i, release);
}
}
uint16_t filteredData(uint8_t pin) {
if (pin > 12) return 0;
return readRegister2b(MPR121_FILTDATA_0L + pin * 2);
}
uint16_t baselineData(uint8_t pin) {
if (pin > 12) return 0;
uint16_t baseline = readRegister(MPR121_BASELINE_0 + pin);
return (baseline << 2);
}
byte setRegister(unsigned int reg, unsigned int val) {
Wire.beginTransmission(MPR121_ADDR);
Wire.write(reg);
Wire.write(val);
return Wire.endTransmission();
}
byte readRegister(unsigned int reg) {
Wire.beginTransmission(MPR121_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(MPR121_ADDR, 1);
byte content = Wire.read();
byte result = Wire.endTransmission();
if (!result)
return content;
else
return 0x0;
}
// Read 2-byte register
uint16_t readRegister2b(unsigned int reg) {
Wire.beginTransmission(MPR121_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
while (Wire.requestFrom(MPR121_ADDR, 2) != 2);
uint16_t v = Wire.read();
v |= ((uint16_t) Wire.read()) << 8;
return v;
}