File: Hsi88.cpp

package info (click to toggle)
railcontrol 24%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,680 kB
  • sloc: cpp: 40,834; javascript: 2,509; makefile: 144; php: 97; sh: 60
file content (198 lines) | stat: -rw-r--r-- 5,234 bytes parent folder | download | duplicates (2)
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
/*
RailControl - Model Railway Control Software

Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org

RailControl 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, or (at your option) any
later version.

RailControl 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 RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/

#include <cstring>    //memset
#include <fcntl.h>
#include <sstream>
#include <termios.h>

#include "Languages.h"
#include "Network/Select.h"
#include "Hardware/Hsi88.h"
#include "Utils/Integer.h"

namespace Hardware
{
	Hsi88::Hsi88(const HardwareParams* params)
	:	HardwareInterface(params->GetManager(),
			params->GetControlID(),
			"HSI-88 / " + params->GetName() + " at serial port " + params->GetArg1(),
			params->GetName()),
	 	serialLine(logger, params->GetArg1(), B9600, 8, 'N', 1),
		run(false)
	{
		logger->Info(Languages::TextStarting, GetFullName());

		memset(s88Init, 0xFF, sizeof(s88Memory));
		memset(s88Memory, 0x00, sizeof(s88Memory));

		s88Modules1 = Utils::Integer::StringToInteger(params->GetArg2(), 0);
		s88Modules2 = Utils::Integer::StringToInteger(params->GetArg3(), 0);
		s88Modules3 = Utils::Integer::StringToInteger(params->GetArg4(), 0);
		s88Modules = s88Modules1 + s88Modules2 + s88Modules3;

		if (s88Modules > MaxS88Modules)
		{
			logger->Error(Languages::TextTooManyS88Modules, s88Modules, MaxS88Modules);
			return;
		}

		if (s88Modules == 0)
		{
			logger->Error(Languages::TextNoS88Modules);
			return;
		}

		std::string version = GetVersion();
		logger->Info(Languages::TextVersion, version);

		unsigned char modulesConfigured = ConfigureS88();
		if (s88Modules != modulesConfigured)
		{
			logger->Error(Languages::TextHsi88ErrorConfiguring, modulesConfigured);
			return;
		}

		logger->Info(Languages::TextHsi88Configured, s88Modules, s88Modules1, s88Modules2, s88Modules3);

		checkEventsThread = std::thread(&Hardware::Hsi88::CheckEventsWorker, this);
	}

	Hsi88::~Hsi88()
	{
		if (!run)
		{
			return;
		}
		run = false;
		checkEventsThread.join();
	}

	std::string Hsi88::ReadUntilCR()
	{
		std::string data;
		unsigned char input = 0;
		while (true)
		{
			int ret = serialLine.Receive(&input, sizeof(input), 1, 0);
			if (ret < 0 || input == '\r')
			{
				logger->HexIn(data);
				return data;
			}
			data.append(reinterpret_cast<char*>(&input), ret);
		}
	}

	std::string Hsi88::GetVersion()
	{
		const unsigned char command[2] = { 'v', '\r' };
		SendData(command, sizeof(command));
		return ReadUntilCR();
	}

	unsigned char Hsi88::ConfigureS88()
	{
		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' };
		SendData(command, sizeof(command));
		unsigned char input[3];
		int ret = serialLine.ReceiveExact(input, sizeof(input));
		logger->HexIn(input, sizeof(input));
		if (ret <= 0)
		{
			return 0;
		}
		if (input[0] != 's')
		{
			return 0;
		}
		return input[1] << 1;
	}

	void Hsi88::ReadData()
	{
		std::string data;
		const unsigned char headerDataSize = 3;
		serialLine.ReceiveExact(data, headerDataSize);
		if (data.size() != headerDataSize)
		{
			return;
		}
		if (data[0] != 'i')
		{
			return;
		}
		const unsigned char modules = data[1];
		const unsigned char moduleDataSize = modules * 3;
		const unsigned char commandSize = moduleDataSize + headerDataSize;
		serialLine.ReceiveExact(data, moduleDataSize);
		logger->HexIn(data);
		if (data.size() != commandSize)
		{
			return;
		}
		for (unsigned char module = 0; module < modules; ++module)
		{
			const unsigned char modulePosition = module * 3 + 2;
			const unsigned char* moduleData = reinterpret_cast<const unsigned char*>(data.c_str()) + modulePosition;
			if (*moduleData > (MaxS88Modules >> 1))
			{
				continue;
			}
			const unsigned char memoryPosition = (data[modulePosition] - 1) * 2;
			CheckFeedbackByte(moduleData[2], memoryPosition);
			CheckFeedbackByte(moduleData[1], memoryPosition + 1);
		}
	}

	void Hsi88::CheckFeedbackByte(const unsigned char dataByte, const unsigned char module)
	{
		unsigned char diff = (s88Memory[module] ^ dataByte) | s88Init[module];
		if (!diff)
		{
			return;
		}
		unsigned char pin = 0;
		while (diff)
		{
			if (diff & 0x01)
			{
				DataModel::Feedback::FeedbackState state = static_cast<DataModel::Feedback::FeedbackState>((dataByte >> pin) & 0x01);
				logger->Info(Languages::TextFeedbackChange, pin + 1, module, Languages::GetOnOff(state));
				manager->FeedbackState(controlID, module * 8 + pin + 1, state);
			}
			diff >>= 1;
			++pin;
		}
		s88Memory[module] = dataByte;
		s88Init[module] = 0;
	}

	void Hsi88::CheckEventsWorker()
	{
		Utils::Utils::SetThreadName("HSI-88");
		run = true;
		while (run)
		{
			ReadData();
			std::this_thread::yield();
		}
	}
} // namespace