Repository navigation
Expand file tree
/
Copy pathAdapter.cpp
More file actions
206 lines (187 loc) · 6.17 KB
/
Copy pathAdapter.cpp
File metadata and controls
206 lines (187 loc) · 6.17 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
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
/*
* © 2023 Chris Harlow
* © 2023 Alex Shepherd
* © 2023 Balazs Racz
* All rights reserved.
*
* This file is part of EX-Adapter for LCC
*
* 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 "Adapter.h"
#include "LocoState.h"
#include "Bus.h"
ParserState Adapter::state=ParserState::READY;
char Adapter::opcode='?';
byte Adapter::params=0;
uint64_t Adapter::p[4];
const uint64_t sender_mask=0xFFFFFFFFFFFF0000;
void Adapter::setup() {
Serial.begin(115200);
Bus::setCallback(eventHandler);
Serial.print(F("<L>\r\n")); // introduce self to CS.
}
void Adapter::loop() {
// Allow Bus acess
Bus::loop();
// Read any inbound
while(Serial.available()) {
if (parse(Serial.read())) {
// and do only one command per loop
processCommand();
break;
}
}
}
const char CMD_LOCO='l'; // <l loco state broadcast
const char CMD_LCC='L'; // <L opcode
const char CMD_LISTENS_TO='L'; //<LL id xsender:event>
const char CMD_SENDS='S'; //<LS xevent>
const char CMD_READY='R'; // <LR>
const char CMD_SEND=' '; // <L event>
const char CMD_DUMP='D';
bool Adapter::parse(char input) {
static byte base=10;
switch (state) {
case READY:
if (input=='<') state=GETOP1;
break;
case GETOP1:
// This check is not nice but allows us to ditch
// anything we are not interested in quickly.
if (input==CMD_LOCO) {
state=SKIP; // normal 1-byte opcodes
break;
}
if (input!=CMD_LCC) {
state=READY;
break;
}
state=GETOP2;
break;
case GETOP2:
// This check is not nice but allows us to ditch
// anything we are not interested in quickly.
if (input == CMD_SEND
|| input == CMD_LISTENS_TO
|| input == CMD_SENDS
|| input == CMD_READY
|| input == CMD_DUMP) {
opcode=input;
state=SKIP;
params=0;
p[0]=0;
}
else state=READY; // ignore until next <
break;
case SKIP:
if (input==' ') break;
if (input=='>') {
state=READY;
return true;
}
if (input=='x') {
base=16;
state=PARSE;
break;
}
base=10;
state=PARSE;
[[fallthrough]];
case PARSE:
if (input>='0' && input<='9') p[params]=p[params]*base + (input-'0');
else if (input>='A' && input<='F') p[params]=p[params]*base + (input-'A' + 10);
else if (input==' ') {
params++;
state=SKIP;
}
else if (input=='>') {
params++;
state=READY;
return true;
}
break; // side effect, ignores dots, colons in x
}
return false;
}
void Adapter::processCommand() {
switch(opcode) {
case CMD_LOCO: // <l id ignored speedbyte functions>
{
// maintain loco states and inform Bus of any changes
auto loco=LocoState::get(p[0]);
uint16_t speed=p[2];
uint32_t funcs=p[3];
if (loco->DCCSpeedByte!=speed) {
loco->DCCSpeedByte=speed;
Bus::locoSpeedUpdate(loco->id,loco->DCCSpeedByte);
}
if (loco->functonMap!=funcs) {
loco->functonMap=funcs;
Bus::locoFunctionUpdate(loco->id,loco->functonMap);
}
break;
}
case CMD_SEND: // <L event> send event to LCC
Bus::sendEvent(p[0]);
break;
case CMD_READY: // <LR> all registration done
Bus::ready();
break;
case CMD_LISTENS_TO: // <LL id event>
// Add sender:event to listened list with id
Bus::registerInboundEvent(p[1],p[0]);
Serial.print(F("<L>\r\n")); // keep polling for metadata
break;
case CMD_SENDS: // <LS event>
Bus::registerOutboundEvent(p[0]);
Serial.print(F("<L>\r\n")); // keep polling for metadata
break;
case CMD_DUMP: // <LD> Dump
Serial.print(F("<* LD no longer exists *>\n"));
break;
default:
// All other incoming commands are ignored to meet
// DCC-EX throttle rules.
break;
}
}
void Adapter::eventHandler(uint32_t eventid) {
Serial.print(F("<L "));
Serial.print(eventid);
Serial.print(F(">\n"));
}
void Adapter::setSpeed(int16_t locoid,byte dccspeed) {
// convert to jmri standard speed as understood by <t
int16_t jmrispeed=dccspeed & 0x7f;
if (jmrispeed==1) jmrispeed=-1;
else if (jmrispeed>0) jmrispeed--;
Serial.print(F("<t "));
Serial.print(locoid);
Serial.print(F(" "));
Serial.print(jmrispeed);
Serial.print(F(" "));
Serial.write((dccspeed & 0x80)? '1' : '0');
Serial.print(F(">\n"));
}
void Adapter::setFunction(int16_t locoid,byte function, bool on) {
Serial.print(F("<F "));
Serial.print(locoid);
Serial.print(F(" "));
Serial.print(function);
Serial.print(F(" "));
Serial.print(on);
Serial.print(F(">\n"));
}