-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
48 lines (41 loc) · 1.37 KB
/
Controller.cpp
File metadata and controls
48 lines (41 loc) · 1.37 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
#include "Controller.h"
Controller::Controller(Train *train, int accelButtonPin, int decelButtonPin, int redLedPin, int greenLedPins[Train::SPEED_LEVELS], Signal* signals[NUM_REAL_SIGNALS]) {
this->train = train;
this->redLedPin = redLedPin;
pinMode(redLedPin, OUTPUT);
for (int i = 0; i < Train::SPEED_LEVELS; i++) {
this->greenLedPins[i] = greenLedPins[i];
pinMode(greenLedPins[i], OUTPUT);
}
accelButton = new PushSwitch(accelButtonPin, 200, HIGH);
decelButton = new PushSwitch(decelButtonPin, 200, HIGH);
this->signals = signals;
}
Controller::~Controller() {
delete accelButton;
delete decelButton;
delete greenLedPins;
}
void Controller::check() {
if (decelButton->check() == LOW) {
train->decelerate();
Serial.print("check: ");
Serial.print(decelButton->check());
Serial.print(", ");
Serial.print(accelButton->check());
Serial.print(" speed:");
Serial.println(train->getSpeed());
} else if (accelButton->check() == LOW) {
train->accelerate();
Serial.print("check: ");
Serial.print(decelButton->check());
Serial.print(", ");
Serial.print(accelButton->check());
Serial.print(" speed:");
Serial.println(train->getSpeed());
}
for (int i = 0; i < Train::SPEED_LEVELS; i++) {
digitalWrite(greenLedPins[i], i < train->getSpeed());
}
digitalWrite(redLedPin, train->isRestricted());
}