-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
274 lines (245 loc) · 7.73 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Traffic light simulation with pedestrian crossing button.
The traffic light has three states: green, yellow, and red.
The traffic light stays green until the pedestrian crossing button is pressed via keyboard or
simulated button press. The pedestrian crossing button is pressed every 20-30 seconds to simulate
crossing. When the button is pressed, the traffic light changes to red and the pedestrian can
cross. The pedestrian crossing takes 20 seconds. The traffic light changes to green after the
pedestrian has crossed. Button press can also be simulated by pressing any key. The program can
be exited by pressing 'q'.
*/
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <random>
#include <string>
#include <thread>
#ifdef _WIN32
# include <conio.h>
#else
# include <termios.h>
# include <unistd.h>
#endif
std::mutex mtx;
std::condition_variable cv;
std::atomic<bool> buttonPress(false);
std::atomic<bool> isRunning(true);
std::atomic<bool> pedestrianCrossing(false);
// Traffic light times in seconds
constexpr int GREEN_TIME = 10;
constexpr int YELLOW_TIME = 3;
constexpr int RED_TIME = 10;
constexpr int BUTTON_TIME = 20;
enum class State
{
GREEN,
YELLOW_AFTER_GREEN,
RED,
YELLOW_AFTER_RED
};
enum class PedestrianState
{
GREEN,
YELLOW_AFTER_GREEN,
RED,
YELLOW_AFTER_RED
};
void buttonSimulator(const std::atomic_bool* isRunning, std::atomic_bool* buttonPress,
std::mutex* mtx, std::condition_variable* cv,
std::atomic_bool* pedestrianCrossing);
void trafficLight();
void keyboardHandler();
int main()
{
std::thread tButton(buttonSimulator, &isRunning, &buttonPress, &mtx, &cv, &pedestrianCrossing);
std::thread tTrafficLight(trafficLight);
std::thread tKeyboardHandler(keyboardHandler);
tKeyboardHandler.join();
isRunning = false;
cv.notify_all();
tButton.join();
tTrafficLight.join();
return 0;
}
// Sleep function that checks for interrupt every 100ms for fast keyboard response
// Second is multiplied by 10 to get the number of 100ms intervals for ish seconds.
void sleepWithInterrupt(int seconds)
{
for (int i = 0; i < seconds * 10; ++i)
{
if (!isRunning)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (buttonPress)
break;
}
}
// Keyboard handling for Windows and Mac/Linux, q for exit, any other key for button press
void keyboardHandler()
{
#ifdef _WIN32
while (isRunning)
{
if (_kbhit())
{
// Get the key press without echoing it to the console
char ch = _getch();
if (ch == 'q')
{
isRunning = false;
cv.notify_all();
}
else if (!pedestrianCrossing)
{
buttonPress = true;
cv.notify_all();
}
}
}
// Reduce CPU usage
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
#else
// Save the current terminal settings and disable canonical mode and echo
termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
while (isRunning)
{
char ch = getchar();
if (ch == 'q')
{
isRunning = false;
cv.notify_all();
}
else if (!pedestrianCrossing)
{
buttonPress = true;
cv.notify_all();
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#endif
}
// Display the current state of the traffic light
void displayLight(const State& state, const PedestrianState& pedState)
{
std::string strState;
std::string strPedState;
switch (state)
{
case State::GREEN:
strState = "\033[32mGREEN\033[0m";
break;
case State::YELLOW_AFTER_GREEN:
strState = "\033[33mYELLOW\033[0m";
break;
case State::YELLOW_AFTER_RED:
strState = "\033[33mYELLOW\033[0m";
break;
case State::RED:
strState = "\033[31mRED\033[0m";
break;
}
switch (pedState)
{
case PedestrianState::GREEN:
strPedState = "\033[32mGREEN\033[0m";
break;
case PedestrianState::YELLOW_AFTER_GREEN:
strPedState = "\033[33mYELLOW\033[0m";
break;
case PedestrianState::YELLOW_AFTER_RED:
strPedState = "\033[33mYELLOW\033[0m";
break;
case PedestrianState::RED:
strPedState = "\033[31mRED\033[0m";
break;
}
std::lock_guard<std::mutex> lck(mtx);
std::cout << "Traffic light state: " << strState << " Pedestrian light state: " << strPedState
<< (pedestrianCrossing ? " (Pedestrian crossing)" : "") << "\n";
}
void trafficLight()
{
// Set initial state to green
State state = State::GREEN;
PedestrianState pedState = PedestrianState::RED;
while (isRunning)
{
// Wait for button press or state change
bool buttonHandled = false; // Avoid race condition and change state only once
{
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [] { return buttonPress || isRunning; });
if (buttonPress && state != State::RED && !pedestrianCrossing)
{
buttonHandled = true;
buttonPress = false;
state = State::YELLOW_AFTER_GREEN;
pedState = PedestrianState::YELLOW_AFTER_RED;
pedestrianCrossing = true;
std::cout << "Button is pressed.\n";
}
}
// Don't change state if the program is exiting
if (!isRunning)
break;
displayLight(state, pedState);
// Change state based on current state
switch (state)
{
case State::GREEN:
sleepWithInterrupt(GREEN_TIME);
if (pedestrianCrossing)
{
state = State::YELLOW_AFTER_GREEN;
pedState = PedestrianState::YELLOW_AFTER_GREEN;
}
break;
case State::YELLOW_AFTER_GREEN:
sleepWithInterrupt(YELLOW_TIME);
state = State::RED;
pedState = PedestrianState::GREEN;
break;
case State::RED:
sleepWithInterrupt(BUTTON_TIME);
state = State::YELLOW_AFTER_RED;
pedState = PedestrianState::YELLOW_AFTER_GREEN;
break;
case State::YELLOW_AFTER_RED:
sleepWithInterrupt(YELLOW_TIME);
state = State::GREEN;
pedState = PedestrianState::RED;
pedestrianCrossing = false;
break;
}
}
}
void buttonSimulator(const std::atomic_bool* isRunning, std::atomic_bool* buttonPress,
std::mutex* mtx, std::condition_variable* cv,
std::atomic_bool* pedestrianCrossing)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(20, 30);
while (*isRunning)
{
int randNum = dist(gen);
if (!isRunning->load())
break;
// std::this_thread::sleep_for(std::chrono::seconds(randNum));
sleepWithInterrupt(randNum);
// If the pedestrian is crossing, don't press the button
if (!pedestrianCrossing->load())
{
std::lock_guard<std::mutex> lck(*mtx);
*buttonPress = true;
cv->notify_all();
}
}
}