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_Frsky_Telem/AP_Frsky_MAVlite_SPortToMAVlite.cpp
Views: 1798
#include "AP_Frsky_MAVlite_SPortToMAVlite.h"12#include "AP_Frsky_MAVlite.h"34#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL5void AP_Frsky_MAVlite_SPortToMAVlite::reset(void)6{7checksum = 0;89expected_seq = 0;10payload_next_byte = 0;11parse_state = State::WANT_LEN;12}1314void AP_Frsky_MAVlite_SPortToMAVlite::update_checksum(const uint8_t c)15{16checksum += c; //0-1FF17checksum += checksum >> 8;18checksum &= 0xFF;19}2021/*22Parses sport packets and if successful fills the rxmsg mavlite struct23*/24bool AP_Frsky_MAVlite_SPortToMAVlite::process(AP_Frsky_MAVlite_Message &rxmsg, const AP_Frsky_SPort::sport_packet_t &packet)25{26// the two skipped bytes in packet.raw here are sensor and frame.27// appid and data are used to transport the mavlite message.2829// deal with packet sequence number:30const uint8_t received_seq = (packet.raw[2] & 0x3F);31// if the first byte of any sport passthrough packet is zero then we reset:32if (received_seq == 0) {33reset();34}35if (received_seq != expected_seq) {36parse_state = State::ERROR;37return false;38}39update_checksum(received_seq);40expected_seq = received_seq + 1;4142// deal with the remainder (post-sequence) of the packet:43for (uint8_t i=3; i<ARRAY_SIZE(packet.raw); i++) {44parse(packet.raw[i]);45}46if (parse_state == State::MESSAGE_RECEIVED) {47rxmsg = _rxmsg;48return true;49}50return false;51}5253void AP_Frsky_MAVlite_SPortToMAVlite::parse(uint8_t byte)54{55switch (parse_state) {5657case State::IDLE:58// it is an error to receive anything but offset==0 byte=0xx059// in this state60parse_state = State::ERROR;61return;6263case State::ERROR:64// waiting for offset==0 && byte==0x00 to bump us into WANT_LEN65return;6667case State::WANT_LEN:68_rxmsg.len = byte;69update_checksum(byte);70parse_state = State::WANT_MSGID;71return;7273case State::WANT_MSGID:74_rxmsg.msgid = byte;75update_checksum(byte);76if (_rxmsg.len == 0) {77parse_state = State::WANT_CHECKSUM;78} else {79parse_state = State::WANT_PAYLOAD;80}81return;8283case State::WANT_PAYLOAD:84// add byte to payload85_rxmsg.payload[payload_next_byte++] = byte;86update_checksum(byte);8788if (payload_next_byte >= _rxmsg.len) {89parse_state = State::WANT_CHECKSUM;90}91return;9293case State::WANT_CHECKSUM:94if (checksum != byte) {95parse_state = State::ERROR;96return;97}98parse_state = State::MESSAGE_RECEIVED;99return;100101case State::MESSAGE_RECEIVED:102return;103}104}105#endif106107