-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualLoco.cpp
405 lines (332 loc) · 11 KB
/
VirtualLoco.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Definition of a loco's properties according to NMRA DCC
*/
#include "VirtualLoco.h"
#include <EEPROM.h>
#ifdef HL_DISP
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// I2C OLED display
extern Adafruit_SSD1306 display;
extern unsigned char imgOne16x16[];
extern unsigned char imgOneInverted16x16[];
#endif
// Constructor
VirtualLoco::VirtualLoco(void) {
address = 0;
id = "# 0";
}
VirtualLoco::VirtualLoco(unsigned int address) {
select(address);
}
VirtualLoco::VirtualLoco(String id, String &rosterList) {
select(id, rosterList);
}
// Destructor
VirtualLoco::~VirtualLoco(void) {
#ifdef HL_DISP
// hide symbol for 1 loco
display.drawBitmap(0, 0, imgOne16x16, 16, 16, OLED_COLOR_BLACK);
display.display();
#endif
#ifdef DEBUG
Serial.println("Loco with DCC address " + String(address) + " has beed rejected.");
#endif
}
// Initialize Class
// Set Prefix for WiThrottle server communication
void VirtualLoco::setPrefix(String cmdPrefix) {
this->cmdPrefix = cmdPrefix;
}
// DCC address
// Select loco by DCC address
void VirtualLoco::select(unsigned int address, bool updateID) {
// I want to check if loco has already been acquired
if (!acquired) {
this->address = address;
addressType = "S"; // Default
// I want to check if DCC address is a long one
if (address > 127) {
addressType = "L";
}
// I want to check if ID has to be updated
if (updateID) {
id = "# " + String(address);
}
initFunctions();
#ifdef HL_DISP
// show symbol for 1 loco
display.drawBitmap(0, 0, imgOne16x16, 16, 16, OLED_COLOR_WHITE);
display.display();
#endif
#ifdef DEBUG
Serial.println("Select loco by DCC address " + String(address) + ".");
#endif
}
else {
/*
* TODO
*/
}
}
// Select loco by ID
void VirtualLoco::select(String id, String &rosterList) {
char* tmpRosterList = (char*)""; // Roster list
String tmpRosterItem = (char*)""; // Roster item
const char* rosterItemDelim = "]\\["; // Delimiter betweed each roster item
const char* rosterItemInfoDelim = "}|{"; // Delimiter betweed each roster item
String tmpID = ""; // ID of roster item
unsigned int tmpAddress = 0; // DCC address of roster item
// I want to check if loco has already been acquired
if (!acquired) {
// Split different entries of roster list
tmpRosterList = strtok(const_cast<char*>(rosterList.c_str()), rosterItemDelim);
while (tmpRosterList != NULL) {
tmpRosterItem = String(tmpRosterList);
tmpID = tmpRosterItem.substring(0, tmpRosterItem.indexOf(rosterItemInfoDelim));
tmpRosterItem = tmpRosterItem.substring(tmpRosterItem.indexOf(rosterItemInfoDelim) + 3, tmpRosterItem.length());
tmpAddress = tmpRosterItem.substring(0, tmpRosterItem.indexOf(rosterItemInfoDelim)).toInt();
// tmpRosterItem = tmpRosterItem.substring(tmpRosterItem.indexOf(rosterItemInfoDelim) + 3, tmpRosterItem.length());
// tmpAddressType = tmpRosterItem;
// I want to check if ID matches
if (id == tmpID) {
this->id = id;
select(tmpAddress, false);
break;
}
tmpRosterList = strtok(NULL, rosterItemDelim);
}
}
else {
/*
* TODO
*/
}
}
// Get address of loco
unsigned int VirtualLoco::getAddress() {
return address;
}
// Get address type of the loco
String VirtualLoco::getAddressType() {
return addressType;
}
// Get description of the loco to send debugging information
String VirtualLoco::getDescription() {
String tmpDescription = "";
if (address != 0) {
tmpDescription = "DCC address: " + String(address);
if (id != "") {
tmpDescription = "ID: '" + id + "'; " + tmpDescription;
}
tmpDescription = "(" + tmpDescription + ")";
}
return tmpDescription;
}
// Direction control
// Set direction of the loco
void VirtualLoco::setDirection(int direction) {
// I want to check if setting the direction is possible
if (acquired && direction != IDLE) {
this->direction = direction;
#ifdef DEBUG
Serial.println("Set direction of loco " + getDescription() + " to " + directionTxt[direction] + ".");
#endif
sendCmd("M0A" + addressType + String(address) + "<;>R" + String(direction));
}
}
// Get direction of the loco
unsigned int VirtualLoco::getDirection() {
return direction;
}
// Notch control
// Set notch of the loco
void VirtualLoco::setNotch(int notch) {
if (acquired) {
if (notch == ESTOP) {
#ifdef DEBUG
Serial.println("Emergency stop loco " + getDescription() + ".");
#endif
this->notch = notch;
sendCmd("M0A" + addressType + String(address) + "<;>X");
}
else if ((this->notch != ESTOP && notch != this->notch) || (this->notch == ESTOP && notch == 0)) {
/*
* Compares reference value of DCC notch with actually
* set DCC notch; if different actual value is updated
* to reference value
*
* change notch only if loco has not been stopped for
* emergency
*
* if loco has been stopped for emergency notch has to
* be set to 0 first
*/
this->notch = notch;
#ifdef DEBUG
Serial.println("Set notch of loco " + getDescription() + " to " + String(notch) + ".");
#endif
sendCmd("M0A" + addressType + String(address) + "<;>V" + String(notch));
}
}
}
// Get notch of the loco
int VirtualLoco::getNotch() {
return this->notch;
}
// DCC functions
// Initialize DCC functions
void VirtualLoco::initFunctions() {
for(byte fn = 0; fn <= 28; fn++) {
function[fn].setFn(fn);
function[fn].setPrefix(cmdPrefix + "A" + getAddressType() + getAddress());
}
}
// Acquire and dispatch loco
// Acquire loco from WiThrottle server and assign to WiThrottle!
void VirtualLoco::acquire() {
bool useID = false; // Use ID of loco to acuire
// I want to check if acquisition is possible
if (!acquired && address != 0) {
#ifdef DEBUG
Serial.println("Acquire loco " + getDescription() + ".");
#endif
// I want to check if loco has to be acquired by ID
if (id.length() > 2) {
if (id.substring(0, 2) != "# ") {
useID = true;
}
}
if (useID) {
sendCmd("M0+" + addressType + String(address) + "<;>E" + id);
}
else {
sendCmd("M0+" + addressType + String(address) + "<;>" + addressType + String(address));
}
#ifdef HL_DISP
// show symbol for 1 loco
display.drawBitmap(0, 0, imgOne16x16, 16, 16, OLED_COLOR_BLACK);
display.drawBitmap(0, 0, imgOneInverted16x16, 16, 16, OLED_COLOR_WHITE);
display.display();
#endif
}
}
// Dispatch loco to WiThrottle server!
void VirtualLoco::dispatch() {
// I want to check if dispatch is possible
if (acquired) {
#ifdef DEBUG
Serial.println("Dispatch loco " + getDescription() + ".");
#endif
sendCmd("M0-" + addressType + String(address) + "<;>r");
select(0);
#ifdef HL_DISP
// hide symbol for 1 loco
display.drawBitmap(0, 0, imgOneInverted16x16, 16, 16, OLED_COLOR_BLACK);
display.drawBitmap(0, 0, imgOne16x16, 16, 16, OLED_COLOR_WHITE);
display.display();
#endif
}
}
// Get acquisition state of the loco
bool VirtualLoco::getAcquired() {
return acquired;
}
// WiThrottle server communication
// Use information sent by WiThrottle server to WiThrottle to update the DCC function's information about state and label
void VirtualLoco::listenToThrottle(String serverInfo) {
char cmdKey;
// I want to check if information belongs to this loco
if (serverInfo.indexOf(getAddressType() + String(getAddress()) + "<;>") >= 0) {
// Information belongs to this loco
cmdKey = serverInfo.charAt(0);
serverInfo = serverInfo.substring(serverInfo.indexOf("<;>") + 3, serverInfo.length());
switch(cmdKey) {
case '+':
// Add a locomotive to the throttle
acquired = true;
#ifdef DEBUG
Serial.println("Loco " + getDescription() + " has been acquired.");
#endif
break;
case '-':
// Remove a locomotive from the throttle
acquired = false;
#ifdef DEBUG
Serial.println("Loco " + getDescription() + " has been dispatched.");
#endif
break;
case 'A':
// Action. The following characters provide more details
cmdKey = serverInfo.charAt(0);
serverInfo = serverInfo.substring(1, serverInfo.length());
switch(cmdKey) {
case 'F':
// Function state information
for(byte fn = 0; fn <= 28; fn++) {
function[fn].listenToLoco(cmdKey + serverInfo);
}
break;
case 'R':
// Direction information
direction = serverInfo.toInt();
#ifdef DEBUG
Serial.println("Direction of loco " + getDescription() + " is " + directionTxt[direction] + ".");
#endif
break;
case 's':
// Speed step information
speedStepMode = serverInfo.toInt();
#ifdef DEBUG
Serial.println("Speed step mode of loco " + getDescription() + " is " + speedStepModeTxt[speedStepMode] + ".");
#endif
break;
case 'V':
// Notch information
notch = serverInfo.toInt();
if (serverInfo.toInt() == ESTOP) {
#ifdef DEBUG
Serial.println("Loco " + getDescription() + " has been stopped for emergency (Notch: " + String(notch) + ").");
#endif
}
else {
#ifdef DEBUG
Serial.println("Notch of loco " + getDescription() + " is " + String(notch) + ".");
#endif
}
break;
default:
// Unknown command
#ifdef DEBUG
Serial.println("Class VirtualLoco: Unknown command " + getAddressType() + String(address) + "<;>" + serverInfo);
#endif
/*
* TODO
*/
break;
}
break;
case 'L':
// Information according the loco's functions
for(byte fn = 0; fn <= 28; fn++) {
function[fn].listenToLoco(serverInfo);
}
break;
case 'S':
// Request steal locomotive
#ifdef DEBUG
Serial.println("Request steal locomotive: " + serverInfo);
#endif
/*
* TODO
*/
break;
default:
// Unknown command
#ifdef DEBUG
Serial.println("Class VirtualLoco: Unknown command '" + serverInfo + "'.");
#endif
break;
}
}
}