Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_ADSB/sagetech-sdk/sgUtil.h
Views: 1799
/**1* @copyright Copyright (c) 2021 Sagetech, Inc. All rights reserved.2*3* @file sgUtil.h4* @author jimb5*6* @date Feb 2, 20217*/89#ifndef UTIL_H10#define UTIL_H1112#include <stdint.h>1314#ifndef swap1615#define swap16(data) \16((((data) >> 8) & 0x00FF) | (((data) << 8) & 0xFF00))17#endif1819#ifndef swap2420#define swap24(data) \21(((data) >> 16) | ((data)&0x00FF00) | (((data) << 16) & 0xFF0000))22#endif2324#ifndef swap3225#define swap32(data) \26(((data) >> 24) | (((data)&0x00FF0000) >> 8) | (((data)&0x0000FF00) << 8) | ((data) << 24))27#endif2829#ifndef swap6430#define swap64(data) \31(swap32((data & 0x00000000ffffffffULL))) << 32 | swap32(data >> 32))32#endif3334#ifndef PI35#define PI 3.1415926535936#endif3738#ifndef toRad39#define toRad(deg) \40((deg)*PI / 180.0)41#endif4243#ifndef toDeg44#define toDeg(rad) \45((rad)*180 / PI)46#endif4748#ifndef toMeter49#define toMeter(feet) \50((feet)*0.3048)51#endif5253#ifndef toFeet54#define toFeet(meter) \55((meter)*3.2808)56#endif5758/**59* Converts an array of bytes to a 16 bit integer.60*61* @param bytes The array of bytes to convert.62*63* @return The 16 bit integer.64*/65int16_t toInt16(const uint8_t bytes[]);6667/**68* Converts an array of bytes to a 32 bit integer.69*70* @param bytes The array of bytes to convert.71*72* @return The 32 bit integer.73*/74int32_t toInt32(const uint8_t bytes[]);7576/**77* Converts an array of bytes to a 16 unsigned bit integer.78*79* @param bytes The array of bytes to convert.80*81* @return The 16 bit integer.82*/83uint16_t toUint16(const uint8_t bytes[]);8485/**86* Converts an array of bytes to a 24 bit unsigned integer with leading 0s.87*88* @param bytes The array of bytes to convert.89*90* @return The 24 bit unsigned integer with leading 0s.91*/92uint32_t toUint24(const uint8_t bytes[]);9394/**95* Converts an array of bytes to a 32 bit unsigned integer.96*97* @param bytes The array of bytes to convert.98*99* @return The 32 bit unsigned integer.100*/101uint32_t toUint32(const uint8_t bytes[]);102103/**104* Converts an array of bytes to a distance.105*106* @param bytes The array of bytes to convert.107*108* @return The distance value.109*/110double toDist(const uint8_t *bytes);111112/**113* Converts an array of bytes to a 24 bit unsigned integer with leading 0's.114*115* @param bytes The array of bytes to convert.116*117* @return The 32 bit unsigned integer.118*/119uint32_t toIcao(const uint8_t bytes[]);120121/**122* Converts an array of bytes to a lat/lon floating point number.123*124* @param bytes The array of bytes to convert.125*126* @return The lat/lon value.127*/128double toLatLon(const uint8_t bytes[]);129130/**131* Convert an array to an altitude.132*133* @param bytes The bytes to get the altitude from.134*135* @return The converted altitude.136*/137double toAlt(const uint8_t bytes[]);138139/**140* Converts an array of bytes to an airborne velocity.141*142* @param bytes The bytes to extract the velocity.143*144* @return The converted velocity.145*/146double toVel(const uint8_t bytes[]);147148/**149* Converts the array of bytes to the surface ground speed.150*151* @param bytes The bytes to extract the ground speed.152*153* @return The converted ground speed.154*/155uint8_t toGS(const uint8_t bytes[]);156157/**158* Converts the bytes into the heading value.159*160* @param bytes The bytes holding the heading value.161*162* @return The heading.163*/164double toHeading(const uint8_t bytes[]);165166/**167* Determine heading from y and x speed vectors.168*169* @param y The y speed vector.170* @param x The x speed vector.171*172* @return The resulting heading.173*/174int16_t toHeading2(double y, double x);175176/**177* Convert the array of bytes to a time of applicability (TOA).178*179* @param bytes The bytes to convert to a TOA.180*181* @return The converted TOA.182*/183float toTOA(const uint8_t bytes[]);184185/**186* Convert an array of bytes to a float187*188* @param bufferPos the address of the field's first corresponding buffer byte.189*190* @return The converted float value.191*/192193float toFloat(const uint8_t *bufferPos);194195/**196* Convert an array of bytes to a double197*198* @param bufferPos the address of the field's first corresponding buffer byte.199*200* @return The converted double value.201*/202203double toDouble(const uint8_t *bufferPos);204205/**206* Converts a uint16_t into its host message buffer format207*208* @param bufferPos The address of the field's first corresponding buffer byte.209* @param value The uint16_t to be converted.210*211* no return value, two buffer bytes are filled by reference212*/213void uint162Buf(uint8_t *bufferPos, uint16_t value);214215/**216* Converts a int16_t into its host message buffer format217*218* @param bufferPos The address of the field's first corresponding buffer byte.219* @param value The int32_t to be converted.220*221* no return value, two buffer bytes are filled by reference222*/223void int242Buf(uint8_t *bufferPos, int32_t value);224225/**226* Converts a uint32_t into a 24 bit host message buffer format227*228* @param bufferPos The address of the field's first corresponding buffer byte.229* @param value The int32_t to be converted.230*231* no return value, three buffer bytes are filled by reference232*/233void uint242Buf(uint8_t *bufferPos, uint32_t value);234235/**236* Converts a uint32_t into its host message buffer format237*238* @param bufferPos The address of the field's first corresponding buffer byte.239* @param value The uint32_t to be converted.240*241* no return value, two buffer bytes are filled by reference242*/243void uint322Buf(uint8_t *bufferPos, uint32_t value);244245/**246* Converts a uint32_t containing an ICAO into its 24-bit host message buffer format247*248* @param bufferPos The address of the field's first corresponding buffer byte.249* @param icao The uint32_t ICAO to be converted.250*251* no return value, three buffer bytes are filled by reference252*253* @warning icao parameter must be between 0x000000 and 0xFFFFFF254*/255void icao2Buf(uint8_t *bufferPos, uint32_t icao);256257/**258* Converts an array of characters into its host message buffer format259*260* @param bufferPos The address of the field's first corresponding buffer byte.261* @param arr[] The array of characters.262* @param len The number of characters in the array.263*264* no return value, the specified quantity of buffer bytes are filled by reference265*/266void charArray2Buf(uint8_t *bufferPos, char arr[], uint8_t len);267268/**269* Converts a float into its host message buffer format270*271* @param bufferPos The address of the field's first corresponding buffer byte.272* @param value The float to be converted.273*274* no return value, four buffer bytes are filled by reference275*276* @warning The output of this function depends on the machine's endianness. It is designed277* for Little-Endian machines, only.278*/279void float2Buf(uint8_t *bufferPos, float value);280281/**282* Converts a double into its host message buffer format283*284* @param bufferPos The address of the field's first corresponding buffer byte285* @param value The double to be converted286*287* no return value, eight buffer bytes are filled by reference288*289* @warning The output of this function depends on the machine's endianness. It is designed290* for Little-Endian machines, only291*/292void double2Buf(uint8_t *bufferPos, double value);293294/**295* Converts a double into an encoded lat/lon buffer format.296*297* @param bytes address of the field's first corresponding buffer byte298* @param value the double to be converted.299*300* no return value, 3 buffer bytes are filled by reference.301*/302void latLon2Buf(uint8_t bytes[], double value);303304/**305* Calculate checksum for a host message.306*307* @param buffer The raw message buffer.308* @param len The total quantity of bytes in the buffer309*310* @return The resulting checksum.311*/312uint8_t calcChecksum(uint8_t *buffer, uint8_t len);313314/**315* Add the checksum to a host message.316*317* @param buffer The raw message buffer.318* @param len The total quantity of bytes in the buffer319*320* no return value, final buffer byte is set to the checksum value.321*/322void appendChecksum(uint8_t *buffer, uint8_t len);323324#endif /* UTIL_H */325326327