-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbustostring.cpp
218 lines (185 loc) · 5.88 KB
/
xbustostring.cpp
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
#include "xbustostring.h"
#include "xbusmessageid.h"
#include "xbus.h"
#include "xsdataidentifier.h"
String g_textBuffer;
uint8_t readUint8(const uint8_t *data, int &index) {
uint8_t result = data[index++];
return result;
}
uint16_t readUint16(const uint8_t *data, int &index) {
// uint16_t result = 0;
// result |= data[index++] << 8;
// result |= data[index++] << 0;
uint16_t value = (data[index] << 8) | data[index + 1];
index += 2;
return value;
// return result;
}
uint32_t readUint32(const uint8_t *data, int &index) {
uint32_t result = 0;
result |= data[index++] << 24;
result |= data[index++] << 16;
result |= data[index++] << 8;
result |= data[index++] << 0;
return result;
}
// uint32_t readUint32BE(const uint8_t *data, int &index) {
// uint32_t result = 0;
// result |= data[index++] << 0; // Least significant byte first
// result |= data[index++] << 8;
// result |= data[index++] << 16;
// result |= data[index++] << 24; // Most significant byte last
// return result;
// }
void dataswapendian(uint8_t *data, int len) {
int i = 0, j = len - 1;
while (i < j) {
uint8_t temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
}
}
double parseFP1632(const uint8_t *data) {
int32_t fpfrac;
int16_t fpint;
// Note: memcpy is used to prevent issues caused by alignment requirements
memcpy(&fpfrac, data, sizeof(fpfrac));
memcpy(&fpint, data + 4, sizeof(fpint));
// Convert the big-endian to the host's byte order
fpfrac = ((fpfrac >> 24) & 0xff) | ((fpfrac << 8) & 0xff0000) | ((fpfrac >> 8) & 0xff00) | ((fpfrac << 24) & 0xff000000);
fpint = (fpint >> 8) | (fpint << 8);
int64_t fp_i64 = (static_cast<int64_t>(fpint) << 32) | (static_cast<int64_t>(fpfrac) & 0xffffffff);
double rv_d = static_cast<double>(fp_i64) / 4294967296.0;
return rv_d;
}
// float readFloat(const uint8_t *data, int &index) {
// Serial.print("Reading float at index: ");
// Serial.println(index);
// for (int i = 0; i < 4; i++) {
// Serial.print(data[index + i], HEX);
// Serial.print(" ");
// }
// Serial.println();
// uint32_t temp = readUint32(data, index);
// Serial.print("temp big endian = ");
// Serial.println(temp);
// float result;
// memcpy(&result, &temp, 4);
// Serial.print("result = ");
// Serial.println(result);
// return result;
// }
String bytesToHexString(const uint8_t *data, int length) {
String hexString = ""; // Create an empty string to hold the result
for (int i = 0; i < length; ++i) {
// Format each byte as a two-digit hexadecimal number and append to the string
if (data[i] < 0x10) {
// Add a leading zero for values less than 0x10 (16 in decimal)
hexString += "0";
}
hexString += String(data[i], HEX);
}
hexString.toUpperCase(); // Convert to uppercase if needed
return hexString;
}
//FA FF 36 2D 40 20 0C 3CE4BCAA 3FF5E331 4119537C 8020 0C BB9B0AA13C0722883BCCBAF3C0200CBD69C6C03D98C820BF3843441A
void readFloatMTi(float &axisValue, uint8_t *data, int ¤tIndex) {
Serial.print("index rfv1= "); Serial.println(currentIndex);
dataswapendian((uint8_t *)&data[currentIndex], 4);
memcpy(&axisValue, &data[currentIndex], 4);
currentIndex += 4;
Serial.print("index rfv2 = "); Serial.println(currentIndex);
}
void printRawXbus(const uint8_t *xbusData) {
if (!Xbus_checkPreamble(xbusData))
return "Invalid xbus message";
// int data_length = xbusData[3] + 5;
// String hexStr = bytesToHexString(xbusData, data_length);
// Serial.println(hexStr);
uint8_t messageId = Xbus_getMessageId(xbusData);
int index = 4;
switch (messageId) {
case XMID_Wakeup:
{
Serial.println("XMID_Wakeup");
}
break;
case XMID_DeviceId:
{
String deviceId_String = "";
for (int i = 4; i <= 7; i++) {
if (xbusData[i] < 0x10) {
// Ensure two characters for each byte, add leading zero if necessary
deviceId_String += "0";
}
deviceId_String += String(xbusData[i], HEX);
}
deviceId_String.toUpperCase(); // Convert the hex string to uppercase
g_textBuffer = "XMID_DeviceId: " + deviceId_String;
Serial.println(g_textBuffer);
}
break;
case XMID_ProductCode:
{
char productCode[21]; // 20 characters + 1 for null terminator
memcpy(productCode, xbusData + index, 20);
productCode[20] = '\0'; // Ensure null termination
g_textBuffer = "XMID_ProductCode: " + String(productCode);
Serial.println(g_textBuffer);
}
break;
case XMID_GotoConfigAck:
{
Serial.println("XMID_GotoConfigAck");
}
break;
case XMID_GotoMeasurementAck:
{
Serial.println("XMID_GotoMeasurementAck");
}
break;
case XMID_MtData2:
break;
case XMID_FirmwareRevision:
{
uint8_t major = readUint8(xbusData, index);
uint8_t minor = readUint8(xbusData, index);
uint8_t patch = readUint8(xbusData, index);
g_textBuffer = "Firmware revision: " + String(major) + "." + String(minor) + "." + String(patch);
Serial.println(g_textBuffer);
}
break;
case XMID_GotoBootLoaderAck:
{
g_textBuffer = "XMID_GotoBootLoaderAck";
Serial.println(g_textBuffer);
}
break;
case XMID_FirmwareUpdate:
{
g_textBuffer = "XMID_FirmwareUpdate";
Serial.println(g_textBuffer);
}
break;
case XMID_ResetAck:
{
g_textBuffer = "XMID_ResetAck";
Serial.println(g_textBuffer);
}
break;
case XMID_SetOutputConfigurationAck:
{
g_textBuffer = "XMID_SetOutputConfigurationAck";
Serial.println(g_textBuffer);
}
break;
default:
{
g_textBuffer = "Unhandled xbus message: MessageId = 0x" + String(messageId, HEX);
Serial.println(g_textBuffer);
}
}
}