-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.cpp
More file actions
84 lines (70 loc) · 1.57 KB
/
Copy pathTrain.cpp
File metadata and controls
84 lines (70 loc) · 1.57 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Train.h"
Train::Train(TrainType type, Block pos, int pwmPinA, int pwmPinB) {
this->type = type;
setPos(pos);
this->speed = MIN_SPEED;
restricted = false;
this->pwmPinA = pwmPinA;
this->pwmPinB = pwmPinB;
pinMode(pwmPinA, OUTPUT);
pinMode(pwmPinB, OUTPUT);
}
TrainType Train::getType() {
return type;
}
Block Train::getPos() {
return pos;
}
void Train::setPos(Block pos) {
this->pos = pos;
}
const int Train::pwmDuty[] = {0, 144, 192, 255};
//const int Train::pwmDuty[] = {0, 255, 255, 255};
int Train::getSpeed() {
return this->speed;
}
void Train::setSpeed(int speed) {
if (isRestricted()) {
speed = MIN_SPEED;
}
if (MIN_SPEED <= speed && speed <= MAX_SPEED) {
this->speed = speed;
if (speed == MIN_SPEED) {
analogWrite(pwmPinA, 255);
analogWrite(pwmPinB, 255);
} else {
analogWrite(pwmPinB, 0);
analogWrite(pwmPinA, pwmDuty[speed]);
}
}
}
void Train::accelerate() {
setSpeed(this->speed + 1);
}
void Train::decelerate() {
setSpeed(this->speed - 1);
}
boolean Train::isRestricted() {
return restricted;
}
void Train::restrict() {
restricted = true;
setSpeed(MIN_SPEED);
print();
Serial.println(" restricted");
}
void Train::release() {
restricted = false;
print();
Serial.println(" released");
}
void Train::print() {
Serial.print(getType() == EXPRESS ? "Express" : "Rapid");
Serial.print("[");
Serial.print(isRestricted() ? "RESTRICTED" : "released");
Serial.print(",speed=");
Serial.print(getSpeed());
Serial.print(",pos=");
Serial.print(getPos());
Serial.print("]");
}