-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayModule.cpp
More file actions
85 lines (70 loc) · 1.49 KB
/
RelayModule.cpp
File metadata and controls
85 lines (70 loc) · 1.49 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
/**
The class implements a set of methods of the RelayModule.h
interface for working with a digital relay module.
https://github.com/YuriiSalimov/RelayModule
Created by Yurii Salimov, December, 2017.
Released into the public domain.
*/
#include "RelayModule.h"
RelayModule::RelayModule(const int IN_pin)
: RelayModule::RelayModule(IN_pin, false) {
}
RelayModule::RelayModule(const int IN_pin, const boolean invertSignal) {
this->IN_pin = IN_pin;
if (invertSignal) {
invert();
}
init();
}
/**
Initialization of module.
Turns off the relay.
*/
void RelayModule::init() {
pinMode(this->IN_pin, OUTPUT);
off();
}
/**
Destructor.
Turns off the relay before deleting the object.
*/
RelayModule::~RelayModule() {
turnOff();
}
void RelayModule::on() {
if (isOff()) {
turnOn();
}
}
void RelayModule::off() {
if (isOn()) {
turnOff();
}
}
boolean RelayModule::isOn() {
return read() == this->onSignal;
}
boolean RelayModule::isOff() {
return read() == this->offSignal;
}
void RelayModule::turnOn() {
write(this->onSignal);
}
void RelayModule::turnOff() {
write(this->offSignal);
}
void RelayModule::write(const int signal) {
digitalWrite(this->IN_pin, signal);
}
int RelayModule::read() {
return digitalRead(this->IN_pin);
}
void RelayModule::invert() {
if (this->onSignal == HIGH) {
this->onSignal = LOW;
this->offSignal = HIGH;
} else {
this->onSignal = HIGH;
this->offSignal = LOW;
}
}