-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapsense.cpp
More file actions
278 lines (239 loc) · 8.18 KB
/
Capsense.cpp
File metadata and controls
278 lines (239 loc) · 8.18 KB
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
272
273
274
275
276
#include "Capsense.h"
Capsense::Capsense(uint8_t mask1 = 255, uint8_t mask2 = 255, TwoWire * the_wire = &Wire): touch {0,0}, touchMask {mask1,mask2}, changed (true), _wire(the_wire){
}
void Capsense::setup(TwoWire *the_wire){
if (0) {
///////
byte infoboard = 0;
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SYSTEM_STATUS);
the_wire->endTransmission();
// This should be 0 since a configuration other than the factory is loaded.
the_wire->requestFrom(I2C_ADDR,1);
Serial.print("the_wire->available First: ");
Serial.println(the_wire->available());
infoboard = the_wire->read();
Serial.println("This should be 0");
Serial.print("SYSTEM_STATUS: ");
Serial.println(infoboard);
the_wire->endTransmission();
///////
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(FAMILY_ID);
the_wire->endTransmission();
the_wire->requestFrom(I2C_ADDR,1);
Serial.print("the_wire->available SECOND: ");
Serial.println(the_wire->available());
infoboard = the_wire->read();
Serial.println("This should be 154");
Serial.print("FAMILY_ID: ");
Serial.println(infoboard);
the_wire->endTransmission();
// the_wire->beginTransmission(I2C_ADDR);
// the_wire->write(DEVICE_ID);
// the_wire->endTransmission();
// //delay(100);
//
// the_wire->requestFrom(I2C_ADDR,2);
// Serial.print("the_wire->available THIRD: ");
// Serial.println(the_wire->available());
// while (the_wire->available()) {
// byte c = the_wire->read();
// Serial.println(c);
// }
//delay(100);
// the_wire->beginTransmission(I2C_ADDR);
// the_wire->write(REFRESH_CTRL);
// the_wire->endTransmission();
// //delay(100);
//
// the_wire->requestFrom(I2C_ADDR,1);
// Serial.print("the_wire->available sixth: ");
// Serial.println(the_wire->available());
// infoboard = the_wire->read();
// Serial.print("REFRESH_CTRL VALUE: ");
// Serial.println(infoboard);
// the_wire->endTransmission();
//delay(100);
}
//**********************************
// Capsense pins configuration
//
// Array to enable each of the 16 capsense sensors
byte sensors[2] = {0xFFu,0xFFu};
// Originally the come as 0x00, 0xFF
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SENSOR_EN);
the_wire->write(sensors,2);
the_wire->endTransmission();
// Not actually necessary I think.
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(FSS_EN);
//the_wire->write(sensors,2);
the_wire->write(0x00);
the_wire->write(0x00);
the_wire->endTransmission();
// // Sensitivity
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SENSITIVITY0);
the_wire->write(B10101010);
the_wire->endTransmission();
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SENSITIVITY1);
the_wire->write(B10101010);
the_wire->endTransmission();
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SENSITIVITY2);
the_wire->write(B10101010);
the_wire->endTransmission();
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SENSITIVITY3);
the_wire->write(B10101010);
the_wire->endTransmission();
// Special Purpose Output pin configuration
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SPO_CFG);
the_wire->write(B00010101);
the_wire->endTransmission();
//
// the_wire->beginTransmission(I2C_ADDR);
// the_wire->write(SPO_CFG);
// the_wire->endTransmission();
// //delay(100);
//
// the_wire->requestFrom(I2C_ADDR,1);
// Serial.print("the_wire->available SPO_CFG: ");
// Serial.println(the_wire->available());
// infoboard = the_wire->read();
// Serial.print("SPO_CFG: ");
// Serial.println(infoboard,BIN);
// the_wire->endTransmission();
// Let's read again the SENSOR_EN just to confirm that nothing has been written yet. To be Deleted
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(SENSOR_EN);
the_wire->endTransmission();
the_wire->requestFrom(I2C_ADDR,2);
Serial.print("the_wire->available FOURTH: ");
Serial.println(the_wire->available());
while (the_wire->available()) { // slave may send less than requested
byte c = the_wire->read(); // receive a byte as character
Serial.println(c); // print the character
}
/*
* WRITING SETTINGS IN CAPSENSE
- Write via I2C all the commands necessary to configure Capsense. I think the idea here is to have a conditional configuration option in the firmware and only enter this step if newer configuration needs to be loaded. All values are stored in internal flash memory so this could be bypassed in final firmware.
- Write ‘3’ to CTRL_CMD in order to generate CRC that is written automatically by the chip to CALC_CRC
- After 220ms or more read CRC from CALC_CRC
- Write it to CONFIG_CRC
- Write ‘2’ to CTRL_CMD to compare current CRC and stored one in CALC_CRC (they are the same) and write in internal flash.
- Wait 220ms or more for command to finish.
*/
// Send 0x03 to calculate de CRC from the changed configuration.
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CTRL_CMD);
the_wire->write(0x03);
the_wire->endTransmission();
// This delay is important
delay(300);
// Read CRC calculated from the 0x03 command sent
byte crc[2] = {0, 0};
int i = 0;
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CALC_CRC);
the_wire->endTransmission();
the_wire->requestFrom(I2C_ADDR,2);
while (the_wire->available()) { // slave may send less than requested
byte c = the_wire->read();
Serial.println(c); // print the character
crc[i] = c; // receive a byte as character
i++;
}
the_wire->endTransmission();
// Write CRC to CONFIG_CRC
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CONFIG_CRC);
the_wire->write(crc,2);
the_wire->endTransmission();
// Send 0x02 to calculate de CRC from the changed configuration.
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CTRL_CMD);
the_wire->write(0x02);
the_wire->endTransmission();
delay(300);
int result = 2;
while (result != 0){
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CTRL_CMD_STATUS);
the_wire->endTransmission();
the_wire->requestFrom(I2C_ADDR,1);
result = the_wire->read();
Serial.print("result CTRL_CMD_STATUS 1: ");
Serial.println(result);
if (result == 1) {
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CTRL_CMD_ERROR);
the_wire->endTransmission();
the_wire->requestFrom(I2C_ADDR,1);
result = the_wire->read();
Serial.print("result CTRL_CMD_ERROR 1: ");
Serial.println(result);
the_wire->endTransmission();
}
the_wire->endTransmission();
}
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CTRL_CMD);
the_wire->write(0xFF);
the_wire->endTransmission();
delay(300);
result = 2;
while (result != 0){
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CTRL_CMD_STATUS);
the_wire->endTransmission();
the_wire->requestFrom(I2C_ADDR,1);
result = the_wire->read();
Serial.print("result CTRL_CMD_STATUS 2: ");
Serial.println(result);
if (result == 1) {
the_wire->beginTransmission(I2C_ADDR);
the_wire->write(CTRL_CMD_ERROR);
the_wire->endTransmission();
the_wire->requestFrom(I2C_ADDR,1);
result = the_wire->read();
Serial.print("result CTRL_CMD_ERROR 2: ");
Serial.println(result);
the_wire->endTransmission();
}
the_wire->endTransmission();
}
delay(500);
}
uint8_t Capsense::update() {
changed = 0;
uint8_t temp[2] = {0, 0}; int i = 0;
_wire->beginTransmission(I2C_ADDR);
_wire->write(BUTTON_STAT);
_wire->endTransmission();
_wire->requestFrom(I2C_ADDR, 2);
while (_wire->available()) { // slave may send less than requested
// byte c = Wire.read();
// temp[i] = c; // receive a byte as character
temp[i] = _wire->read();
i++;
}
_wire->endTransmission();
for (int t = 0; t < 2; t++) {
if (temp[t] != touch[t]) {
changed = 1;
touch[t] = temp[t];
}
}
return changed;
}
uint8_t * Capsense::readTouch(){
changed = 0;
static uint8_t tmp[2];
tmp[0] = (uint8_t)touch[0] & touchMask[1]; tmp[1] = (uint8_t)touch[0] & touchMask[1];
return (uint8_t*) touch;
}