Line data Source code
1 : /* 2 : * This file is free software: you can redistribute it and/or modify it 3 : * under the terms of the GNU General Public License as published by the 4 : * Free Software Foundation, either version 3 of the License, or 5 : * (at your option) any later version. 6 : * 7 : * This file is distributed in the hope that it will be useful, but 8 : * WITHOUT ANY WARRANTY; without even the implied warranty of 9 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 10 : * See the GNU General Public License for more details. 11 : * 12 : * You should have received a copy of the GNU General Public License along 13 : * with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : 16 : #pragma once 17 : 18 : #include "AP_RCProtocol_config.h" 19 : 20 : #if AP_RCPROTOCOL_SRXL2_ENABLED 21 : 22 : #include "AP_RCProtocol.h" 23 : #include "SoftSerial.h" 24 : 25 : #define SRXL2_MAX_CHANNELS 32U /* Maximum number of channels from srxl2 datastream */ 26 : #define SRXL2_FRAMELEN_MAX 80U /* maximum possible framelengh */ 27 : #define SRXL2_HEADER_LEN 3U 28 : 29 : class AP_RCProtocol_SRXL2 : public AP_RCProtocol_Backend { 30 : public: 31 : AP_RCProtocol_SRXL2(AP_RCProtocol &_frontend); 32 : virtual ~AP_RCProtocol_SRXL2(); 33 : void process_byte(uint8_t byte, uint32_t baudrate) override; 34 : void process_handshake(uint32_t baudrate) override; 35 : void start_bind(void) override; 36 : void update(void) override; 37 : // get singleton instance 38 0 : static AP_RCProtocol_SRXL2* get_singleton() { 39 0 : return _singleton; 40 : } 41 : 42 : void capture_scaled_input(const uint8_t *values_p, bool in_failsafe, int16_t rssi); 43 : void send_on_uart(uint8_t* pBuffer, uint8_t length); 44 : void change_baud_rate(uint32_t baudrate); 45 : 46 : private: 47 : 48 : static AP_RCProtocol_SRXL2* _singleton; 49 : 50 : void _process_byte(uint32_t timestamp_us, uint8_t byte); 51 : void _bootstrap(uint8_t device_id); 52 0 : bool is_bootstrapped() const { return _device_id != 0; } 53 : 54 : uint8_t _buffer[SRXL2_FRAMELEN_MAX]; /* buffer for raw srxl frame data in correct order --> buffer[0]=byte0 buffer[1]=byte1 */ 55 : uint8_t _buflen; /* length in number of bytes of received srxl dataframe in buffer */ 56 : uint32_t _last_run_ms; // last time the state machine was run 57 : uint16_t _channels[SRXL2_MAX_CHANNELS < MAX_RCIN_CHANNELS ? SRXL2_MAX_CHANNELS : MAX_RCIN_CHANNELS]; /* buffer for extracted RC channel data as pulsewidth in microseconds */ 58 : bool _in_bootstrap_or_failsafe; // controls whether we allow UART sends outside a receive time constraint 59 : uint8_t _device_id; 60 : 61 : enum { 62 : STATE_IDLE, /* do nothing */ 63 : STATE_NEW, /* get header of frame + prepare for frame reception */ 64 : STATE_COLLECT /* collect RC channel data from frame */ 65 : }; 66 : uint8_t _frame_len_full; /* Length in number of bytes of full srxl datastream */ 67 : uint8_t _decode_state; /* Current state of SRXL frame decoding */ 68 : uint8_t _decode_state_next; /* State of frame decoding that will be applied when the next byte from dataframe drops in */ 69 : bool _in_failsafe = false; 70 : int16_t _new_rssi = -1; 71 : uint32_t _last_handshake_ms; 72 : uint32_t _handshake_start_ms; 73 : }; 74 : 75 : #endif // AP_RCPROTOCOL_SRXL2_ENABLED