Debian Codegraph @

Hardware/Hsi88.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 <fcntl.h>
23 #include <sstream>
24 #include <termios.h>
25
26 #include "Languages.h"
27 #include "Network/Select.h"
28 #include "Hardware/Hsi88.h"
29 #include "Utils/Integer.h"
30
31 namespace Hardware
32 {
33 Hsi88::Hsi88(const HardwareParams* params)
34 : HardwareInterface(params->GetManager(),
35 params->GetControlID(),
36 "HSI-88 / " + params->GetName() + " at serial port " + params->GetArg1(),
37 params->GetName()),
38 serialLine(logger, params->GetArg1(), B9600, 8, 'N', 1),
39 run(false)
40 {
41 logger->Info(Languages::TextStarting, GetFullName());
42
43 memset(s88Init, 0xFF, sizeof(s88Memory));
44 memset(s88Memory, 0x00, sizeof(s88Memory));
45
46 s88Modules1 = Utils::Integer::StringToInteger(params->GetArg2(), 0);
47 s88Modules2 = Utils::Integer::StringToInteger(params->GetArg3(), 0);
48 s88Modules3 = Utils::Integer::StringToInteger(params->GetArg4(), 0);
49 s88Modules = s88Modules1 + s88Modules2 + s88Modules3;
50
51 if (s88Modules > MaxS88Modules)
52 {
53 logger->Error(Languages::TextTooManyS88Modules, s88Modules, MaxS88Modules);
54 return;
55 }
56
57 if (s88Modules == 0)
58 {
59 logger->Error(Languages::TextNoS88Modules);
60 return;
61 }
62
63 std::string version = GetVersion();
64 logger->Info(Languages::TextVersion, version);
65
66 unsigned char modulesConfigured = ConfigureS88();
67 if (s88Modules != modulesConfigured)
68 {
69 logger->Error(Languages::TextHsi88ErrorConfiguring, modulesConfigured);
70 return;
71 }
72
73 logger->Info(Languages::TextHsi88Configured, s88Modules, s88Modules1, s88Modules2, s88Modules3);
74
75 checkEventsThread = std::thread(&Hardware::Hsi88::CheckEventsWorker, this);
76 }
77
78 Hsi88::~Hsi88()
79 {
80 if (!run)
81 {
82 return;
83 }
84 run = false;
85 checkEventsThread.join();
86 }
87
88 std::string Hsi88::ReadUntilCR()
89 {
90 std::string data;
91 unsigned char input = 0;
92 while (true)
93 {
94 int ret = serialLine.Receive(&input, sizeof(input), 1, 0);
95 if (ret < 0 || input == '\r')
96 {
97 logger->HexIn(data);
98 return data;
99 }
100 data.append(reinterpret_cast<char*>(&input), ret);
101 }
102 }
103
104 std::string Hsi88::GetVersion()
105 {
106 const unsigned char command[2] = { 'v', '\r' };
107 SendData(command, sizeof(command));
108 return ReadUntilCR();
109 }
110
111 unsigned char Hsi88::ConfigureS88()
112 {
113 const unsigned char command [5] = { 's', static_cast<unsigned char>(s88Modules1 >> 1), static_cast<unsigned char>(s88Modules2 >> 1), static_cast<unsigned char>(s88Modules3 >> 1), '\r' };
114 SendData(command, sizeof(command));
115 unsigned char input[3];
116 int ret = serialLine.ReceiveExact(input, sizeof(input));
117 logger->HexIn(input, sizeof(input));
118 if (ret <= 0)
119 {
120 return 0;
121 }
122 if (input[0] != 's')
123 {
124 return 0;
125 }
126 return input[1] << 1;
127 }
128
129 void Hsi88::ReadData()
130 {
131 std::string data;
132 const unsigned char headerDataSize = 3;
133 serialLine.ReceiveExact(data, headerDataSize);
134 if (data.size() != headerDataSize)
135 {
136 return;
137 }
138 if (data[0] != 'i')
139 {
140 return;
141 }
142 const unsigned char modules = data[1];
143 const unsigned char moduleDataSize = modules * 3;
144 const unsigned char commandSize = moduleDataSize + headerDataSize;
145 serialLine.ReceiveExact(data, moduleDataSize);
146 logger->HexIn(data);
147 if (data.size() != commandSize)
148 {
149 return;
150 }
151 for (unsigned char module = 0; module < modules; ++module)
152 {
153 const unsigned char modulePosition = module * 3 + 2;
154 const unsigned char* moduleData = reinterpret_cast<const unsigned char*>(data.c_str()) + modulePosition;
155 if (*moduleData > (MaxS88Modules >> 1))
156 {
157 continue;
158 }
159 const unsigned char memoryPosition = (data[modulePosition] - 1) * 2;
160 CheckFeedbackByte(moduleData[2], memoryPosition);
161 CheckFeedbackByte(moduleData[1], memoryPosition + 1);
162 }
163 }
164
165 void Hsi88::CheckFeedbackByte(const unsigned char dataByte, const unsigned char module)
166 {
167 unsigned char diff = (s88Memory[module] ^ dataByte) | s88Init[module];
168 if (!diff)
169 {
170 return;
171 }
172 unsigned char pin = 0;
173 while (diff)
174 {
175 if (diff & 0x01)
176 {
177 DataModel::Feedback::FeedbackState state = static_cast<DataModel::Feedback::FeedbackState>((dataByte >> pin) & 0x01);
178 logger->Info(Languages::TextFeedbackChange, pin + 1, module, Languages::GetOnOff(state));
179 manager->FeedbackState(controlID, module * 8 + pin + 1, state);
180 }
181 diff >>= 1;
182 ++pin;
183 }
184 s88Memory[module] = dataByte;
185 s88Init[module] = 0;
186 }
187
188 void Hsi88::CheckEventsWorker()
189 {
190 Utils::Utils::SetThreadName("HSI-88");
191 run = true;
192 while (run)
193 {
194 ReadData();
195 std::this_thread::yield();
196 }
197 }
198 } // namespace