CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_ADSB/sagetech-sdk/sgUtil.h
Views: 1799
1
/**
2
* @copyright Copyright (c) 2021 Sagetech, Inc. All rights reserved.
3
*
4
* @file sgUtil.h
5
* @author jimb
6
*
7
* @date Feb 2, 2021
8
*/
9
10
#ifndef UTIL_H
11
#define UTIL_H
12
13
#include <stdint.h>
14
15
#ifndef swap16
16
#define swap16(data) \
17
((((data) >> 8) & 0x00FF) | (((data) << 8) & 0xFF00))
18
#endif
19
20
#ifndef swap24
21
#define swap24(data) \
22
(((data) >> 16) | ((data)&0x00FF00) | (((data) << 16) & 0xFF0000))
23
#endif
24
25
#ifndef swap32
26
#define swap32(data) \
27
(((data) >> 24) | (((data)&0x00FF0000) >> 8) | (((data)&0x0000FF00) << 8) | ((data) << 24))
28
#endif
29
30
#ifndef swap64
31
#define swap64(data) \
32
(swap32((data & 0x00000000ffffffffULL))) << 32 | swap32(data >> 32))
33
#endif
34
35
#ifndef PI
36
#define PI 3.14159265359
37
#endif
38
39
#ifndef toRad
40
#define toRad(deg) \
41
((deg)*PI / 180.0)
42
#endif
43
44
#ifndef toDeg
45
#define toDeg(rad) \
46
((rad)*180 / PI)
47
#endif
48
49
#ifndef toMeter
50
#define toMeter(feet) \
51
((feet)*0.3048)
52
#endif
53
54
#ifndef toFeet
55
#define toFeet(meter) \
56
((meter)*3.2808)
57
#endif
58
59
/**
60
* Converts an array of bytes to a 16 bit integer.
61
*
62
* @param bytes The array of bytes to convert.
63
*
64
* @return The 16 bit integer.
65
*/
66
int16_t toInt16(const uint8_t bytes[]);
67
68
/**
69
* Converts an array of bytes to a 32 bit integer.
70
*
71
* @param bytes The array of bytes to convert.
72
*
73
* @return The 32 bit integer.
74
*/
75
int32_t toInt32(const uint8_t bytes[]);
76
77
/**
78
* Converts an array of bytes to a 16 unsigned bit integer.
79
*
80
* @param bytes The array of bytes to convert.
81
*
82
* @return The 16 bit integer.
83
*/
84
uint16_t toUint16(const uint8_t bytes[]);
85
86
/**
87
* Converts an array of bytes to a 24 bit unsigned integer with leading 0s.
88
*
89
* @param bytes The array of bytes to convert.
90
*
91
* @return The 24 bit unsigned integer with leading 0s.
92
*/
93
uint32_t toUint24(const uint8_t bytes[]);
94
95
/**
96
* Converts an array of bytes to a 32 bit unsigned integer.
97
*
98
* @param bytes The array of bytes to convert.
99
*
100
* @return The 32 bit unsigned integer.
101
*/
102
uint32_t toUint32(const uint8_t bytes[]);
103
104
/**
105
* Converts an array of bytes to a distance.
106
*
107
* @param bytes The array of bytes to convert.
108
*
109
* @return The distance value.
110
*/
111
double toDist(const uint8_t *bytes);
112
113
/**
114
* Converts an array of bytes to a 24 bit unsigned integer with leading 0's.
115
*
116
* @param bytes The array of bytes to convert.
117
*
118
* @return The 32 bit unsigned integer.
119
*/
120
uint32_t toIcao(const uint8_t bytes[]);
121
122
/**
123
* Converts an array of bytes to a lat/lon floating point number.
124
*
125
* @param bytes The array of bytes to convert.
126
*
127
* @return The lat/lon value.
128
*/
129
double toLatLon(const uint8_t bytes[]);
130
131
/**
132
* Convert an array to an altitude.
133
*
134
* @param bytes The bytes to get the altitude from.
135
*
136
* @return The converted altitude.
137
*/
138
double toAlt(const uint8_t bytes[]);
139
140
/**
141
* Converts an array of bytes to an airborne velocity.
142
*
143
* @param bytes The bytes to extract the velocity.
144
*
145
* @return The converted velocity.
146
*/
147
double toVel(const uint8_t bytes[]);
148
149
/**
150
* Converts the array of bytes to the surface ground speed.
151
*
152
* @param bytes The bytes to extract the ground speed.
153
*
154
* @return The converted ground speed.
155
*/
156
uint8_t toGS(const uint8_t bytes[]);
157
158
/**
159
* Converts the bytes into the heading value.
160
*
161
* @param bytes The bytes holding the heading value.
162
*
163
* @return The heading.
164
*/
165
double toHeading(const uint8_t bytes[]);
166
167
/**
168
* Determine heading from y and x speed vectors.
169
*
170
* @param y The y speed vector.
171
* @param x The x speed vector.
172
*
173
* @return The resulting heading.
174
*/
175
int16_t toHeading2(double y, double x);
176
177
/**
178
* Convert the array of bytes to a time of applicability (TOA).
179
*
180
* @param bytes The bytes to convert to a TOA.
181
*
182
* @return The converted TOA.
183
*/
184
float toTOA(const uint8_t bytes[]);
185
186
/**
187
* Convert an array of bytes to a float
188
*
189
* @param bufferPos the address of the field's first corresponding buffer byte.
190
*
191
* @return The converted float value.
192
*/
193
194
float toFloat(const uint8_t *bufferPos);
195
196
/**
197
* Convert an array of bytes to a double
198
*
199
* @param bufferPos the address of the field's first corresponding buffer byte.
200
*
201
* @return The converted double value.
202
*/
203
204
double toDouble(const uint8_t *bufferPos);
205
206
/**
207
* Converts a uint16_t into its host message buffer format
208
*
209
* @param bufferPos The address of the field's first corresponding buffer byte.
210
* @param value The uint16_t to be converted.
211
*
212
* no return value, two buffer bytes are filled by reference
213
*/
214
void uint162Buf(uint8_t *bufferPos, uint16_t value);
215
216
/**
217
* Converts a int16_t into its host message buffer format
218
*
219
* @param bufferPos The address of the field's first corresponding buffer byte.
220
* @param value The int32_t to be converted.
221
*
222
* no return value, two buffer bytes are filled by reference
223
*/
224
void int242Buf(uint8_t *bufferPos, int32_t value);
225
226
/**
227
* Converts a uint32_t into a 24 bit host message buffer format
228
*
229
* @param bufferPos The address of the field's first corresponding buffer byte.
230
* @param value The int32_t to be converted.
231
*
232
* no return value, three buffer bytes are filled by reference
233
*/
234
void uint242Buf(uint8_t *bufferPos, uint32_t value);
235
236
/**
237
* Converts a uint32_t into its host message buffer format
238
*
239
* @param bufferPos The address of the field's first corresponding buffer byte.
240
* @param value The uint32_t to be converted.
241
*
242
* no return value, two buffer bytes are filled by reference
243
*/
244
void uint322Buf(uint8_t *bufferPos, uint32_t value);
245
246
/**
247
* Converts a uint32_t containing an ICAO into its 24-bit host message buffer format
248
*
249
* @param bufferPos The address of the field's first corresponding buffer byte.
250
* @param icao The uint32_t ICAO to be converted.
251
*
252
* no return value, three buffer bytes are filled by reference
253
*
254
* @warning icao parameter must be between 0x000000 and 0xFFFFFF
255
*/
256
void icao2Buf(uint8_t *bufferPos, uint32_t icao);
257
258
/**
259
* Converts an array of characters into its host message buffer format
260
*
261
* @param bufferPos The address of the field's first corresponding buffer byte.
262
* @param arr[] The array of characters.
263
* @param len The number of characters in the array.
264
*
265
* no return value, the specified quantity of buffer bytes are filled by reference
266
*/
267
void charArray2Buf(uint8_t *bufferPos, char arr[], uint8_t len);
268
269
/**
270
* Converts a float into its host message buffer format
271
*
272
* @param bufferPos The address of the field's first corresponding buffer byte.
273
* @param value The float to be converted.
274
*
275
* no return value, four buffer bytes are filled by reference
276
*
277
* @warning The output of this function depends on the machine's endianness. It is designed
278
* for Little-Endian machines, only.
279
*/
280
void float2Buf(uint8_t *bufferPos, float value);
281
282
/**
283
* Converts a double into its host message buffer format
284
*
285
* @param bufferPos The address of the field's first corresponding buffer byte
286
* @param value The double to be converted
287
*
288
* no return value, eight buffer bytes are filled by reference
289
*
290
* @warning The output of this function depends on the machine's endianness. It is designed
291
* for Little-Endian machines, only
292
*/
293
void double2Buf(uint8_t *bufferPos, double value);
294
295
/**
296
* Converts a double into an encoded lat/lon buffer format.
297
*
298
* @param bytes address of the field's first corresponding buffer byte
299
* @param value the double to be converted.
300
*
301
* no return value, 3 buffer bytes are filled by reference.
302
*/
303
void latLon2Buf(uint8_t bytes[], double value);
304
305
/**
306
* Calculate checksum for a host message.
307
*
308
* @param buffer The raw message buffer.
309
* @param len The total quantity of bytes in the buffer
310
*
311
* @return The resulting checksum.
312
*/
313
uint8_t calcChecksum(uint8_t *buffer, uint8_t len);
314
315
/**
316
* Add the checksum to a host message.
317
*
318
* @param buffer The raw message buffer.
319
* @param len The total quantity of bytes in the buffer
320
*
321
* no return value, final buffer byte is set to the checksum value.
322
*/
323
void appendChecksum(uint8_t *buffer, uint8_t len);
324
325
#endif /* UTIL_H */
326
327