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/sgEncodeOperating.c
Views: 1799
/**1* @copyright Copyright (c) 2021 Sagetech, Inc. All rights reserved.2*3* @file sgEncodeOperating.c4* @author Jacob.Garrison5*6* @date Feb 15, 20217*8* This file receives a populated operating message struct and9* converts it into a operating message buffer.10*/1112#include <stdbool.h>13#include <stdlib.h>1415#include "sg.h"16#include "sgUtil.h"1718#define SG_PAYLOAD_LEN_OPMSG SG_MSG_LEN_OPMSG - 5 /// the payload length.1920#define PBASE 4 /// the payload offset.21#define OFFSET_SQUAWK 0 /// the squawk code offset in the payload.22#define OFFSET_CONFIG 2 /// the mode/config offset in the payload.23#define OFFSET_EMRG_ID 3 /// the emergency flag offset in the payload.24#define OFFSET_ALT 4 /// the altitude offset in the payload.25#define OFFSET_RATE 6 /// the climb rate offset in the payload.26#define OFFSET_HEADING 8 /// the heading offset in the payload.27#define OFFSET_AIRSPEED 10 /// the airspeed offset in the payload.2829/*30* Documented in the header file.31*/32bool sgEncodeOperating(uint8_t *buffer, sg_operating_t *op, uint8_t msgId)33{34// populate header35buffer[0] = SG_MSG_START_BYTE;36buffer[1] = SG_MSG_TYPE_HOST_OPMSG;37buffer[2] = msgId;38buffer[3] = SG_PAYLOAD_LEN_OPMSG;3940// populate Squawk code41uint162Buf(&buffer[PBASE + OFFSET_SQUAWK], op->squawk);4243// populate Mode/Config44buffer[PBASE + OFFSET_CONFIG] = op->milEmergency << 5 |45op->enableXBit << 4 |46op->enableSqt << 3 |47op->savePowerUp << 2 |48op->opMode;4950// populate Emergency/Ident51buffer[PBASE + OFFSET_EMRG_ID] = op->identOn << 3 |52op->emergcType;5354// populate Altitude55uint16_t altCode = 0;56if (op->altUseIntrnl)57{58altCode = 0x8000;59}60else if (op->altHostAvlbl)61{62// 100 foot encoding conversion63altCode = (op->altitude + 1200) / 100;6465if (op->altRes25)66{67altCode *= 4;68}6970// 'Host altitude available' flag71altCode += 0x4000;72}73uint162Buf(&buffer[PBASE + OFFSET_ALT], altCode);7475// populate Altitude Rate76int16_t rate = op->climbRate / 64;77if (!op->climbValid)78{79rate = 0x8000;80}81uint162Buf(&buffer[PBASE + OFFSET_RATE], rate);8283// populate Heading84// conversion: heading * ( pow(2, 15) / 360 )85uint16_t heading = op->heading * 32768 / 360;86if (op->headingValid)87{88heading += 0x8000;89}90uint162Buf(&buffer[PBASE + OFFSET_HEADING], heading);9192// populate Airspeed93uint16_t airspeed = op->airspd;94if (op->airspdValid)95{96airspeed += 0x8000;97}98uint162Buf(&buffer[PBASE + OFFSET_AIRSPEED], airspeed);99100// populate checksum101appendChecksum(buffer, SG_MSG_LEN_OPMSG);102103return true;104}105106107