Debian Codegraph @

Hardware/Protocols/DccPpEx.cpp

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <string>
22
23 #include "DataModel/Feedback.h"
24 #include "Manager.h"
25 #include "Hardware/Protocols/DccPpEx.h"
26
27 using std::string;
28 using std::to_string;
29
30 namespace Hardware
31 {
32 namespace Protocols
33 {
34 void DccPpEx::Booster(const BoosterState status)
35 {
36 string buffer("<");
37 logger->Info(status ? Languages::TextTurningBoosterOn : Languages::TextTurningBoosterOff);
38 buffer += to_string(static_cast<unsigned char>(status));
39 buffer += ">";
40 SendInternal(buffer);
41 }
42
43 void DccPpEx::LocoSpeed(__attribute__((unused)) const Protocol protocol,
44 const Address address,
45 const Speed speed)
46 {
47 locoCache.SetSpeed(address, speed);
48 Orientation orientation = locoCache.GetOrientation(address);
49 LocoSpeedOrientation(address, speed, orientation);
50 }
51
52 void DccPpEx::LocoOrientation(__attribute__((unused)) const Protocol protocol,
53 const Address address,
54 const Orientation orientation)
55 {
56 Speed speed = locoCache.GetSpeed(address);
57 locoCache.SetOrientation(address, orientation);
58 LocoSpeedOrientation(address, speed, orientation);
59 }
60
61 void DccPpEx::LocoFunction(__attribute__((unused)) const Protocol protocol,
62 const Address address,
63 const DataModel::LocoFunctionNr function,
64 const DataModel::LocoFunctionState on)
65 {
66 string buffer("<F ");
67 buffer += to_string(address);
68 buffer += " ";
69 buffer += to_string(function);
70 buffer += " ";
71 buffer += on ? "1" : "0";
72 buffer += ">";
73 SendInternal(buffer);
74 }
75
76 void DccPpEx::Accessory(__attribute__((unused)) const Protocol protocol,
77 const Address address,
78 const DataModel::AccessoryState state,
79 const bool on,
80 __attribute__((unused)) const DataModel::AccessoryPulseDuration duration)
81 {
82 if (!on)
83 {
84 return;
85 }
86 string buffer("<a ");
87 buffer += to_string(address);
88 buffer += " ";
89 buffer += state == DataModel::AccessoryStateOn ? "1" : "0";
90 buffer += ">";
91 SendInternal(buffer);
92 buffer[1] = 'T';
93 SendInternal(buffer);
94 buffer[1] = 'Z';
95 SendInternal(buffer);
96 }
97
98 void DccPpEx::ProgramWrite(const ProgramMode mode,
99 const Address address,
100 const CvNumber cv,
101 const CvValue value)
102 {
103 switch(mode)
104 {
105 case ProgramModeDccDirect:
106 ProgramWriteProgram(cv, value);
107 return;
108
109 case ProgramModeDccPomLoco:
110 ProgramWriteMain(address, cv, value);
111 return;
112
113 default:
114 return;
115 }
116 }
117
118 void DccPpEx::ProgramWriteMain(const Address address,
119 const CvNumber cv,
120 const CvValue value)
121 {
122 string buffer("<w ");
123 buffer += to_string(address);
124 buffer += " ";
125 buffer += to_string(cv);
126 buffer += " ";
127 buffer += to_string(value);
128 buffer += ">";
129 SendInternal(buffer);
130 }
131
132 void DccPpEx::ProgramWriteProgram(const CvNumber cv,
133 const CvValue value)
134 {
135 string buffer("<W ");
136 buffer += to_string(cv);
137 buffer += " ";
138 buffer += to_string(value);
139 buffer += ">";
140 SendInternal(buffer);
141 }
142
143 void DccPpEx::ProgramRead(const ProgramMode mode,
144 __attribute__((unused)) const Address address,
145 const CvNumber cv)
146 {
147 switch(mode)
148 {
149 case ProgramModeDccDirect:
150 ProgramReadProgram(cv);
151 return;
152
153 default:
154 return;
155 }
156 }
157
158 void DccPpEx::ProgramReadProgram(const CvNumber cv)
159 {
160 string buffer("<R ");
161 buffer += to_string(cv);
162 buffer += " 8 9>";
163 SendInternal(buffer);
164 }
165
166 void DccPpEx::LocoSpeedOrientation(const Address address,
167 const Speed speed,
168 const Orientation orientation)
169 {
170 string buffer("<t 1 ");
171 buffer += to_string(address);
172 buffer += " ";
173 buffer += to_string(speed >= 1008 ? 126 : speed >> 3);
174 buffer += " ";
175 buffer += to_string(orientation);
176 buffer += ">";
177 SendInternal(buffer);
178 }
179
180 void DccPpEx::Receiver()
181 {
182 Utils::Utils::SetThreadName("DCC-EX");
183 logger->Info(Languages::TextReceiverThreadStarted);
184 while (true)
185 {
186 string buffer;
187 const bool ret = ReceiveInternal(buffer);
188 if (!ret)
189 {
190 break;
191 }
192 Parse(buffer);
193 }
194 logger->Info(Languages::TextTerminatingReceiverThread);
195 }
196
197 bool DccPpEx::ReceiveInternal(string& buffer)
198 {
199 size_t size = 0;
200 while (size == 0 || ((buffer[size - 1] != '>') && (buffer[size - 1] != 0x0a)))
201 {
202 if (!run)
203 {
204 return false;
205 }
206 Receive(buffer);
207 size = buffer.size();
208 }
209
210 if (size == 0)
211 {
212 return false;
213 }
214
215 logger->HexIn(buffer);
216 return true;
217 }
218
219 void DccPpEx::Parse(const string& buffer)
220 {
221 unsigned int pos = 0;
222 while (buffer.size() > pos)
223 {
224 if (buffer[pos] != '<')
225 {
226 ++pos;
227 continue;
228 }
229
230 ++pos;
231 if (buffer.size() <= pos)
232 {
233 return;
234 }
235 switch(buffer[pos++])
236 {
237 case 'Q':
238 {
239 // pin is high
240 const FeedbackPin pin = ParseInt(buffer, pos);
241 logger->Info(Languages::TextFeedbackChange, pin & 0x000F, pin >> 4, Languages::GetText(Languages::TextOn));
242 manager->FeedbackState(controlID, pin, DataModel::Feedback::FeedbackStateOccupied);
243 break;
244 }
245
246 case 'q':
247 {
248 // pin is low
249 const FeedbackPin pin = ParseInt(buffer, pos);
250 logger->Info(Languages::TextFeedbackChange, pin & 0x000F, pin >> 4, Languages::GetText(Languages::TextOff));
251 manager->FeedbackState(controlID, pin, DataModel::Feedback::FeedbackStateFree);
252 break;
253 }
254
255 case 'r':
256 {
257 // read CV value
258 ParseInt(buffer, pos); // should be 8, sent in ProgramReadProgram
259 ParsePipe(buffer, pos);
260 ParseInt(buffer, pos); // should be 9, sent in ProgramReadProgram
261 ParsePipe(buffer, pos);
262 const CvNumber cv = ParseInt(buffer, pos);
263 const int value = ParseInt(buffer, pos);
264 if (value < 0)
265 {
266 // read error
267 break;
268 }
269 logger->Info(Languages::TextProgramReadValue, cv, static_cast<CvValue>(value));
270 manager->ProgramValue(cv, value);
271 break;
272 }
273
274 default:
275 // other answer does not matter
276 break;
277 }
278
279 do
280 {
281 ++pos;
282 }
283 while (buffer.size() > pos && buffer[pos] != '>');
284 }
285 }
286
287 int DccPpEx::ParseInt(const string& buffer, unsigned int& pos)
288 {
289 ParseSpace(buffer, pos);
290 bool minus = false;
291 int out = 0;
292 while (buffer.size() > pos)
293 {
294 const char c = buffer[pos];
295 if (c == '-')
296 {
297 minus = true;
298 ++pos;
299 continue;
300 }
301 if (c < '0' || c > '9')
302 {
303 break;
304 }
305 out *= 10;
306 out += c - '0';
307 ++pos;
308 }
309 return minus ? -out : out;
310 }
311
312 void DccPpEx::ParseSpace(const string& buffer, unsigned int& pos)
313 {
314 while (buffer.size() > pos)
315 {
316 const char c = buffer[pos];
317 if (c != ' ')
318 {
319 return;
320 }
321 ++pos;
322 }
323 }
324
325 bool DccPpEx::ParsePipe(const string& buffer, unsigned int& pos)
326 {
327 if (buffer.size() <= pos)
328 {
329 return false;
330 }
331
332 const char c = buffer[pos];
333 if (c != '|')
334 {
335 return false;
336 }
337
338 ++pos;
339 return true;
340 }
341 } // namespace
342 } // namespace