-
Notifications
You must be signed in to change notification settings - Fork 0
/
tb.h
148 lines (132 loc) · 3.45 KB
/
tb.h
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
#ifndef TouchButton_h
#define TouchButton_h
#include <Arduino.h>
#include "driver/touch_pad.h"
// Habilita los mensajes de depuración si está definido.
#define MESSAGES
#if defined MESSAGES
#define printLine(x) Serial.println(x)
#else
#define printLine(x)
#endif
// Definiciones de tiempo por defecto
#ifndef ClickTIME
#define ClickTIME 50
#endif
#ifndef ResetTIME
#define ResetTIME 700
#endif
class tb {
public:
tb(uint8_t pin, uint8_t threshold = 70);
inline bool IRAM_ATTR isTouched();
bool isHold(short HoldTIME = 400);
bool isRelease(short ReleaseTIME = 400);
bool isHolded(short HoldedTIME = 400);
bool isClick();
bool isClicks(uint8_t count);
uint16_t valor;
private:
inline long IRAM_ATTR _touchTime();
uint8_t _pin;
uint8_t _threshold;
unsigned long _touchStart = 0;
unsigned long _oldT = 0;
uint8_t _cont = 0;
bool _pushed = false;
bool result=false;
bool _Hold = false;
float initialThreshold;
void initializeTouchPad();
};
// Constructor
tb::tb(uint8_t pin, uint8_t threshold) {
_pin = pin;
_threshold = threshold;
initializeTouchPad();
}
void tb::initializeTouchPad() {
touch_pad_init();
touch_pad_io_init(static_cast<touch_pad_t>(_pin));
touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
delay(10);
touch_pad_set_cnt_mode(static_cast<touch_pad_t>(_pin), TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_HIGH);
touch_pad_config(static_cast<touch_pad_t>(_pin), 0);
delay(40);
touch_pad_read(static_cast<touch_pad_t>(_pin), &valor);
initialThreshold = valor * _threshold * 0.01;
touch_pad_set_thresh(static_cast<touch_pad_t>(_pin), valor * 0.66666f);
}
inline bool IRAM_ATTR tb::isTouched() {
touch_pad_read(static_cast<touch_pad_t>(_pin), &valor);
return (valor < initialThreshold);
}
inline long IRAM_ATTR tb::_touchTime() {
unsigned long now = millis();
long result = 0;
bool touching = isTouched();
if (_touchStart) {
result = now - _touchStart;
}
if (touching && !_touchStart) {
_touchStart = now;
}
if (!touching) {
_touchStart = 0;
_pushed = false;
}
return touching ? result : -result;
}
bool tb::isClick() {
long touchTime = _touchTime();
long t = _touchStart - _oldT;
if (t > ResetTIME) {
_cont = 0;
}
if (touchTime > ClickTIME) {
if (!_pushed) {
_oldT = _touchStart;
_cont++;
_pushed = true;
}
} else {
_pushed = false;
}
return _pushed; // Devuelve el estado de `_pushed`
}
bool tb::isClicks(uint8_t count) {
result = false;
if (isClick()) {
if (_cont == count) {
_cont = 0;
result = true;
}
}
return result;
}
bool tb::isHold(short HoldTIME) {
return _touchTime() > HoldTIME;
}
bool tb::isHolded(short HoldedTIME) {
result = false;
if (isHold(HoldedTIME) && !_Hold) {
_Hold = true;
result = true;
}
if (_Hold && !isHold(HoldedTIME)) {
_Hold = false;
}
return result;
}
bool tb::isRelease(short ReleaseTIME) {
result = false;
if (isHold(ReleaseTIME)) {
_Hold = true;
}
if (_Hold && !isHold(ReleaseTIME)) {
_Hold = false;
result = true;
}
return result;
}
#endif