-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cpp
103 lines (83 loc) · 2.07 KB
/
controller.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "sequence.h"
#include "trafficlight_group.h"
#include "controller.h"
Controller::Controller()
{
}
void Controller::addTrafficLightGroup(std::shared_ptr<TrafficLightGroup> group, unsigned int id)
{
_groups[id] = group;
}
void Controller::addSequence(std::shared_ptr<Sequence> sequence, unsigned int targetGroupId)
{
_sequences.push_back({sequence, targetGroupId});
}
void Controller::clearSequences()
{
_sequences.clear();
}
void Controller::run()
{
if (count() > 0) {
do
{
auto sequence = getCurrentSequence();
auto group = getCurrentGroup();
if (sequence && group) {
sequence->restart();
if (sequence->count() > 0) {
do {
auto lightsToChange = sequence->getCurrentLight();
auto delay = sequence->getCurrentDelay();
group->turnAllLightsOff();
group->turnLightsOn(lightsToChange);
sleep_ms(delay.count());
}
while (sequence->next());
}
}
}
while (next());
}
}
void Controller::reset()
{
_index = 0;
}
bool Controller::next()
{
++_index;
if (_index >= count()) {
reset();
return false;
}
return true;
}
unsigned int Controller::getCurrentGroupId() const
{
return std::get<1>(getCurrent());
}
size_t Controller::count() const
{
return _sequences.size();
}
std::tuple<std::shared_ptr<Sequence>, unsigned int> Controller::getCurrent() const
{
return _sequences[_index];
}
std::shared_ptr<Sequence> Controller::getCurrentSequence() const
{
return std::get<0>(getCurrent());
}
std::shared_ptr<TrafficLightGroup> Controller::getGroup(size_t id) const
{
auto group = _groups.find(id);
if (group != _groups.end()) {
return group->second;
}
return nullptr;
}
std::shared_ptr<TrafficLightGroup> Controller::getCurrentGroup() const
{
return getGroup(getCurrentGroupId());
}