-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx48_output.ino
67 lines (53 loc) · 1.31 KB
/
x48_output.ino
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
#if TIMER_ENABLED
Task outputTask(1000, 1, outputCb, &scheduler);
#endif
bool outputState;
OneButton outputButton(PIN_BTN, true);
void outputSetup() {
pinMode(PIN_OUTPUT, OUTPUT);
outputButton.setDebounceTicks(30);
outputButton.setPressTicks(5000);
outputButton.attachClick(outputHandleButton);
outputSet(false, true);
}
void outputHandleButton() {
outputSet(!outputState);
}
void outputSet(bool out, bool ignore) {
if (ignore == false && out == outputState) return;
logValue("Output value: ", out);
outputState = out;
digitalWrite(PIN_OUTPUT, out);
#if TIMER_ENABLED
if (out == false) {
outputTask.disable();
mqttPublish(MQTT_TOPIC_TIMER_REMAIN, "0");
}
#endif
String s = outputState ? "1" : "0";
mqttPublish(MQTT_TOPIC_STATE, s, true);
}
void outputSet(bool out) {
outputSet(out, false);
}
#if TIMER_ENABLED
void outputCb() {
String s(outputTask.getIterations());
mqttPublish(MQTT_TOPIC_TIMER_REMAIN, s);
if (outputTask.isLastIteration()) {
outputSet(false);
}
}
void outputSetTimer(uint32_t seconds) {
if (seconds == 0) {
outputSet(false);
} else {
outputTask.setIterations(seconds);
outputTask.restartDelayed(0);
logValue("Timer set to: ", seconds);
outputSet(true);
String s(seconds);
mqttPublish(MQTT_TOPIC_TIMER_REMAIN, s);
}
}
#endif