-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcaptouch.cpp
250 lines (220 loc) · 7.12 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
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
//============================================================
// Touch Sensing Demo on Spark Core
//
//============================================================
// Copyright (c) 2014 Tangibit Studios LLC. All rights reserved.
// Copyright (c) 2014 David Greaves <david@dgreaves.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, see <http://www.gnu.org/licenses/>.
//
//============================================================
#include "application.h"
#include "captouch.h"
// This has to be accessible by the ISR until the ISR supports lambdas
volatile unsigned long CapTouch_tR = 0;
CapTouch::CapTouch(int sensorPin, int driverPin) :
m_intrIsAttached(false)
,m_pollTime(CAPTOUCH_POLL_TIME)
{
m_driverPin = driverPin;
m_sensorPin = sensorPin;
m_touchSenseLast = LOW;
m_touchDebounceTimeLast = 0;
m_touchNow = LOW;
m_touchLast = LOW;
m_tLastDelay = 0;
}
void CapTouch::detachIntr() {
detachInterrupt(m_sensorPin);
m_intrIsAttached = false;
}
void CapTouch::attachIntr() {
auto mylambda = [&]() {this->touchSense();} ;
attachInterrupt(m_sensorPin, mylambda , RISING);
m_intrIsAttached = true;
}
void CapTouch::setup()
{
// intialize conditions for touch sensor
pinMode(m_driverPin,OUTPUT);
attachIntr();
// calibrate touch sensor- Keep hands off!!!
m_tBaseline = touchSampling(); // initialize to first reading
m_tBaselineSum = m_tBaseline * CAPTOUCH_TOUCH_POOL;
m_tJitterSum = 0;
// time stamps
m_lastUpdate = 0;
}
CapTouch::Event CapTouch::getEvent()
{
CapTouch::Event touchEvent = CapTouch::NoEvent;
//Update every POLL_TIME [ms]
if (millis() > m_lastUpdate + m_pollTime)
{
// check Touch UI
touchEvent = touchEventCheck();
// time stamp updated
m_lastUpdate = millis();
}
return(touchEvent);
}
//------------------------------------------------------------
// ISR for touch sensing
//------------------------------------------------------------
void CapTouch::touchSense()
{
CapTouch_tR = micros();
}
//------------------------------------------------------------
// touch sampling
//
// sample touch sensor 32 times and get average RC delay [usec]
//------------------------------------------------------------
long CapTouch::touchSampling()
{
long tDelay = 0;
int mSample = 0;
long jitter = 0;
for (int i=0; i<32; i++)
{
// discharge capacitance at rPin
pinMode(m_sensorPin, OUTPUT);
digitalWrite(m_driverPin,LOW);
digitalWrite(m_sensorPin,LOW);
// Now give the pin time to actually go low and drain "the
// capacitor"
delay(2);
// revert to high impedance input
pinMode(m_sensorPin,INPUT);
// timestamp & transition sPin to HIGH and wait for interrupt
// in a read loop
m_tS = micros();
CapTouch_tR = m_tS;
digitalWrite(m_driverPin,HIGH);
do
{
// wait for transition - interrupt handler will set tR
} while (digitalRead(m_sensorPin)==LOW);
// accumulate the RC delay samples
// ignore readings when micros() overflows
if (CapTouch_tR>m_tS)
{
tDelay = tDelay + (CapTouch_tR - m_tS);
mSample++;
}
}
// Leave it in LOW mode keeping the capacitor drained.
pinMode(m_sensorPin, OUTPUT);
digitalWrite(m_sensorPin,LOW);
// calculate average RC delay [usec]
if (mSample>0)
{
tDelay = tDelay/mSample;
m_tLastDelay = tDelay;
}
else
{
tDelay = 0; // this is an error condition!
#ifdef CAPTOUCH_DEBUG
Serial.println("Error - no delay obtained in sample");
#endif
}
// autocalibration using moving average of 1/5 of pool size
// when in Releas-ed state.
if (m_touchSenseLast == LOW) {
m_tBaselineSum += (tDelay - m_tBaseline) * 2;
m_tBaseline = m_tBaselineSum / CAPTOUCH_TOUCH_POOL;
if (tDelay > m_tBaseline) {
jitter = tDelay - m_tBaseline;
} else {
jitter = m_tBaseline - tDelay;
}
m_tJitterSum -= m_tJitter;
m_tJitterSum += jitter;
m_tJitter = m_tJitterSum / CAPTOUCH_JITTER_POOL;
} else {
// auto calibrate *really* slowly in Touch-ed state to allow
// for a touch-pad to be connected after setup() has run;
// disconnected/reconnected or other odd changes. Technically
// this means that a Touch event will time out eventually.
// In practice this is order of minutes.
// tDelay is always > m_tBaseline in Touch
m_tBaselineSum++;
m_tBaseline = m_tBaselineSum / CAPTOUCH_TOUCH_POOL;
}
#ifdef CAPTOUCH_DEBUG
Serial.print("check at ");
Serial.print(m_lastUpdate);
Serial.print(" Delay:baseline:jitter=");
Serial.print(tDelay);
Serial.print(":");
Serial.print(m_tBaseline);
Serial.print(":");
Serial.print(m_tJitter);
#endif
return tDelay;
}
//------------------------------------------------------------
// touch event check
//
// check touch sensor for events:
// NoEvent no change
// TouchEvent sensor is touched (Low to High)
// ReleaseEvent sensor is released (High to Low)
//
//------------------------------------------------------------
CapTouch::Event CapTouch::touchEventCheck()
{
unsigned long touchSense; // current reading
unsigned long touchDebounceTime = 50; // debounce time
CapTouch::Event tEvent = CapTouch::NoEvent; // default event
// read touch sensor
m_tReading = touchSampling();
// touch sensor is HIGH if trigger point 1.25*Baseline
if (m_tReading>(m_tBaseline + m_tBaseline/4) &&
m_tReading>(m_tBaseline + m_tJitter*2)) {
touchSense = HIGH;
} else {
touchSense = LOW;
}
// debounce touch sensor
// if state changed then reset debounce timer
if (touchSense != m_touchSenseLast) {
m_touchDebounceTimeLast = millis();
}
m_touchSenseLast = touchSense;
// accept as a stable sensor reading if the debounce time is exceeded without reset
if (millis() > m_touchDebounceTimeLast + touchDebounceTime) {
m_touchNow = touchSense;
}
// set events based on transitions between readings
if (!m_touchLast && m_touchNow) {
tEvent = TouchEvent;
}
if (m_touchLast && !m_touchNow) {
tEvent = ReleaseEvent;
}
// update last reading
m_touchLast = m_touchNow;
#ifdef CAPTOUCH_DEBUG
Serial.print(" Event:");
if (tEvent == TouchEvent)
Serial.println(" Touch");
else if (tEvent == ReleaseEvent)
Serial.println(" Release");
else if (tEvent == NoEvent)
Serial.println(" None");
#endif
return tEvent;
}