-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupActor.cpp
86 lines (74 loc) · 1.97 KB
/
GroupActor.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
79
80
81
82
83
84
85
//
// GroupActor.cpp
// GroupActor
//
// Created by Sebastian Westemeyer on 06.01.18.
//
#include "GroupActor.hpp"
#include "GroupBundle.hpp"
GroupActor::GroupActor(GroupBundle *bundle, const byte id, const byte section, unsigned long codeOn, unsigned long codeOff) : Actor(bundle, codeOn, codeOff), m_id(id), m_section(section), m_actors(NULL), m_numberOfActors(0), m_state(false) {
bundle->addGroup(this);
// Serial.println("Id: " + String(m_id) + " On: " + String(codeOn) + "/" + String(m_codeOn) + ", Off: " + String(codeOff) + "/" + m_codeOff);
}
const byte GroupActor::getId() const {
return m_id;
}
const byte GroupActor::getSectionId() const {
return m_section;
}
GroupActor* GroupActor::addActor(Actor *newActor) {
Actor** prevVector = m_actors;
m_actors = new Actor*[m_numberOfActors + 1];
for (int i = 0; i < m_numberOfActors; ++i) {
m_actors[i] = prevVector[i];
}
m_actors[m_numberOfActors++] = newActor;
if (prevVector != NULL) {
delete prevVector;
}
return this;
}
void GroupActor::switchOn() {
// iterate vector of actors...
for (int i = 0; i < m_numberOfActors ; ++i) {
// ... and switch all of them on
m_actors[i]->switchOn();
}
}
void GroupActor::switchOff() {
// iterate vector of actors...
for (int i = 0; i < m_numberOfActors ; ++i) {
// ... and switch all of them on
m_actors[i]->switchOff();
}
}
bool GroupActor::switchGroup(const byte id, bool toState) {
// check, whether this is the group to switch...
if (m_id == id) {
// toggle state
m_state = !m_state;
// ... either on...
if(toState) {
switchOn();
return true;
}
// ... or off
switchOff();
}
return false;
}
bool GroupActor::toggle(const byte id) {
// check, whether this is the group to switch...
if (m_id == id) {
// toggle state
m_state = !m_state;
// ... either on...
if(m_state) {
switchOn();
return true;
}
// ... or off
switchOff();
}
return false;
}