forked from DCC-EX/CommandStation-EX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EXComm.cpp
288 lines (257 loc) · 6.76 KB
/
EXComm.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
/*
* © 2024 Thierry Paris
* All rights reserved.
*
* This file is part of CommandStation-EX-Labox
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "defines.h"
#include "DCCEXParser.h"
#include "hmi.h"
#ifdef ENABLE_EXCOMM
#include "LaboxModes.h"
#include "EXComm.h"
#include "EXCommItems.h"
bool EXComm::DIAGBASE = false;
#define DIAG_EXCOMM if (EXComm::DIAGBASE) DIAG
byte EXComm::lastItem = 0;
byte EXComm::nextCycleItem = MAX_COMMITEMS;
EXCommItem* EXComm::commItems[MAX_COMMITEMS];
String *EXComm::pInfos = NULL;
int EXComm::infosCount = 0;
void EXComm::Setup() {
lastItem = 0;
SetupPrivate(LABOX_EXCOMMS);
nextCycleItem = MAX_COMMITEMS;
}
// The setup call is done this way so that the tracks can be in a list
// from the config... the tracks default to NULL in the declaration
void EXComm::SetupPrivate(
EXCommItem * comm0, EXCommItem * comm1, EXCommItem * comm2,
EXCommItem * comm3, EXCommItem * comm4, EXCommItem * comm5,
EXCommItem * comm6, EXCommItem * comm7 ) {
addItem(0, comm0);
addItem(1, comm1);
addItem(2, comm2);
addItem(3, comm3);
addItem(4, comm4);
addItem(5, comm5);
addItem(6, comm6);
addItem(7, comm7);
}
bool EXComm::addItem(byte t, EXCommItem* apComm) {
if (t < MAX_COMMITEMS) {
if (apComm) {
if ((LaboxModes::progMode && apComm->ProgTrackEnabled) ||
(!LaboxModes::progMode && apComm->MainTrackEnabled)) {
commItems[t] = apComm;
lastItem = t;
return true;
}
else {
DIAG(F("[%s] item not applicable in this mode."), apComm->getName());
}
}
commItems[t] = NULL;
}
return false;
}
void EXComm::begin() {
//printItems();
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL) {
//DIAG(F("%d %s"), i, commItems[i]->name);
commItems[i]->begin();
}
}
}
void EXComm::loop() {
// Execute 'AlwaysLoop' items
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL && commItems[i]->AlwaysLoop) {
commItems[i]->loop();
}
}
// Execute this cycle item
nextCycleItem++;
if (nextCycleItem > lastItem)
nextCycleItem = 0;
if (commItems[nextCycleItem] == NULL)
return;
// If not already done in the previous for !
if (!commItems[nextCycleItem]->AlwaysLoop)
commItems[nextCycleItem]->loop();
}
void EXComm::broadcast(byte *com)
{
DIAG_EXCOMM(F("[EXCOMM] broadcast : %s"), com);
int16_t p[DCCEXParser::MAX_COMMAND_PARAMS];
while (com[0] == '<' || com[0] == ' ')
com++; // strip off any number of < or spaces
byte opcode = com[0];
byte params = DCCEXParser::splitValues(p, com, false);
switch (opcode)
{
case 'l': // LOCO <l CAB SPEED DIRECTION>
DIAG_EXCOMM(F("[EXCOMM] broadcast loco"));
if (params >= 0) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL) {
commItems[i]->broadcastLoco(p[0]);
}
}
}
return;
case 'H': // TURNOUT <H ADDRESS ACTIVATE>
DIAG_EXCOMM(F("[EXCOMM] broadcast turnout"));
if (params >= 1) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL) {
commItems[i]->broadcastTurnout(p[0], p[1]);
}
}
}
return;
case 'j': // CLOACKTIME <jC TIME RATE>
DIAG_EXCOMM(F("[EXCOMM] broadcast clock time"));
if (params >= 1) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL) {
commItems[i]->broadcastClockTime(p[0], p[1]);
}
}
}
return;
case 'p': // POWER <px name> where x is 0 or 1
DIAG_EXCOMM(F("[EXCOMM] broadcast power"));
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL) {
commItems[i]->broadcastPower();
}
}
return;
case 'Q': // SENSOR ON
DIAG_EXCOMM(F("[EXCOMM] broadcast sensor ON"));
if (params >= 0) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL) {
commItems[i]->broadcastSensor(p[0], 1);
}
}
}
return;
case 'q': // SENSOR OFF
DIAG_EXCOMM(F("[EXCOMM] broadcast sensor OFF"));
if (params >= 0) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL) {
commItems[i]->broadcastSensor(p[0], 0);
}
}
}
return;
}
}
void EXComm::sendPower(bool iSOn) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL)
commItems[i]->sendPower(iSOn);
}
}
void EXComm::sendThrottle(uint16_t cab, uint8_t tSpeed, bool tDirection) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL)
commItems[i]->sendThrottle(cab, tSpeed, tDirection);
}
}
void EXComm::sendFunction(uint16_t cab, int16_t functionNumber, bool on) {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL)
commItems[i]->sendFunction(cab, functionNumber, on);
}
}
void EXComm::sendEmergency() {
for (int i = 0; i <= lastItem; i++) {
if (commItems[i] != NULL)
commItems[i]->sendEmergency();
}
}
void EXComm::print() {
DIAG(F("EXComm items:"));
int i = 0;
for (; i < MAX_COMMITEMS;) {
if (commItems[i] == NULL) {
DIAG(F(" %d : NULL"), i);
}
else {
DIAG(F(" %d : %s"), i, commItems[i]->name);
}
i++;
}
DIAG(F("Lastitem: %d"), lastItem);
}
int EXComm::getAllInfo(byte maxSize)
{
if (pInfos == NULL)
{
pInfos = new String[(lastItem + 1) * 2];
String mess1, mess2, mess3;
for(int i = 0; i <= lastItem; i++)
{
if (commItems[i] == NULL)
continue;
mess1.clear();
mess2.clear();
mess3.clear();
commItems[i]->getInfos(&mess1, &mess2, &mess3, maxSize);
if (mess1.length() <= maxSize && mess1.length() > 0)
pInfos[infosCount++] = mess1;
else
{
if (mess1.length() > 0)
{
Serial.print("[");
Serial.print(commItems[i]->getName());
Serial.println("] info1 too long");
}
}
if (mess2.length() <= maxSize && mess2.length() > 0)
pInfos[infosCount++] = mess2;
else
{
if (mess2.length() > 0)
{
Serial.print("[");
Serial.print(commItems[i]->getName());
Serial.println("] info2 too long");
}
}
if (mess3.length() <= maxSize && mess3.length() > 0)
pInfos[infosCount++] = mess3;
else
{
if (mess3.length() > 0)
{
Serial.print("[");
Serial.print(commItems[i]->getName());
Serial.println("] info3 too long");
}
}
}
}
return infosCount;
}
#endif