-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxn-commands.h
528 lines (445 loc) · 15.3 KB
/
xn-commands.h
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#ifndef XN_COMMANDS
#define XN_COMMANDS
/*
This file defines XpressNET commands.
Each command type has its own class inherited from XnCmd.
See xn.h or README for more documentation.
*/
#include <functional>
#include <vector>
#include "q-str-exception.h"
#include "xn-loco-addr.h"
namespace Xn {
struct EInvalidCv : public QStrException {
EInvalidCv(const QString str) : QStrException(str) {}
};
struct EInvalidSpeed : public QStrException {
EInvalidSpeed(const QString str) : QStrException(str) {}
};
struct Cmd {
virtual std::vector<uint8_t> getBytes() const = 0;
virtual QString msg() const = 0;
virtual ~Cmd() = default;
virtual bool conflict(const Cmd &) const { return false; }
virtual bool okResponse() const { return false; }
};
template <typename Target>
bool is(const Cmd &x) {
return (dynamic_cast<const Target *>(&x) != nullptr);
}
///////////////////////////////////////////////////////////////////////////////
struct CmdOff : public Cmd {
std::vector<uint8_t> getBytes() const override { return {0x21, 0x80}; }
QString msg() const override { return "Track Off"; }
};
struct CmdOn : public Cmd {
std::vector<uint8_t> getBytes() const override { return {0x21, 0x81}; }
QString msg() const override { return "Track On"; }
bool conflict(const Cmd &cmd) const override { return is<CmdOff>(cmd); }
};
struct CmdEmergencyStop : public Cmd {
std::vector<uint8_t> getBytes() const override { return {0x80}; }
QString msg() const override { return "All Loco Emergency Stop"; }
};
struct CmdEmergencyStopLoco : public Cmd {
const LocoAddr loco;
CmdEmergencyStopLoco(const LocoAddr loco) : loco(loco) {}
std::vector<uint8_t> getBytes() const override { return {0x92, loco.hi(), loco.lo()}; }
QString msg() const override { return "Single Loco Emergency Stop : " + QString::number(loco); }
bool okResponse() const override { return true; }
};
///////////////////////////////////////////////////////////////////////////////
using GotLIVersion = std::function<void(void *sender, unsigned hw, unsigned sw)>;
struct CmdGetLIVersion : public Cmd {
GotLIVersion const callback;
CmdGetLIVersion(GotLIVersion const callback) : callback(callback) {}
std::vector<uint8_t> getBytes() const override { return {0xF0}; }
QString msg() const override { return "LI Get Version"; }
};
using GotLIAddress = std::function<void(void *sender, unsigned addr)>;
struct CmdGetLIAddress : public Cmd {
GotLIAddress const callback;
CmdGetLIAddress(GotLIAddress const callback) : callback(callback) {}
std::vector<uint8_t> getBytes() const override { return {0xF2, 0x01, 0x00}; }
QString msg() const override { return "LI Get Address"; }
};
struct CmdSetLIAddress : public Cmd {
const unsigned addr;
CmdSetLIAddress(const unsigned addr) : addr(addr) {}
std::vector<uint8_t> getBytes() const override {
return {0xF2, 0x01, static_cast<uint8_t>(addr)};
}
QString msg() const override { return "LI Set Address to " + QString::number(addr); }
bool conflict(const Cmd &cmd) const override { return is<CmdSetLIAddress>(cmd); }
};
using GotCSVersion = std::function<void(void *sender, unsigned major, unsigned minor, uint8_t id)>;
struct CmdGetCSVersion : public Cmd {
GotCSVersion const callback;
CmdGetCSVersion(GotCSVersion const callback) : callback(callback) {}
std::vector<uint8_t> getBytes() const override { return {0x21, 0x21}; }
QString msg() const override { return "Get Command station version"; }
};
struct CmdGetCSStatus : public Cmd {
std::vector<uint8_t> getBytes() const override { return {0x21, 0x24}; }
QString msg() const override { return "Get Command station status"; }
};
///////////////////////////////////////////////////////////////////////////////
struct CmdPomWriteCv : public Cmd {
const LocoAddr loco;
const uint16_t cv;
const uint8_t value;
CmdPomWriteCv(const LocoAddr loco, const uint16_t cv, const uint8_t value)
: loco(loco), cv(cv), value(value) {
if (cv > 1023)
throw EInvalidCv("CV value is too high!");
}
std::vector<uint8_t> getBytes() const override {
return {
0xE6, 0x30, loco.hi(), loco.lo(),
static_cast<uint8_t>(0xEC + (((cv-1) >> 8) & 0x03)),
static_cast<uint8_t>((cv-1) & 0xFF), value
};
}
QString msg() const override {
return "POM Addr " + QString::number(loco.addr) + ", CV " + QString::number(cv) +
", Value: " + QString::number(value);
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdPomWriteCv>(cmd)) {
const auto &casted = dynamic_cast<const CmdPomWriteCv &>(cmd);
return ((casted.loco == this->loco) && (casted.cv == this->cv));
}
return false;
}
bool okResponse() const override { return true; }
};
struct CmdPomWriteBit : public Cmd {
const LocoAddr loco;
const uint16_t cv;
const uint8_t biti;
const bool value;
CmdPomWriteBit(const LocoAddr loco, const uint16_t cv, const unsigned biti, const bool value)
: loco(loco), cv(cv), biti(biti), value(value) {
if (cv > 1023)
throw EInvalidCv("CV value is too high!");
}
std::vector<uint8_t> getBytes() const override {
return {0xE6, 0x30, loco.hi(), loco.lo(),
static_cast<uint8_t>(0xE8 + (((cv-1) >> 8) & 0x03)),
static_cast<uint8_t>((cv-1) & 0xFF),
static_cast<uint8_t>(0xF0 + (value << 3) + biti)};
}
QString msg() const override {
return "POM Addr " + QString::number(loco.addr) + ", CV " + QString::number(cv) +
", Bit: " + QString::number(biti) + ", Value: " + QString::number(value);
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdPomWriteBit>(cmd)) {
const auto &casted = dynamic_cast<const CmdPomWriteBit &>(cmd);
return ((casted.loco == this->loco) && (casted.cv == this->cv) &&
(casted.biti == this->biti));
}
if (is<CmdPomWriteCv>(cmd)) {
const auto &casted = dynamic_cast<const CmdPomWriteCv &>(cmd);
return ((casted.loco == this->loco) && (casted.cv == this->cv));
}
return false;
}
bool okResponse() const override { return true; }
};
///////////////////////////////////////////////////////////////////////////////
union FA {
uint8_t all;
struct {
bool f1 : 1;
bool f2 : 1;
bool f3 : 1;
bool f4 : 1;
bool f0 : 1;
bool _ : 3;
} sep;
FA(uint8_t fa) : all(fa) {}
FA() : all(0) {}
};
union FB {
uint8_t all;
struct {
bool f5 : 1;
bool f6 : 1;
bool f7 : 1;
bool f8 : 1;
bool f9 : 1;
bool f10 : 1;
bool f11 : 1;
bool f12 : 1;
} sep;
FB(uint8_t fb) : all(fb) {}
FB() : all(0) {}
};
union FC {
uint8_t all;
struct {
bool f13 : 1;
bool f14 : 1;
bool f15 : 1;
bool f16 : 1;
bool f17 : 1;
bool f18 : 1;
bool f19 : 1;
bool f20 : 1;
} sep;
FC(uint8_t fc) : all(fc) {}
FC() : all(0) {}
};
union FD {
uint8_t all;
struct {
bool f21 : 1;
bool f22 : 1;
bool f23 : 1;
bool f24 : 1;
bool f25 : 1;
bool f26 : 1;
bool f27 : 1;
bool f28 : 1;
} sep;
FD(uint8_t fd) : all(fd) {}
FD() : all(0) {}
};
enum class Direction {
Backward = false,
Forward = true,
};
using GotLocoInfo = std::function<void(void *sender, bool used, Direction direction,
unsigned speed, FA fa, FB fb)>;
struct CmdGetLocoInfo : public Cmd {
const LocoAddr loco;
GotLocoInfo const callback;
CmdGetLocoInfo(const LocoAddr loco, GotLocoInfo const callback)
: loco(loco), callback(callback) {}
std::vector<uint8_t> getBytes() const override { return {0xE3, 0x00, loco.hi(), loco.lo()}; }
QString msg() const override { return "Get Loco Information " + QString::number(loco.addr); }
};
using GotLocoFunc1328 = std::function<void(void *sender, FC fc, FD fd)>;
struct CmdGetLocoFunc1328 : public Cmd {
const LocoAddr loco;
GotLocoFunc1328 const callback;
CmdGetLocoFunc1328(const LocoAddr loco, GotLocoFunc1328 const callback)
: loco(loco), callback(callback) {}
std::vector<uint8_t> getBytes() const override { return {0xE3, 0x09, loco.hi(), loco.lo()}; }
QString msg() const override {
return "Get Loco Function 13-28 Status " + QString::number(loco.addr);
}
};
///////////////////////////////////////////////////////////////////////////////
struct CmdSetSpeedDir : public Cmd {
const LocoAddr loco;
const unsigned speed;
const Direction dir;
CmdSetSpeedDir(const LocoAddr loco, const unsigned speed, const Direction dir)
: loco(loco), speed(speed), dir(dir) {
if (speed > 28)
throw EInvalidSpeed("Speed out of range!");
}
std::vector<uint8_t> getBytes() const override {
unsigned sp;
if (speed > 0)
sp = speed + 3;
else
sp = 0;
return {0xE4, 0x12, loco.hi(), loco.lo(),
static_cast<uint8_t>((static_cast<bool>(dir) << 7) + ((sp >> 1) & 0x0F) +
((sp & 0x1) << 4))};
}
QString msg() const override {
return "Loco " + QString::number(loco) + " Set Speed " + QString::number(speed) +
", Dir " + QString::number(static_cast<int>(dir));
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdSetSpeedDir>(cmd)) {
const auto &casted = dynamic_cast<const CmdSetSpeedDir &>(cmd);
return (casted.loco == this->loco);
}
if (is<CmdEmergencyStop>(cmd))
return true;
if (is<CmdEmergencyStopLoco>(cmd)) {
const auto &casted = dynamic_cast<const CmdEmergencyStopLoco &>(cmd);
return (casted.loco == this->loco);
}
return false;
}
bool okResponse() const override { return true; }
};
///////////////////////////////////////////////////////////////////////////////
struct CmdSetFuncA : public Cmd {
const LocoAddr loco;
const FA fa;
CmdSetFuncA(const LocoAddr loco, const FA fa) : loco(loco), fa(fa) {}
std::vector<uint8_t> getBytes() const override {
return {0xE4, 0x20, loco.hi(), loco.lo(), fa.all};
}
QString msg() const override {
return "Set loco " + QString::number(loco.addr) + " func A (0-4): " + QString::number(fa.all, 2).rightJustified(5, '0');
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdSetFuncA>(cmd)) {
const auto &casted = dynamic_cast<const CmdSetFuncA &>(cmd);
return (casted.loco == this->loco);
}
return false;
}
bool okResponse() const override { return true; }
};
enum class FSet {
F5toF8,
F9toF12,
};
struct CmdSetFuncB : public Cmd {
const LocoAddr loco;
const FB fb;
const FSet range;
CmdSetFuncB(const LocoAddr loco, const FB fb, const FSet range)
: loco(loco), fb(fb), range(range) {}
std::vector<uint8_t> getBytes() const override {
if (range == FSet::F5toF8)
return {0xE4, 0x21, loco.hi(), loco.lo(), static_cast<uint8_t>(fb.all & 0xF)};
return {0xE4, 0x22, loco.hi(), loco.lo(), static_cast<uint8_t>(fb.all >> 4)};
}
QString msg() const override {
return "Set loco " + QString::number(loco.addr) + " func B (5-12): " + QString::number(fb.all, 2).rightJustified(8, '0');
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdSetFuncB>(cmd)) {
const auto &casted = dynamic_cast<const CmdSetFuncB &>(cmd);
return ((casted.loco == this->loco) && (casted.range == this->range));
}
return false;
}
bool okResponse() const override { return true; }
};
struct CmdSetFuncC : public Cmd {
const LocoAddr loco;
const FC fc;
CmdSetFuncC(const LocoAddr loco, const FC fc) : loco(loco), fc(fc) {}
std::vector<uint8_t> getBytes() const override {
return {0xE4, 0x23, loco.hi(), loco.lo(), fc.all};
}
QString msg() const override {
return "Set loco " + QString::number(loco.addr) + " func C (13-20): " + QString::number(fc.all, 2).rightJustified(8, '0');
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdSetFuncC>(cmd)) {
const auto &casted = dynamic_cast<const CmdSetFuncC &>(cmd);
return (casted.loco == this->loco);
}
return false;
}
bool okResponse() const override { return true; }
};
struct CmdSetFuncD : public Cmd {
const LocoAddr loco;
const FD fd;
CmdSetFuncD(const LocoAddr loco, const FD fd) : loco(loco), fd(fd) {}
std::vector<uint8_t> getBytes() const override {
return {0xE4, 0x28, loco.hi(), loco.lo(), fd.all};
}
QString msg() const override {
return "Set loco " + QString::number(loco.addr) + " func D (21-28): " + QString::number(fd.all, 2).rightJustified(8, '0');
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdSetFuncD>(cmd)) {
const auto &casted = dynamic_cast<const CmdSetFuncD &>(cmd);
return (casted.loco == this->loco);
}
return false;
}
bool okResponse() const override { return true; }
};
///////////////////////////////////////////////////////////////////////////////
enum class ReadCVStatus {
Ok = 0x14,
ShortCircuit = 0x12,
DataByteNotFound = 0x13,
CSbusy = 0x1F,
CSready = 0x11,
};
using ReadCV =
std::function<void(void *sender, ReadCVStatus status, uint8_t cv, uint8_t value)>;
struct CmdReadDirect : public Cmd {
const uint8_t cv;
ReadCV const callback;
CmdReadDirect(const uint8_t cv, ReadCV const callback) : cv(cv), callback(callback) {}
std::vector<uint8_t> getBytes() const override { return {0x22, 0x15, cv}; }
QString msg() const override {
return "Direct Mode CV " + QString::number(cv) + " read request";
}
bool okResponse() const override { return true; }
};
struct CmdWriteDirect : public Cmd {
const uint8_t cv;
const uint8_t data;
CmdWriteDirect(const uint8_t cv, const uint8_t data)
: cv(cv), data(data) {}
std::vector<uint8_t> getBytes() const override { return {0x23, 0x16, cv, data}; }
QString msg() const override {
return "Direct Mode Write CV " + QString::number(cv) + " = " + QString::number(data);
}
bool okResponse() const override { return true; }
};
struct CmdRequestReadResult : public Cmd {
const uint8_t cv;
ReadCV const callback;
CmdRequestReadResult(const uint8_t cv, ReadCV const callback)
: cv(cv), callback(callback) {}
std::vector<uint8_t> getBytes() const override { return {0x21, 0x10}; }
QString msg() const override { return "Request for service mode results (after read)"; }
};
struct CmdRequestWriteResult : public Cmd {
const uint8_t cv;
const uint8_t value;
CmdRequestWriteResult(const uint8_t cv, const uint8_t value)
: cv(cv), value(value) {}
std::vector<uint8_t> getBytes() const override { return {0x21, 0x10}; }
QString msg() const override { return "Request for service mode results (after write)"; }
};
///////////////////////////////////////////////////////////////////////////////
struct CmdAccInfoRequest : public Cmd {
const uint8_t groupAddr;
const bool nibble;
CmdAccInfoRequest(const uint8_t groupAddr, const bool nibble)
: groupAddr(groupAddr), nibble(nibble) {}
std::vector<uint8_t> getBytes() const override {
return {0x42, groupAddr, static_cast<uint8_t>(0x80+nibble) };
}
QString msg() const override {
return "Accessory Decoder Information Request: group " + QString::number(groupAddr) +
", nibble:" + QString::number(nibble);
}
};
struct CmdAccOpRequest : public Cmd {
const uint16_t portAddr; // 0-2047
const bool state;
CmdAccOpRequest(const uint16_t portAddr, const bool state)
: portAddr(portAddr), state(state) {}
std::vector<uint8_t> getBytes() const override {
return {
0x52,
static_cast<uint8_t>(portAddr >> 3),
static_cast<uint8_t>(0x80 + (portAddr & 0x7) + (state << 3)) };
}
QString msg() const override {
return "Accessory Decoder Operation Request: port " + QString::number(portAddr) +
", state:" + QString::number(state);
}
bool conflict(const Cmd &cmd) const override {
if (is<CmdAccOpRequest>(cmd)) {
const auto &casted = dynamic_cast<const CmdAccOpRequest &>(cmd);
return ((casted.portAddr/2) == (this->portAddr/2));
}
return false;
}
bool okResponse() const override { return true; } // just for uLI
};
///////////////////////////////////////////////////////////////////////////////
} // namespace Xn
#endif