-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgaragecontroller.h
64 lines (52 loc) · 1.51 KB
/
garagecontroller.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
#ifndef GarageController_h
#define GarageController_h
#include <vector>
#include <PubSubClient.h>
typedef struct {
String topic;
int switchPin;
} activator;
class GarageController {
public:
GarageController(std::vector<activator> gates);
void callback(const char* topic, unsigned char* payload, unsigned int length);
private:
GarageController();
void toggle_switch(int pin);
std::vector<activator> _gates;
unsigned int toggle_time = 1000;
};
GarageController::GarageController(std::vector<activator> gates):
_gates(gates) {
for (auto& g : _gates) {
digitalWrite(g.switchPin, LOW);
pinMode(g.switchPin, INPUT);
}
}
void GarageController::callback(const char* topic, byte* payload, unsigned int length) {
Serial.printf("Message arrived [%s] ", topic);
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
String topic_s(topic);
for (auto &g : _gates) {
if (topic_s.indexOf(g.topic) != -1) {
Serial.printf("Activation by topic %s\n", g.topic.c_str());
toggle_switch(g.switchPin);
// play_song(piezoPin, melody1, sizeof(melody1)/sizeof(note));
return;
}
}
Serial.printf("Unknown topic '%s'\n", topic);
}
void GarageController::toggle_switch(int pin) {
Serial.printf("Toggling switch at pin %d\n", pin);
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(toggle_time);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
Serial.printf("Closing switch at pin %d\n", pin);
}
#endif /* GarageController_h */