-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelayModule.cpp
executable file
·78 lines (56 loc) · 2.6 KB
/
RelayModule.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
78
#include "RelayModule.h" //To know function declarations of RelayModule Class.
//Activates specified relay in the "Relays" and sets the relay's status as 1.
//If RelayModule is working with LOW logic, we can activate a relay with digitalWrite(pin, LOW).
//So, we can send the logicType(in above case LOW) to the write function to make the relay active.
bool RelayModule::activate(uint8_t relayNumber)
{
Relays[ relayNumber-1 ].write(logicType).setStatus(1);
return Relays[ relayNumber-1 ].getStatus();
}
//Activates all the relays on the Relay Module.
bool RelayModule::activateAll()
{
for(uint8_t i=1; i <= channelNumber; i++)
if ( ! activate(i) ) return false;
return true;
}
//Deactivates specified relay in the "Relays" and sets the relay's status as 0.
//If RelayModule is working with LOW logic, we can deactivate a relay with digitalWrite(pin, HIGH).
//So, we can send "the reverse of logicType(in above case HIGH)" to the write function to make the relay deactive.
bool RelayModule::deactivate(uint8_t relayNumber)
{
uint8_t close;
(logicType == HIGH)? close=LOW : close=HIGH;
Relays[relayNumber-1].write(close).setStatus(0);
return ( Relays[ relayNumber-1 ].getStatus() ) ? false : true; // If relay's status is 0, then operation is successfull.
}
//Deactivates all the relays on the Relay Module.
bool RelayModule::deactivateAll()
{
for(uint8_t i=1; i<=channelNumber; i++)
if ( ! deactivate(i) ) return false;
return true;
}
//Returns the status of relays as integer.
// Relay4 Relay3 Relay2 Relay1
//As an example, 1 0 1 0 specifies that Relay4 and Relay2 is active.
// At Base 2, 1x(2^3) + 0x(2^2) + 1x(2^1) + 0x(2^0)
// 15 = 8 + 4 + 2 + 1 All relays are active.
// 6 = 0 + 4 + 2 + 0 Relay 2 and 3 are active.
// 7 = 0 + 4 + 2 + 1 Relay 2, 3 and 4 are active.
// 4 = 0 + 4 + 0 + 0 Relay 3 is active.
// 1 = 0 + 0 + 0 + 1 Relay 1 is active.
uint8_t RelayModule::getStatus(int relayNumber)
{
if ( relayNumber != -1 ) return Relays[ relayNumber ].getStatus();
uint8_t status = 0;
for(uint8_t i=0, digitValue = 1; i<channelNumber; i++, digitValue *= 2)
status += (Relays[i].getStatus() * digitValue);
return status;
}
//Relays is a 0-based index array. To reach first relay in this array, we would use RelayModule.Relays[0]
//We can also reach first relay in this array by using RelayModule[1] instead of RelayModule.Relays[0].
Relay& RelayModule::operator[](int index)
{
return Relays[index-1];
}