-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCCpp_utils.cpp
More file actions
406 lines (345 loc) · 12 KB
/
DCCpp_utils.cpp
File metadata and controls
406 lines (345 loc) · 12 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// Created by Mathieu Andrade on 23/05/2021.
//
#include <windows.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include "DCCpp_utils.h"
void DCCpp_utils::printDebugMessage(const std::string &msg)
{
std::stringstream debugMessage;
SYSTEMTIME st;
GetSystemTime(&st);
// Build debug message with time and header
debugMessage << st.wHour << ':' << st.wMinute << ':' << st.wSecond << ':' << st.wMilliseconds << " -> CDM-Rail DCCPP Debug message : " << msg << '\n';
// copying the contents of the string to char array
char char_array[debugMessage.str().length() + 1];
strcpy(char_array, debugMessage.str().c_str());
OutputDebugStringA(char_array); // For Legacy support
std::cout << char_array;
}
int DCCpp_utils::saveFeedbackMsg(DCC_CMD_TAG *cmdTag, FEEDBACK_MSG_VECTOR &list)
{
FEEDBACK_MSG newFeedbackMsg;
if (cmdTag != nullptr)
{
newFeedbackMsg.nCmdTagType = cmdTag->nCmdTagType;
newFeedbackMsg.lTimeTickSchedule = cmdTag->lTimeTickSchedule;
newFeedbackMsg.lTimeTickDone = cmdTag->lTimeTickDone;
newFeedbackMsg.nCmdIdx = cmdTag->nCmdIdx;
newFeedbackMsg.nData = cmdTag->nData;
newFeedbackMsg.nAddress = cmdTag->nAddress;
newFeedbackMsg.pObjPtr = cmdTag->pObjPtr;
}
list.push_back(newFeedbackMsg);
return static_cast<int>(list.size()) - 1;
}
bool DCCpp_utils::copyFbMsgToGenericData(pDGI_GENERIC_RCV_DATA &genericRcvData, FEEDBACK_MSG_VECTOR &list)
{
genericRcvData->nCompletionCode = list[0].nCompletionCode;
genericRcvData->nRdCommand = list[0].nRdCommand;
genericRcvData->xCmdTag.nCmdTagType = list[0].nCmdTagType;
genericRcvData->xCmdTag.lTimeTickSchedule = list[0].lTimeTickSchedule;
genericRcvData->xCmdTag.lTimeTickDone = list[0].lTimeTickDone;
genericRcvData->xCmdTag.nCmdIdx = list[0].nCmdIdx;
genericRcvData->xCmdTag.nData = list[0].nData;
genericRcvData->xCmdTag.nAddress = list[0].nAddress;
genericRcvData->xCmdTag.pObjPtr = list[0].pObjPtr;
genericRcvData->nNbDataItems = list[0].nNbDataItems;
for (int i = 0; i < DGI_MAX_DATA_ITEMS; ++i)
{
genericRcvData->xDataItem[i].nAddress = list[0].xDataItem[i].nAddress;
genericRcvData->xDataItem[i].hData[0] = list[0].xDataItem[i].hData[0];
genericRcvData->xDataItem[i].hData[1] = list[0].xDataItem[i].hData[1];
genericRcvData->xDataItem[i].hData[2] = list[0].xDataItem[i].hData[2];
genericRcvData->xDataItem[i].hData[3] = list[0].xDataItem[i].hData[3];
}
return true;
}
bool DCCpp_utils::removeFeedbackMsg(const FEEDBACK_MSG &cmdTag, FEEDBACK_MSG_VECTOR &list)
{
list.erase(findFeedbackMsg(cmdTag, list));
return true;
}
FEEDBACK_MSG_IT DCCpp_utils::findFeedbackMsg(const FEEDBACK_MSG &cmdTag, FEEDBACK_MSG_VECTOR &list)
{
FEEDBACK_MSG_IT it;
for (it = list.begin(); it != list.end(); ++it)
{
if (it->nCmdTagType == cmdTag.nCmdTagType && it->pObjPtr == cmdTag.pObjPtr)
{
break; // Element found, stop loop
}
}
return it;
}
void DCCpp_utils::removeCharsFromString(std::string &str, char *charsToRemove)
{
for (unsigned int i = 0; i < strlen(charsToRemove); ++i)
{
str.erase(remove(str.begin(), str.end(), charsToRemove[i]), str.end());
}
}
unsigned int DCCpp_utils::ascii2Hex(const unsigned char *str)
{
unsigned int value;
value = 0;
value <<= 4;
if (str[0] >= '0' && str[0] <= '9')
{
value += (str[0] - '0');
}
else if (str[0] >= 'a' && str[0] <= 'f')
{
value += (str[0] - 'a' + 10);
}
else if (str[0] >= 'A' && str[0] <= 'F')
{
value += (str[0] - 'A' + 10);
}
return value;
}
int DCCpp_utils::saveLocoInfos(int index, int address, int speed, int direction, int functions)
{
if (index == -1)
{
DCCpp::listOfLocoInfos.push_back({address, speed, direction, functions});
index = (int)DCCpp::listOfLocoInfos.size() - 1; // Get last index of list
}
else
{
DCCpp::listOfLocoInfos[index].address = address;
DCCpp::listOfLocoInfos[index].speed = speed;
DCCpp::listOfLocoInfos[index].direction = direction;
DCCpp::listOfLocoInfos[index].functions = functions;
}
return index + 1; // Return register number (register number start at 1, so this is index in list + 1)
}
int DCCpp_utils::getLocoInfosIndex(const int address)
{
LOCO_INFOS_IT it;
int index = -1;
for (it = DCCpp::listOfLocoInfos.begin(); it != DCCpp::listOfLocoInfos.end(); ++it)
{
if (it->address == address)
{
index = it - DCCpp::listOfLocoInfos.begin();
break; // Element found, stop loop
}
}
return index;
}
unsigned int DCCpp_utils::getLocoFuncValue(unsigned int funcValue, unsigned int funcMask)
{
unsigned int value;
bool F0State;
switch (funcMask)
{
case 0x001F:
value = funcValue & funcMask;
F0State = (value & 1) != 0;
value >>= 1;
if (F0State)
{
value |= 0x10;
}
value += 0x80;
break;
case 0x01E0:
value = funcValue & funcMask;
value >>= 5;
value += 0xB0;
break;
case 0x1E00:
value = funcValue & funcMask;
value >>= 9;
value += 0xA0;
break;
default:
value = 0;
break;
}
return value;
}
int DCCpp_utils::convertLocoFuncValue(int funcValue, int newValue)
{
int value = newValue;
if (newValue > 127 && newValue < 160)
{
value = value - 128;
value <<= 1;
if((value & 32) != 0)
{
value -= 31;
}
}
else if (newValue > 175 && newValue < 192)
{
value = value - 176;
value = value * 32;
}
else if (newValue > 159 && newValue < 176)
{
value = value - 160;
value = value * 512;
}
return funcValue ^ value;
}
void DCCpp_utils::setTimeouts(DWORD timeout)
{
COMMTIMEOUTS timeouts;
GetCommTimeouts(DCCpp::comPort, &timeouts);
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = timeout;
timeouts.ReadTotalTimeoutMultiplier = 50;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 50;
SetCommTimeouts(DCCpp::comPort, &timeouts);
}
void DCCpp_utils::setComParams()
{
DCB dcbSerialParams;
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
// Write the parameters
GetCommState(DCCpp::comPort, &dcbSerialParams);
dcbSerialParams.BaudRate = CBR_115200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
SetCommState(DCCpp::comPort, &dcbSerialParams);
}
std::list<std::string> DCCpp_utils::listPorts()
{
char lpTargetPath[5000]; // buffer to store the path of the COM PORTS
std::list<std::string> portList;
for (int i = 0; i < 255; i++) // checking ports from COM0 to COM255
{
std::string str = "COM" + std::to_string(i); // converting to COM0, COM1, COM2
DWORD result = QueryDosDeviceA(str.c_str(), lpTargetPath, 5000);
// Test the return value and error if any
if (result != 0) // QueryDosDevice returns zero if it didn't find an object
{
portList.push_back(str);
}
}
return portList;
}
void DCCpp_utils::saveAllDCCppParams()
{
WritePrivateProfileString("Default", "com", DCCpp::comNumber, "dccpp_config.ini");
WritePrivateProfileString("Default", "ip", DCCpp::ipAddress, "dccpp_config.ini");
WritePrivateProfileString("Default", "mode", DCCpp::usbMode ? "true" : "false", "dccpp_config.ini");
WritePrivateProfileString("Default", "accessory", DCCpp::accessoryCmdType, "dccpp_config.ini");
std::string detectors = std::to_string(DCCpp::detectorsModuleCount);
WritePrivateProfileString("Default", "s88_modules", detectors.c_str(), "dccpp_config.ini");
std::string timer = std::to_string(DCCpp::cmdTimer);
WritePrivateProfileString("Default", "timer", timer.c_str(), "dccpp_config.ini");
}
void DCCpp_utils::getDCCppParams()
{
char temp[6];
GetPrivateProfileString("Default", "mode", "true", temp, 6, "dccpp_config.ini");
DCCpp::usbMode = (std::strcmp(temp, "true") == 0);
GetPrivateProfileString("Default", "com", "", DCCpp::comNumber, 5, "dccpp_config.ini");
GetPrivateProfileString("Default", "ip", "", DCCpp::ipAddress, 20, "dccpp_config.ini");
// GetPrivateProfileString("Default", "accessory", "T", DCCpp::accessoryCmdType, 2, "dccpp_config.ini");
DCCpp::detectorsModuleCount = GetPrivateProfileInt("Default", "s88_modules", 8, "dccpp_config.ini");
DCCpp::cmdTimer = GetPrivateProfileInt("Default", "timer", 100, "dccpp_config.ini");
}
bool DCCpp_utils::saveCmdWtRsp(const DCC_CMD_TYPE cmdType, const CMD_STATION_FB_TYPE fbCmdType, const CMD_ARG args)
{
if (args != nullptr)
{
DCCpp_commands::listOfCmdWaitingResp.push_back({cmdType, fbCmdType, {args[0], args[1], args[2], args[3], args[4]}});
}
else
{
DCCpp_commands::listOfCmdWaitingResp.push_back({cmdType, fbCmdType});
}
return true;
}
bool DCCpp_utils::findCmdWtRsp(CMD_WT_RSP_IT &it, CMD_STATION_FB_TYPE fbType, std::string &cmd, int nbOfArgs, int argsOffset, CMD_ARG args)
{
bool found = false;
DCCpp_utils::getArgs(cmd, args);
for (it = DCCpp_commands::listOfCmdWaitingResp.begin(); it != DCCpp_commands::listOfCmdWaitingResp.end(); ++it)
{
if (it->fbCmdType == fbType)
{
// Command type found
// Check if args corresponding to target
for (int i = argsOffset; i < nbOfArgs; i++)
{
if (it->args[i] == args[i])
{
found = true; // If args corresponding to target, is good
}
else
{
found = false; // If args not corresponding to target
break; // stop loop
}
}
if (found)
{
break; // Element found, stop loop
}
}
}
return found;
}
bool DCCpp_utils::removeCmdWtRsp(const CMD_WT_RSP_IT &cmdWtRsp)
{
DCCpp_commands::listOfCmdWaitingResp.erase(cmdWtRsp);
return true;
}
void DCCpp_utils::getArgs(std::string &str, CMD_ARG args)
{
int index = 0;
char charStr[str.length() + 1];
// copying the contents of the
// string to char array
strcpy(charStr, str.c_str());
char *token = std::strtok(charStr, " ");
// Keep printing tokens while one of the
// delimiters present in str[].
while (token != nullptr)
{
args[index] = std::stoi(token);
token = strtok(nullptr, " ");
index++;
}
}
void DCCpp_utils::clearDetectorStates() {
// Clear states to prevent a multiple call of resume operations
DCCpp::detectorStates = "";
// Init all nibbles states to 0
for (int i = 0; i < (DCCpp::detectorsModuleCount * 2); ++i)
{
DCCpp::detectorStates.push_back('0');
}
}
// https://stackoverflow.com/questions/4308503/how-to-enable-visual-styles-without-a-manifest
// NOTE: It is recommended that you delay-load ComCtl32.dll (/DelayLoad:ComCtl32.dll)
// and that you ensure this code runs before GUI components are loaded.
// Otherwise, you may get weird issues, like black backgrounds in icons in image lists.
ULONG_PTR DCCpp_utils::enableVisualStyles()
{
TCHAR dir[MAX_PATH];
ULONG_PTR ulpActivationCookie = FALSE;
ACTCTX actCtx =
{
sizeof(actCtx),
ACTCTX_FLAG_RESOURCE_NAME_VALID
| ACTCTX_FLAG_SET_PROCESS_DEFAULT
| ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID,
TEXT("shell32.dll"), 0, 0, dir, (LPCTSTR)124
};
UINT cch = GetSystemDirectory(dir, sizeof(dir) / sizeof(*dir));
if (cch >= sizeof(dir) / sizeof(*dir)) { return FALSE; /*shouldn't happen*/ }
dir[cch] = TEXT('\0');
ActivateActCtx(CreateActCtx(&actCtx), &ulpActivationCookie);
return ulpActivationCookie;
}