Debian Codegraph @

Hardware/M6051.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 <cstring> //memset
22 #include <sstream>
23 #include <string>
24
25 #include "Network/Select.h"
26 #include "Hardware/M6051.h"
27 #include "Utils/Integer.h"
28
29 using std::string;
30
31 namespace Hardware
32 {
33 M6051::M6051(const HardwareParams* params)
34 : HardwareInterface(params->GetManager(),
35 params->GetControlID(),
36 "Maerklin Interface (6050/6051) / " + params->GetName() + " at serial port " + params->GetArg1(),
37 params->GetName()),
38 serialLine(logger, params->GetArg1(), B2400, 8, 'N', 2),
39 run(true)
40 {
41 logger->Info(Languages::TextStarting, GetFullName());
42
43 s88Modules = Utils::Integer::StringToInteger(params->GetArg2(), 0, MaxS88Modules);
44 if (s88Modules == 0)
45 {
46 logger->Info(Languages::TextNoS88Modules);
47 return;
48 }
49 logger->Info(Languages::TextNrOfS88Modules, s88Modules);
50 s88Thread = std::thread(&Hardware::M6051::S88Worker, this);
51 }
52
53 M6051::~M6051()
54 {
55 run = false;
56 if (s88Modules > 0)
57 {
58 s88Thread.join();
59 }
60 }
61
62 void M6051::Booster(const BoosterState status)
63 {
64 if (!serialLine.IsConnected())
65 {
66 return;
67 }
68
69 unsigned char c;
70
71 if (status)
72 {
73 logger->Info(Languages::TextTurningBoosterOn);
74 c = 96;
75 }
76 else
77 {
78 logger->Info(Languages::TextTurningBoosterOn);
79 c = 97;
80 }
81 serialLine.Send(c);
82 }
83
84 void M6051::LocoSpeed(__attribute__((unused)) const Protocol protocol, const Address address, const Speed speed)
85 {
86 if (!serialLine.IsConnected())
87 {
88 return;
89 }
90 unsigned char speedMM = (speed / 69) + (GetSpeedMapEntry(address) & 16);
91 speedMap[address] = speedMM;
92 unsigned char addressMM = static_cast<unsigned char>(address);
93 logger->Info(Languages::TextSettingSpeed, address, speedMM);
94 SendTwoBytes(speedMM, addressMM);
95 }
96
97 void M6051::LocoOrientation(__attribute__((unused)) const Protocol protocol, const Address address, __attribute__((unused)) const Orientation orientation)
98 {
99 if (!serialLine.IsConnected())
100 {
101 return;
102 }
103 logger->Info(Languages::TextSettingDirectionOfTravel, address);
104 unsigned char speedMM = 15 + (GetSpeedMapEntry(address) & 16);
105 unsigned char addressMM = static_cast<unsigned char>(address);
106 SendTwoBytes(speedMM, addressMM);
107 }
108
109 void M6051::LocoFunction(__attribute__((unused)) const Protocol protocol,
110 const Address address,
111 const DataModel::LocoFunctionNr function,
112 const DataModel::LocoFunctionState on)
113 {
114 if (function > 4)
115 {
116 return;
117 }
118
119 if (!serialLine.IsConnected())
120 {
121 return;
122 }
123
124 logger->Info(Languages::TextSettingFunction, static_cast<int>(function), address, Languages::GetOnOff(on));
125 unsigned char addressMM = static_cast<unsigned char>(address);
126 if (function == 0)
127 {
128 unsigned char speedMM = (GetSpeedMapEntry(address) & 15) + (static_cast<unsigned char>(on) << 4);
129 speedMap[address] = speedMM;
130 SendTwoBytes(speedMM, addressMM);
131 return;
132 }
133
134 unsigned char functionMM = GetFunctionMapEntry(address);
135 unsigned char position = function - 1;
136 functionMM &= (~(1 << position)); // mask out related function
137 functionMM |= (static_cast<unsigned char>(on == DataModel::LocoFunctionStateOn) << position); // add related function
138 functionMap[address] = functionMM;
139 functionMM += 64;
140 SendTwoBytes(functionMM, addressMM);
141 }
142
143 void M6051::Accessory(__attribute__((unused)) const Protocol protocol,
144 const Address address,
145 const DataModel::AccessoryState state,
146 const bool on,
147 __attribute__((unused)) const DataModel::AccessoryPulseDuration duration)
148 {
149 if (!serialLine.IsConnected())
150 {
151 return;
152 }
153
154 logger->Info(Languages::TextSettingAccessoryOnOff, address, Languages::GetGreenRed(state), Languages::GetOnOff(on));
155 const unsigned char stateMM = (on ? (state == DataModel::AccessoryStateOn ? 33 : 34) : 32);
156 const unsigned char addressMM = static_cast<unsigned char>(address);
157 SendTwoBytes(stateMM, addressMM);
158 }
159
160 void M6051::S88Worker()
161 {
162 Utils::Utils::SetThreadName("M6051");
163 const unsigned char s88DoubleModules = ((s88Modules + 1) / 2);
164 const unsigned char command = 128 + s88DoubleModules;
165 const unsigned char s88SingleModules = (s88DoubleModules * 2);
166 while(run && serialLine.IsConnected())
167 {
168 serialLine.ClearBuffers();
169 serialLine.Send(command);
170 for (unsigned char module = 0; module < s88SingleModules; ++module)
171 {
172 string data;
173 bool ret = serialLine.Receive(data, 1);
174 if (ret <= 0)
175 {
176 logger->Error(Languages::TextUnableToReceiveData);
177 break;
178 }
179 unsigned char byte = data[0];
180
181 if (byte != s88Memory[module])
182 {
183 unsigned char xorByte = byte ^ s88Memory[module];
184 for (unsigned char pin = 1; pin <= 8; ++pin)
185 {
186 unsigned char shift = 8 - pin;
187 if (((xorByte >> shift) & 0x01) == 0)
188 {
189 continue;
190 }
191
192 const char* onOff;
193 DataModel::Feedback::FeedbackState state;
194 if ((byte >> shift) & 0x01)
195 {
196 onOff = Languages::GetText(Languages::TextOn);
197 state = DataModel::Feedback::FeedbackStateOccupied;
198 }
199 else
200 {
201 onOff = Languages::GetText(Languages::TextOff);
202 state = DataModel::Feedback::FeedbackStateFree;
203 }
204 logger->Info(Languages::TextFeedbackChange, pin, module, onOff);
205 Address address = (module * 8) + pin;
206 manager->FeedbackState(controlID, address, state);
207 }
208 s88Memory[module] = byte;
209 }
210 }
211 std::this_thread::yield();
212 }
213 }
214 } // namespace