-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInput.h
More file actions
87 lines (64 loc) · 1.59 KB
/
Input.h
File metadata and controls
87 lines (64 loc) · 1.59 KB
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
/*
* Author: Claudiu Matei
*/
#ifndef Input_h
#define Input_h
#include <Arduino.h>
#include <Print.h>
#include <WString.h>
#include <FlowerPlatformArduinoRuntime.h>
class Input {
protected:
int lastValue = 0;
unsigned long lastTime = 0;
uint8_t pin;
bool isAnalog = false;
public:
// TODO CS: TEMP
bool contributesToState;
unsigned int pollInterval = 50;
Callback<ValueChangedEvent>* onValueChanged = NULL;
/*
* @flower { constructorVariant="Default" }
*/
Input(int pin, bool isAnalog = false, bool internalPullUp = true);
void loop();
void printStateAsJson(const __FlashStringHelper* objectName, Print* print);
};
Input::Input(int pin, bool isAnalog, bool internalPullUp) {
this->pin = pin;
this->isAnalog = isAnalog;
pinMode(pin, INPUT);
if (internalPullUp) {
digitalWrite(pin, HIGH);
lastValue = HIGH;
} else {
lastValue = isAnalog ? analogRead(pin) : digitalRead(pin);
}
}
void Input::loop() {
int value = isAnalog ? analogRead(pin) : digitalRead(pin);
if (value == lastValue) {
return;
}
if (!isAnalog && (millis() - lastTime < pollInterval)) {
return;
}
if (onValueChanged != NULL) {
ValueChangedEvent event;
event.previousValue = lastValue;
event.currentValue = value;
(*onValueChanged)(&event);
}
lastValue = value;
if (!isAnalog) {
lastTime = millis();
}
}
void Input::printStateAsJson(const __FlashStringHelper* objectName, Print* print) {
print->print(F("\""));
print->print(objectName);
print->print(F("\":"));
print->print(lastValue);
}
#endif