-
Notifications
You must be signed in to change notification settings - Fork 0
/
CapTouch.cpp
171 lines (133 loc) · 3.86 KB
/
CapTouch.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
#include <CapTouch.h>
CapTouch::CapTouch(){}
CapTouch::CapTouch(int i2c_bus, int i2c_address) {
setup(i2c_bus, i2c_address);
}
int CapTouch::setup(int i2c_bus, int i2c_address) {
if(initI2C_RW(i2c_bus, i2c_address, -1)) {
fprintf(stderr, "Unable to initialise I2C communication\n");
return 1;
}
if(setMode(DIFF) != 0) {
fprintf(stderr, "Unable to set mode\n");
return 2;
}
if(updateBaseLine() != 0) {
fprintf(stderr, "Unable to update baseline\n");
return 3;
}
if(prepareForDataRead() != 0) {
fprintf(stderr, "Unable to prepare for reading data\n");
return 4;
}
isReady = true;
return 0;
}
void CapTouch::cleanup() {
closeI2C();
}
CapTouch::~CapTouch(){
cleanup();
}
int CapTouch::identify() {
int bytesToRead = 2;
int bytesRead = read(i2C_file, dataBuffer, bytesToRead);
if (bytesRead != bytesToRead)
{
fprintf(stderr, "Unexpected or no response.\n No valid device connected.\n");
device_type_ = 0;
firmware_version_ = 0;
return device_type_;
}
device_type_ = dataBuffer[0];
firmware_version_ = dataBuffer[1];
return device_type_;
}
int CapTouch::setMode(uint8_t mode) {
char buf[3] = { kOffsetCommand, kCommandMode, mode };
if(int writtenValue = (::write(i2C_file, buf, 3)) !=3)
{
fprintf(stderr, "Failed to set CapTouch's mode.\n");
fprintf(stderr, "%d\n", writtenValue);
return 1;
}
usleep(commandSleepTime); // need to give enough time to process command
return 0;
}
int CapTouch::setScanSettings(uint8_t speed, uint8_t num_bits) {
char buf[4] = { kOffsetCommand, kCommandScanSettings, speed, num_bits };
if(int writtenValue = (::write(i2C_file, buf, 4)) !=4)
{
fprintf(stderr, "Failed to set CapTouch's scan settings.\n");
fprintf(stderr, "%d\n", writtenValue);
return 1;
}
usleep(commandSleepTime); // need to give enough time to process command
return 0;
}
int CapTouch::setPrescaler(uint8_t prescaler) {
char buf[3] = { kOffsetCommand, kCommandPrescaler, prescaler };
if(int writtenValue = (::write(i2C_file, buf, 3)) !=3)
{
fprintf(stderr, "Failed to set CapTouch's prescaler.\n");
fprintf(stderr, "%d\n", writtenValue);
return 1;
}
usleep(commandSleepTime); // need to give enough time to process command
return 0;
}
int CapTouch::setNoiseThreshold(uint8_t threshold) {
char buf[3] = { kOffsetCommand, kCommandNoiseThreshold, threshold};
if(int writtenValue = (::write(i2C_file, buf, 3)) !=3)
{
fprintf(stderr, "Failed to set CapTouch's threshold.\n");
fprintf(stderr, "%d\n", writtenValue);
return 1;
}
usleep(commandSleepTime); // need to give enough time to process command
return 0;
}
int CapTouch::setIDACValue(uint8_t value) {
char buf[3] = { kOffsetCommand, kCommandIdac, value };
if(int writtenValue = (::write(i2C_file, buf, 3)) !=3)
{
fprintf(stderr, "Failed to set CapTouch's IDAC value.\n");
fprintf(stderr, "%d\n", writtenValue);
return 1;
}
usleep(commandSleepTime); // need to give enough time to process command
return 0;
}
int CapTouch::updateBaseLine() {
char buf[2] = { kOffsetCommand, kCommandBaselineUpdate };
if(int writtenValue = (::write(i2C_file, buf, 2)) !=2)
{
fprintf(stderr, "Failed to set CapTouch's baseline.\n");
fprintf(stderr, "%d\n", writtenValue);
return 1;
}
usleep(commandSleepTime); // need to give enough time to process command
return 0;
}
int CapTouch::prepareForDataRead() {
char buf[1] = { kOffsetData };
if(write(i2C_file, buf, 1) != 1)
{
fprintf(stderr, "Failed to prepare CapTouch data collection\n");
return 1;
}
usleep(commandSleepTime); // need to give enough time to process command
return 0;
}
int CapTouch::readI2C() {
int bytesRead = read(i2C_file, dataBuffer, kRawLength);
if (bytesRead != kRawLength)
{
fprintf(stderr, "Failure to read Byte Stream\n");
return 1;
}
for (unsigned int i=0; i < numSensors; i++) {
rawData[i] = ((dataBuffer[2*i] << 8) + dataBuffer[2*i+1]) & 0x0FFF;
}
return 0;
}