Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / telemetry / frsky.cpp
blob0fe1a5bfd58fdf4b7444fbe226a8e153a0363304
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
23 void processFrskyTelemetryData(uint8_t data)
25 #if defined(PCBSKY9X) && defined(BLUETOOTH)
26 // TODO if (g_model.bt_telemetry)
27 btPushByte(data);
28 #endif
30 #if defined(AUX_SERIAL)
31 if (g_eeGeneral.auxSerialMode == UART_MODE_TELEMETRY_MIRROR) {
32 auxSerialPutc(data);
34 #endif
36 if (pushFrskyTelemetryData(data)) {
37 if (IS_FRSKY_SPORT_PROTOCOL()) {
38 sportProcessTelemetryPacket(telemetryRxBuffer);
40 else {
41 frskyDProcessPacket(telemetryRxBuffer);
46 bool pushFrskyTelemetryData(uint8_t data)
48 static uint8_t dataState = STATE_DATA_IDLE;
50 switch (dataState) {
51 case STATE_DATA_START:
52 if (data == START_STOP) {
53 if (IS_FRSKY_SPORT_PROTOCOL()) {
54 dataState = STATE_DATA_IN_FRAME ;
55 telemetryRxBufferCount = 0;
58 else {
59 if (telemetryRxBufferCount < TELEMETRY_RX_PACKET_SIZE) {
60 telemetryRxBuffer[telemetryRxBufferCount++] = data;
62 dataState = STATE_DATA_IN_FRAME;
64 break;
66 case STATE_DATA_IN_FRAME:
67 if (data == BYTE_STUFF) {
68 dataState = STATE_DATA_XOR; // XOR next byte
70 else if (data == START_STOP) {
71 if (IS_FRSKY_SPORT_PROTOCOL()) {
72 dataState = STATE_DATA_IN_FRAME ;
73 telemetryRxBufferCount = 0;
75 else {
76 // end of frame detected
77 dataState = STATE_DATA_IDLE;
78 return true;
80 break;
82 else if (telemetryRxBufferCount < TELEMETRY_RX_PACKET_SIZE) {
83 telemetryRxBuffer[telemetryRxBufferCount++] = data;
85 break;
87 case STATE_DATA_XOR:
88 if (telemetryRxBufferCount < TELEMETRY_RX_PACKET_SIZE) {
89 telemetryRxBuffer[telemetryRxBufferCount++] = data ^ STUFF_MASK;
91 dataState = STATE_DATA_IN_FRAME;
92 break;
94 case STATE_DATA_IDLE:
95 if (data == START_STOP) {
96 telemetryRxBufferCount = 0;
97 dataState = STATE_DATA_START;
99 break;
101 } // switch
103 if (IS_FRSKY_SPORT_PROTOCOL() && telemetryRxBufferCount >= FRSKY_SPORT_PACKET_SIZE) {
104 // end of frame detected
105 dataState = STATE_DATA_IDLE;
106 return true;
109 return false;