2020-03-23 16:20:00 -03:00
/*
* This file is free software : you can redistribute it and / or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This file is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
* See the GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# pragma once
2023-04-26 04:19:07 -03:00
# include "AP_RCProtocol_config.h"
# if AP_RCPROTOCOL_SRXL2_ENABLED
2020-03-23 16:20:00 -03:00
# include "AP_RCProtocol.h"
# include "SoftSerial.h"
# define SRXL2_MAX_CHANNELS 32U /* Maximum number of channels from srxl2 datastream */
# define SRXL2_FRAMELEN_MAX 80U /* maximum possible framelengh */
# define SRXL2_HEADER_LEN 3U
class AP_RCProtocol_SRXL2 : public AP_RCProtocol_Backend {
public :
AP_RCProtocol_SRXL2 ( AP_RCProtocol & _frontend ) ;
2020-04-30 16:34:51 -03:00
virtual ~ AP_RCProtocol_SRXL2 ( ) ;
2020-03-23 16:20:00 -03:00
void process_byte ( uint8_t byte , uint32_t baudrate ) override ;
2020-05-09 05:20:33 -03:00
void process_handshake ( uint32_t baudrate ) override ;
2020-03-23 16:20:00 -03:00
void start_bind ( void ) override ;
void update ( void ) override ;
// get singleton instance
static AP_RCProtocol_SRXL2 * get_singleton ( ) {
return _singleton ;
}
2023-04-26 10:16:32 -03:00
void capture_scaled_input ( const uint8_t * values_p , bool in_failsafe , int16_t rssi ) ;
void send_on_uart ( uint8_t * pBuffer , uint8_t length ) ;
void change_baud_rate ( uint32_t baudrate ) ;
2020-03-23 16:20:00 -03:00
private :
static AP_RCProtocol_SRXL2 * _singleton ;
void _process_byte ( uint32_t timestamp_us , uint8_t byte ) ;
2020-05-09 05:20:33 -03:00
void _bootstrap ( uint8_t device_id ) ;
bool is_bootstrapped ( ) const { return _device_id ! = 0 ; }
2020-03-23 16:20:00 -03:00
uint8_t _buffer [ SRXL2_FRAMELEN_MAX ] ; /* buffer for raw srxl frame data in correct order --> buffer[0]=byte0 buffer[1]=byte1 */
uint8_t _buflen ; /* length in number of bytes of received srxl dataframe in buffer */
uint32_t _last_run_ms ; // last time the state machine was run
2023-03-15 08:57:24 -03:00
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 */
2020-05-09 05:20:33 -03:00
bool _in_bootstrap_or_failsafe ; // controls whether we allow UART sends outside a receive time constraint
uint8_t _device_id ;
2020-03-23 16:20:00 -03:00
enum {
STATE_IDLE , /* do nothing */
STATE_NEW , /* get header of frame + prepare for frame reception */
STATE_COLLECT /* collect RC channel data from frame */
} ;
2020-05-09 05:20:33 -03:00
uint8_t _frame_len_full ; /* Length in number of bytes of full srxl datastream */
uint8_t _decode_state ; /* Current state of SRXL frame decoding */
uint8_t _decode_state_next ; /* State of frame decoding that will be applied when the next byte from dataframe drops in */
2020-03-23 16:20:00 -03:00
bool _in_failsafe = false ;
int16_t _new_rssi = - 1 ;
2020-05-09 05:20:33 -03:00
uint32_t _last_handshake_ms ;
uint32_t _handshake_start_ms ;
2020-03-23 16:20:00 -03:00
} ;
2023-04-26 04:19:07 -03:00
# endif // AP_RCPROTOCOL_SRXL2_ENABLED