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
|
#include <stdint.h>
#include <stdbool.h>
#include "rvcontroller-ecalls.h"
bool streq(char *a, char *b) {
for (int i=0;a[i] != 0 || b[i] != 0;i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
void sleep(uint32_t delay) {
//This will start having problems after about 130 years
//of uptime, but that's probably acceptable
uint32_t endtime = rdtime() + delay;
while (rdtime() < endtime) {}
return;
}
bool checkdetector(void) {
char channelbuf[16];
char msgbuf[16];
while (digiline_bufferlevel() > 0) {
lightweight_mode(0);
digiline_receive(channelbuf,16,msgbuf,16);
if (streq(channelbuf,"detect")) {
return true;
}
}
lightweight_mode(1);
return false;
}
void main() {
#ifdef RRXING_DE
printstr("DE Railroad Crossing\n");
#elif RRXING_UK
printstr("UK Railroad Crossing\n");
#elif RRXING_US
printstr("US Railroad Crossing\n");
#endif
printstr("for RVController\nInitializing HW\n");
digiline_send("light","OFF");
digiline_send("bell","off");
digiline_send("gate","up");
digiline_send("signal","RED");
digiline_clearbuffer();
while (true) {
printstr("Idle\n");
lightweight_mode(1);
printstr("Lightweight: On\n");
while (!checkdetector()) {}
printstr("Train detected\nLightweight: Off\n");
lightweight_mode(0);
#ifdef RRXING_DE
digiline_send("light","YELLOW");
printstr("Light: Yellow\n");
sleep(2);
digiline_send("bell","on");
printstr("Bell: On\n");
sleep(2);
digiline_send("light","RED");
printstr("Light: Red\n");
sleep(3);
digiline_send("gate","down");
printstr("Gate: Down\n");
sleep(4);
digiline_send("bell","off");
printstr("Bell: Off\n");
#elif RRXING_UK
digiline_send("bell","on");
printstr("Bell: On\n");
digiline_send("light","YELLOW");
printstr("Light: Yellow\n");
sleep(3);
digiline_send("light","RED");
printstr("Light: Red\n");
sleep(1);
digiline_send("light","FLASHRED");
printstr("Light: Flash Red\n");
sleep(5);
digiline_send("gate","down");
printstr("Gate: Down\n");
sleep(4);
#elif RRXING_US
digiline_send("light","FLASHRED");
printstr("Light: Flash Red\n");
digiline_send("bell","on");
printstr("Bell: On\n");
sleep(4);
digiline_send("gate","down");
printstr("Gate: Down\n");
sleep(4);
#else
#error No country selected
#endif
digiline_send("signal","GREEN");
printstr("Signal: Green\n");
digiline_clearbuffer();
for (uint8_t time = 15;time > 0;time--) {
printstr("Timeout in ");
printint(time);
printstr("s\n");
sleep(1);
bool train = checkdetector();
lightweight_mode(0); //checkdetector() turns this on
if (train) {
digiline_clearbuffer();
time = 25;
printstr("Time reset by train\n");
}
}
printstr("Timed out\n");
digiline_send("signal","RED");
printstr("Signal: Red\n");
#if defined(RRXING_UK) || defined(RRXING_US)
digiline_send("bell","off");
printstr("Bell: Off\n");
sleep(1);
#endif
digiline_send("gate","up");
printstr("Gate: Up\n");
sleep(2);
digiline_send("light","OFF");
printstr("Light: Off\n");
}
}
|