-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelay.cpp
77 lines (62 loc) · 1.48 KB
/
Relay.cpp
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
#include <Relay.h>
#include "RelayConfig.h"
#if RELAY_SAFETY == true
#include <Avail.h>
#endif // RELAY_SAFETY == true
Relay::Relay(uint8_t pin) {
this->_pin = pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
Relay &Relay::on(void) {
RELAY_STATE &state = this->_currentState;
#if RELAY_SAFETY == true
uint32_t *safety = &this->_safety;
uint32_t *last = &this->_lastToggle;
if (state != RELAY_ON && Avail::millis(safety, last)) {
*last = millis();
digitalWrite(this->_pin, LOW);
state = RELAY_ON;
}
#else
if (state != RELAY_ON) {
digitalWrite(this->_pin, LOW);
state = RELAY_ON;
}
#endif // RELAY_SAFETY
return *this;
}
Relay &Relay::off(void) {
RELAY_STATE &state = this->_currentState;
#if RELAY_SAFETY == true
uint32_t *safety = &this->_safety;
uint32_t *last = &this->_lastToggle;
if (state != RELAY_OFF && Avail::millis(safety, last)) {
*last = millis();
digitalWrite(this->_pin, HIGH);
state = RELAY_OFF;
}
#else
if (state != RELAY_OFF) {
digitalWrite(this->_pin, HIGH);
state = RELAY_OFF;
}
#endif // RELAY_SAFETY
return *this;
}
Relay &Relay::toggle(void) {
if (*this->getState() == RELAY_OFF) {
return this->on();
} else {
return this->off();
}
}
RELAY_STATE *Relay::getState() {
return &this->_currentState;
}
#if RELAY_SAFETY == true
Relay &Relay::setSafety(uint32_t safety) {
this->_safety = max(safety, 250);
return *this;
}
#endif // RELAY_SAFETY == true