Repository navigation
Expand file tree
/
Copy pathDepartureSignal.cpp
More file actions
64 lines (56 loc) · 1.85 KB
/
Copy pathDepartureSignal.cpp
File metadata and controls
64 lines (56 loc) · 1.85 KB
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
#include "DepartureSignal.h"
DepartureSignal::DepartureSignal(Block block, Train *initialTrain, State initialState, int preCdsPin, int mainCdsPin, int signalPin, long stopMillis) :
Signal(block, initialTrain, initialState, preCdsPin, mainCdsPin, signalPin) {
Serial.println("departure signal");
this->stopMillis = stopMillis;
}
boolean DepartureSignal::check() {
// check if the next train is approaching
const int pre = preCds->check();
const int main = mainCds->check();
if (pre == HIGH && main == LOW && state == RUNNING) {
setApproaching();
existingTrain->setSpeed(1);
}
if (pre == HIGH && main == HIGH && state == APPROACHING) {
Serial.println("stop!");
Serial.println(stopMillis);
setPassing();
lastStoppedMillis = millis();
}
if (stopMillis < millis() - lastStoppedMillis) {
existingTrain->setSpeed(1);
} else {
existingTrain->setSpeed(0);
}
if (state == PASSING && pre == LOW && main == LOW && nextSignal->canEnter(existingTrain)) { // last condition for noise tolerant
setNoTrain();
}
// change the signal
if (restricted || !nextSignal->canEnter(existingTrain)) {
// lights red
color = RED;
if (existingTrain != NULL && state == PASSING) {
existingTrain->restrict();
}
} else {
// lights green
color = GREEN;
if (existingTrain != NULL && existingTrain->isRestricted()) {
existingTrain->release();
}
}
digitalWrite(signalPin, color);
boolean stateChanged = state != previousState;
// boolean stateChanged = state != previousState || lastPre != pre || lastMain != main;
// lastPre = pre;
// lastMain = main;
// if (stateChanged) print(pre, main);
// save current state for next check()
previousState = state;
return stateChanged;
}
boolean DepartureSignal::enter(Train *train) {
train->setSpeed(1);
return Signal::enter(train);
}