-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLineInterface.cpp
More file actions
45 lines (39 loc) · 1.06 KB
/
Copy pathLineInterface.cpp
File metadata and controls
45 lines (39 loc) · 1.06 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
/* Interface for a single digital output pin
* -----------------------------------------
* @author Tal Eisenberg <eisental@gmail.com>
*/
#include "LineInterface.h"
#include "send.h"
LineInterface::LineInterface(JsonVariant conf)
: ToggleInterface("line", strdup(conf["name"])) {
if (!conf.containsKey("pin")) {
send_error("Missing 'pin' key in line config");
pin = -1;
return;
}
if (!conf["pin"].is<int>()) {
send_error("'pin' value should be an integer");
pin = -1;
return;
}
if (conf.containsKey("reverse")) {
if (!conf["reverse"].is<bool>()) {
send_error("'reverse' value should be a boolean");
reverse = false;
}
reverse = conf["reverse"].as<bool>();
}
else {
reverse = false;
}
pin = conf["pin"].as<int>();
pinMode(pin, OUTPUT);
digitalWrite(pin, reverse ? HIGH : LOW);
}
void LineInterface::value_changed() {
if (pin < 0) {
send_error("Can't write value. Pin index is undefined.");
return;
}
digitalWrite(pin, value == 0 ? (reverse ? HIGH : LOW) : (reverse ? LOW : HIGH));
}