-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrafficLight.cpp
149 lines (140 loc) · 3.96 KB
/
TrafficLight.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "TrafficLight.h"
#include <iostream>
#include <string>
const int TrafficLight::yellowTimeBlinkFrequency_ = 1;
const int TrafficLight::yellowSwitchesMaxCount_ = 4;
const int TrafficLight::redTimeSwitch_ = 2;
const int TrafficLight::greenTimeSwitch_ = 3;
void TrafficLight::emulateTrafficLight()
{
while (!isFinished_)
{
if (!isStopped_ && !myTimer_.isTimerRunning())
{
trafficLightDrawer_.clearConsole();
trafficLightDrawer_.showControllInfo();
drawTL(currentTrafficLightState_);
if (timeRemainingUntilSwitch_ == std::chrono::milliseconds(0))
{
myTimer_.runTimer(std::chrono::milliseconds(currentSwitchTime_ * 1000), [&]
{ updateTLState(); });
}
else
{
myTimer_.runTimer(std::chrono::milliseconds(timeRemainingUntilSwitch_), [&]
{ updateTLState(); });
timeRemainingUntilSwitch_ = std::chrono::milliseconds(0);
}
}
}
}
void TrafficLight::updateTLState()
{
switch (currentTrafficLightState_)
{
case STATES::EMPTY:
if (isYellowBlinking_)
{
currentTrafficLightState_ = STATES::YELLOW;
currentYellowSwitchesCount_++;
}
else
{
currentTrafficLightState_ = STATES::RED;
currentSwitchTime_ = redTimeSwitch_;
}
break;
case STATES::RED:
currentTrafficLightState_ = STATES::YELLOW;
currentSwitchDirection_ = SWITCH_DIRECTION::FORWARD;
isYellowBlinking_ = true;
currentSwitchTime_ = yellowTimeBlinkFrequency_;
break;
case STATES::YELLOW:
if (isYellowBlinking_ && currentYellowSwitchesCount_ < yellowSwitchesMaxCount_)
{
currentTrafficLightState_ = STATES::EMPTY;
currentYellowSwitchesCount_++;
}
else if (currentSwitchDirection_ == SWITCH_DIRECTION::FORWARD)
{
isYellowBlinking_ = false;
currentYellowSwitchesCount_ = 0;
currentTrafficLightState_ = STATES::GREEN;
currentSwitchTime_ = greenTimeSwitch_;
}
else
{
isYellowBlinking_ = false;
currentYellowSwitchesCount_ = 0;
currentTrafficLightState_ = STATES::RED;
currentSwitchTime_ = redTimeSwitch_;
}
break;
case STATES::GREEN:
isYellowBlinking_ = true;
currentTrafficLightState_ = STATES::YELLOW;
currentSwitchDirection_ = SWITCH_DIRECTION::REVERSE;
currentSwitchTime_ = yellowTimeBlinkFrequency_;
break;
default:
break;
}
}
void TrafficLight::drawTL(TrafficLight::STATES currentState)
{
switch (currentState)
{
case TrafficLight::STATES::EMPTY:
trafficLightDrawer_.drawEmpty();
break;
case TrafficLight::STATES::RED:
trafficLightDrawer_.drawRed();
break;
case TrafficLight::STATES::YELLOW:
trafficLightDrawer_.drawYellow();
break;
case TrafficLight::STATES::GREEN:
trafficLightDrawer_.drawGreen();
break;
default:
break;
}
}
void TrafficLight::handlePressedKey(char pressedKey)
{
switch (pressedKey)
{
case 'S':
case 's':
onStartButtonPressed();
break;
case 'P':
case 'p':
onPauseButtonPressed();
break;
case 'E':
case 'e':
onExitButtonPressed();
break;
default:
break;
}
}
void TrafficLight::onStartButtonPressed()
{
isStopped_ = false;
}
void TrafficLight::onPauseButtonPressed()
{
if (myTimer_.isTimerRunning())
{
isStopped_ = true;
timeRemainingUntilSwitch_ = myTimer_.stopTimer();
std::cout << "Paused. Time remaining: " + std::to_string(timeRemainingUntilSwitch_.count()) + "ms" << std::endl;
}
}
void TrafficLight::onExitButtonPressed()
{
isFinished_ = true;
}