forked from Syralist/esphomekeypad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypad_textsensor.h
59 lines (50 loc) · 1.5 KB
/
keypad_textsensor.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
#include "esphome.h"
#include "Keypad.h"
class KeypadTextSensor : public Component, public TextSensor {
public:
std::string keysequenz;
static const byte n_rows = 4;
static const byte n_cols = 3;
static const unsigned int resetTime = 300;
unsigned int resetCounter = 0;
bool keyPressed = false;
char keys[n_rows][n_cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte colPins[n_cols] = {D3, D2, D1};
byte rowPins[n_rows] = {D7, D6, D5, D4};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);
void setup() override {
// This will be called by App.setup()
}
void loop() override {
// This will be called by App.loop()
char myKey = myKeypad.getKey();
if (myKey != NO_KEY){
if (myKey == '#'){
publish_state(keysequenz);
keysequenz.clear();
}
else if (myKey == '*'){
keysequenz.clear();
publish_state(keysequenz);
}
else {
keysequenz.push_back(myKey);
keyPressed = true;
}
}
if (keyPressed){
resetCounter++;
}
if (resetCounter >= resetTime){
keysequenz.clear();
publish_state(keysequenz);
resetCounter = 0;
keyPressed = false;
}
}
};