-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchcontrol.cpp
55 lines (46 loc) · 1.13 KB
/
touchcontrol.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
#include "Arduino.h"
#include "touchcontrol.h"
#include "config.h"
TouchControl::TouchControl(String name, int pin, int threshold) {
_name = name;
_pin = pin;
_threshold = threshold;
_pressed = 0;
_val = 0;
_pressFn = [](int val) { };
_releaseFn = [](int val) { };
_stilldownFn = [](int val) { };
}
void TouchControl::update() {
int val = touchRead(_pin);
#ifdef DEBUG
Serial.print("pin ");
Serial.print(_pin);
Serial.print(": ");
Serial.println(val);
#endif
if (val <= _threshold) {
_pressed++;
if (_pressed == 10) _pressFn(val);
if (_pressed > 20) _stilldownFn(val);
} else {
if (_pressed > 5) _releaseFn(val);
_pressed = 0;
}
}
int TouchControl::get_state() {
int val = touchRead(_pin);
return _val;
}
bool TouchControl::is_pressed() {
return _pressed!=0;
}
void TouchControl::set_press(ControlFn pressFn) {
_pressFn = pressFn;
}
void TouchControl::set_release(ControlFn releaseFn) {
_releaseFn = releaseFn;
}
void TouchControl::set_stilldown(ControlFn stilldownFn) {
_stilldownFn = stilldownFn;
}