-
Notifications
You must be signed in to change notification settings - Fork 1
/
Output.h
99 lines (72 loc) · 1.63 KB
/
Output.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
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
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Author: Claudiu Matei
*/
#ifndef Output_h
#define Output_h
#include <Arduino.h>
#include <FlowerPlatformArduinoRuntime.h>
#include <Print.h>
class Output {
protected:
int lastValue;
uint8_t pin;
bool isPwm;
public:
// TODO CS: TEMP
bool contributesToState;
Callback<ValueChangedEvent>* onValueChanged = NULL;
/*
* @flower { constructorVariant="Default" }
*/
Output(int pin, bool isPwm = false, uint8_t initialValue = LOW);
void printStateAsJson(const __FlashStringHelper* objectName, Print* print);
void setHigh();
void setLow();
void setValue(int value);
int getValue();
void toggleHighLow();
};
Output::Output(int pin, bool isPwm, uint8_t initialValue) {
this->pin = pin;
this->isPwm = isPwm;
pinMode(pin, OUTPUT);
digitalWrite(pin, initialValue);
lastValue = initialValue;
}
void Output::printStateAsJson(const __FlashStringHelper* objectName, Print* print) {
print->print(F("\""));
print->print(objectName);
print->print(F("\": "));
print->print(lastValue);
}
void Output::setHigh() {
setValue(HIGH);
}
void Output::setLow() {
setValue(LOW);
}
void Output::setValue(int value) {
if (isPwm) {
analogWrite(pin, value);
} else {
digitalWrite(pin, value);
}
if (onValueChanged != NULL) {
ValueChangedEvent event;
event.previousValue = lastValue;
event.currentValue = value;
(*onValueChanged)(&event);
}
lastValue = value;
}
int Output::getValue() {
return lastValue;
}
void Output::toggleHighLow() {
if (lastValue) {
setValue(LOW);
} else {
setValue(HIGH);
}
}
#endif