-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
120 lines (97 loc) · 3.91 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
#include <chrono>
#include <memory>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "Systems/sequenced_interruptable_system.h"
#include "Systems/single_interruptable_crossing_system.h"
#include "Systems/na_stop_give_way_system.h"
#include "Systems/light_test_system.h"
#include "trafficlight.h"
std::shared_ptr<SequencedInterruptableSystem> _standardSystem;
std::shared_ptr<SingleInterruptableCrossingSystem> _flashingCrossingSystem, _standardCrossingSystem;
std::shared_ptr<NAStopGiveWaySystem> _stopGiveWaySystem;
std::shared_ptr<LightTestSystem> _lightTestSystem;
void runSequencedSystem(std::vector<std::shared_ptr<TrafficLight>> trafficLights)
{
if (!_standardSystem) {
_standardSystem = std::make_shared<SequencedInterruptableSystem>(trafficLights, SequencedInterruptableSystem::SequenceType::Auto);
_standardSystem->setTiming(SequencedInterruptableSystemTimings::MinimumTimeUntilRedLight, std::chrono::seconds(6), 0);
_standardSystem->setTiming(SequencedInterruptableSystemTimings::MinimumTimeUntilRedLight, std::chrono::seconds(2), 1);
_standardSystem->setTiming(SequencedInterruptableSystemTimings::CrossingTime, std::chrono::seconds(6), 0);
_standardSystem->setTiming(SequencedInterruptableSystemTimings::CrossingTime, std::chrono::seconds(3), 1);
}
_standardSystem->requestCrossing();
_standardSystem->run();
}
void runFlashingCrossingSystem(std::vector<std::shared_ptr<TrafficLight>> trafficLights)
{
if (!_flashingCrossingSystem) {
_flashingCrossingSystem = std::make_shared<SingleInterruptableCrossingSystem>(trafficLights, SingleInterruptableCrossingSystem::CrossingStyle::Flashing);
}
_flashingCrossingSystem->requestCrossing();
_flashingCrossingSystem->run();
}
void runStandardCrossingSystem(std::vector<std::shared_ptr<TrafficLight>> trafficLights)
{
if (!_standardCrossingSystem) {
_standardCrossingSystem = std::make_shared<SingleInterruptableCrossingSystem>(trafficLights, SingleInterruptableCrossingSystem::CrossingStyle::Standard);
}
_standardCrossingSystem->requestCrossing();
_standardCrossingSystem->run();
}
void runNAStopGiveWaySystem(std::vector<std::shared_ptr<TrafficLight>> trafficLights)
{
if (!_stopGiveWaySystem) {
_stopGiveWaySystem = std::make_shared<NAStopGiveWaySystem>(trafficLights, 1u);
}
_stopGiveWaySystem->run();
}
void runTestSystem(std::vector<std::shared_ptr<TrafficLight>> trafficLights)
{
if (!_lightTestSystem) {
_lightTestSystem = std::make_shared<LightTestSystem>(trafficLights);
}
_lightTestSystem->run();
}
void lightsThread()
{
auto northTrafficLight = std::make_shared<TrafficLight>(0u, 1u, 2u, 3u, 4u, TrafficLight::LedType::CommonAnode);
auto southTrafficLight = std::make_shared<TrafficLight>(5u, 6u, 7u, 8u, 9u, TrafficLight::LedType::CommonAnode);
std::vector<std::shared_ptr<TrafficLight>> trafficLights = { northTrafficLight, southTrafficLight };
while(true) {
runTestSystem(trafficLights);
for (auto loops = 2; loops > 0; --loops) {
runSequencedSystem(trafficLights);
}
runTestSystem(trafficLights);
runFlashingCrossingSystem(trafficLights);
runTestSystem(trafficLights);
runStandardCrossingSystem(trafficLights);
runTestSystem(trafficLights);
runNAStopGiveWaySystem(trafficLights);
}
}
void initPin(uint pinId, int direction = GPIO_OUT) {
gpio_init(pinId);
gpio_set_dir(pinId, GPIO_OUT);
}
void inputsThread()
{
initPin(13, GPIO_IN);
initPin(PICO_DEFAULT_LED_PIN);
while (true) {
if (gpio_get(13)) {
if (_standardSystem) {
_standardSystem->requestCrossing();
//_standardSystem->requestNextGroup();
}
sleep_ms(1000);
}
sleep_ms(100);
}
}
int main()
{
multicore_launch_core1(&inputsThread);
lightsThread();
}