-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathThrottle.cpp
116 lines (96 loc) · 2.83 KB
/
Throttle.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
#include <DCCEXProtocol.h>
#include "Throttle.h"
#if DCCEXCONTROLLER_DEBUG == 0
#define debug_print(params...) Serial.print(params)
#define debug_println(params...) Serial.print(params); Serial.print(" ("); Serial.print(millis()); Serial.println(")")
#define debug_printf(params...) Serial.printf(params)
#else
#define debug_print(...)
#define debug_println(...)
#define debug_printf(...)
#endif
Throttle::Throttle(DCCEXProtocol* dccexProtocol) {
_dccexProtocol=dccexProtocol;
_consist = new Consist();
}
void Throttle::addLoco(Loco* loco, Facing facing) {
if (!_consist) return;
_consist->addLoco(loco, facing);
}
void Throttle::removeLoco(Loco* loco) {
if (!_consist) return;
_consist->removeLoco(loco);
}
void Throttle::removeLoco(int address) {
if (!_consist) return;
ConsistLoco* _conLoco = _consist->getByAddress(address);
if (_conLoco) {
_consist->removeLoco(_conLoco->getLoco());
}
}
void Throttle::removeAllLocos() {
if (!_consist) return;
_consist->removeAllLocos();
}
Consist* Throttle::getConsist() {
if (!_consist) return nullptr;
return _consist;
}
int Throttle::getLocoCount() {
if (!_consist) return 0;
return _consist->getLocoCount();
}
Loco* Throttle::getByAddress(int address) {
if (!_consist) return nullptr;
return _consist->getByAddress(address)->getLoco();
}
ConsistLoco* Throttle::getConLocoByAddress(int address) {
if (!_consist) return nullptr;
return _consist->getByAddress(address);
}
ConsistLoco* Throttle::getFirst() {
if (!_consist) return nullptr;
return _consist->getFirst();
}
ConsistLoco* Throttle::getLocoAtPosition(int index) {
if (!_consist) return nullptr;
if ((_consist->getLocoCount()-1) < index) return nullptr;
int i = 0;
for (ConsistLoco* cl=_consist->getFirst(); cl; cl=cl->getNext()) {
if (i == index) {
return cl;
}
i++;
}
return nullptr;
}
void Throttle::setSpeed(int speed) {
if (!_consist) return;
_dccexProtocol->setThrottle(_consist, speed, _consist->getDirection());
}
Direction Throttle::getDirection() {
if (!_consist) return Forward;
return _consist->getDirection();
}
void Throttle::setDirection(Direction direction) {
if (!_consist) return;
_dccexProtocol->setThrottle(_consist, _consist->getSpeed(), direction);
}
Facing Throttle::getLocoFacing(int address) {
if (!_consist) return FacingForward;
return _consist->getByAddress(address)->getFacing();
}
void Throttle::setLocoFacing(int address, Facing facing) {
if (!_consist) return;
_consist->setLocoFacing(_consist->getByAddress(address)->getLoco(), facing);
}
void Throttle::setFunction(Loco* loco, int function, bool state) {
if (state) {
_dccexProtocol->functionOn(loco, function);
} else {
_dccexProtocol->functionOff(loco, function);
}
}
void Throttle::process() {
// Routine calls here included in the loop to read encoder or other inputs
}