-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelays.h
66 lines (56 loc) · 1.45 KB
/
relays.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
//
// Class that deals with relays on specific pins
//
#define MAX_RELAYS 3
class Relays {
private:
const byte pins[MAX_RELAYS] = {3, 4, 5};
// default first relay as on for the pond pump
byte states[MAX_RELAYS] = {1, 0, 0};
const char *names[MAX_RELAYS] = {
"pond_pump",
"waterfall_lights",
"rockery_lights",
};
public:
Relays() {
// setup pins
for (int index = 0; index < sizeof(pins); index++) {
pinMode(pins[index], OUTPUT);
this->set_state(index, this->states[index]);
}
};
void set_state(const int index, const bool value) {
// relays are active zero so flip the request
this->states[index] = (byte) value;
digitalWrite(pins[index], (int) !value);
};
bool get_state(const int index) {
return this->states[index];
};
const char *name(int index) {
return names[index];
}
const int count() {
return sizeof(pins);
};
void get_states(byte buf[MAX_RELAYS]){
for(int i=0;i<MAX_RELAYS;i++)
buf[i] = states[i];
}
void test(){
byte buf[MAX_RELAYS]={0};
this->get_states(buf);
for (int index = 0; index < sizeof(pins); index++) {
this->set_state(index, 1);
delay(1000);
}
for (int index = 0; index < sizeof(pins); index++) {
this->set_state(index, 0);
delay(1000);
}
for (int index = 0; index < sizeof(pins); index++) {
this->set_state(index, buf[index]);
}
}
};