-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCapacitiveSensorButton.cpp
95 lines (82 loc) · 2.78 KB
/
CapacitiveSensorButton.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
#include "CapacitiveSensorButton.h"
#include <CapacitiveSensor.h>
#include "dbg.h"
#define NUM_SAMPLES 50
#define PROCESSING_INTERVAL_MS 25
#define THRESHOLD 350
#define CLICK_TIME_THRESHOLD_MS 500
#define DOUBLE_CLICK_TIME_THRESHOLD_MS 250
#define DBG_INFO(fmt, ...) //DBG(fmt, ##__VA_ARGS__)
CapacitiveSensorButton::CapacitiveSensorButton(uint8_t sendPin, uint8_t receivePin, Handler handler) :
cs(sendPin, receivePin),
handler(handler),
lastAverageCalculation(0),
lastDownTime(0),
lastUpTime(0),
rapidClickCounter(1),
isPressed(false),
startHandlingLongPress(false) {
cs.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs.set_CS_Timeout_Millis(20);
}
void CapacitiveSensorButton::loop() {
if (!handler) {
return;
}
uint32_t now = millis();
long sensorTime = cs.capacitiveSensor(NUM_SAMPLES);
if (sensorTime < 0) {
if (now - lastDebug >= 1000) {
DBG("Failed to read capacitive sensor. Error code = %ld\n", sensorTime);
lastDebug = now;
}
return;
}
touchSensorData.addMeasurement(sensorTime);
debugTouchSensorData.addMeasurement(sensorTime);
if (now - lastDebug >= 200) {
DBG_INFO("min: %-4d avg: %-4d max: %-4d\n",
debugTouchSensorData.minValue,
debugTouchSensorData.getAverage(),
debugTouchSensorData.maxValue);
debugTouchSensorData.reset();
lastDebug = now;
}
if (now - lastAverageCalculation >= PROCESSING_INTERVAL_MS) {
if (touchSensorData.getCounter() == 0) {
DBG_INFO("Failed to get average for capacitive sensor charge time\n");
return;
}
uint32_t averageSensorTime = touchSensorData.getAverage();
if (isPressed && averageSensorTime < THRESHOLD) {
isPressed = false;
startHandlingLongPress = false;
if (now - lastDownTime > CLICK_TIME_THRESHOLD_MS) {
rapidClickCounter = 0;
} else {
++rapidClickCounter;
}
lastUpTime = now;
DBG("Touch up %d\n", rapidClickCounter);
} else if (!isPressed && averageSensorTime > THRESHOLD) {
isPressed = true;
startHandlingLongPress = true;
lastDownTime = now;
DBG("Touch down\n");
}
touchSensorData.reset();
lastAverageCalculation = now;
}
if (!isPressed && rapidClickCounter != 0 && now - lastUpTime > DOUBLE_CLICK_TIME_THRESHOLD_MS) {
DBG("Click %d\n", rapidClickCounter);
// 1 - single click, 2 - double click, etc...
handler(rapidClickCounter, EventType::Click);
rapidClickCounter = 0;
}
if (isPressed && now - lastDownTime > CLICK_TIME_THRESHOLD_MS) {
DBG("Long press %d\n", rapidClickCounter);
// 0 - push and hold, 1 - click and hold, 2 - double click and hold, etc...
handler(rapidClickCounter, startHandlingLongPress ? EventType::HoldStart : EventType::Holding);
startHandlingLongPress = false;
}
}