-
Notifications
You must be signed in to change notification settings - Fork 16
/
keypad_sensor.h
48 lines (38 loc) · 1.12 KB
/
keypad_sensor.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
#include "esphome.h"
#include "Keypad.h"
class KeypadSensor : public Component, public Sensor {
public:
static const byte n_rows = 4;
static const byte n_cols = 3;
bool keyPublished = false;
static const unsigned int resetTime = 500;
unsigned int lastPublish = 0;
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){
int key = myKey - 48;
publish_state(key);
keyPublished = true;
lastPublish = millis();
}
else{
if (keyPublished && (millis() - lastPublish) >= resetTime){
publish_state(NAN);
keyPublished = false;
}
}
}
};