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 : * Code by Andrew Tridgell and Siddharth Bharat Purohit 16 : */ 17 : 18 : #pragma once 19 : 20 : #include "AP_RCProtocol_config.h" 21 : 22 : #if AP_RCPROTOCOL_DSM_ENABLED 23 : 24 : #include "AP_RCProtocol_Backend.h" 25 : 26 : #include "SoftSerial.h" 27 : 28 : #define AP_DSM_MAX_CHANNELS 12 29 : 30 : class AP_RCProtocol_DSM : public AP_RCProtocol_Backend { 31 : public: 32 2094 : AP_RCProtocol_DSM(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {} 33 : void process_pulse(uint32_t width_s0, uint32_t width_s1) override; 34 : void process_byte(uint8_t byte, uint32_t baudrate) override; 35 : void start_bind(void) override; 36 : void update(void) override; 37 : 38 : private: 39 : void _process_byte(uint32_t timestamp_ms, uint8_t byte); 40 : void dsm_decode(); 41 : bool dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value); 42 : void dsm_guess_format(bool reset, const uint8_t dsm_frame[16], unsigned frame_channels); 43 : bool dsm_parse_byte(uint32_t frame_time_ms, uint8_t b, uint16_t *values, 44 : uint16_t *num_values, uint16_t max_channels); 45 : bool dsm_decode(uint32_t frame_time_ms, const uint8_t dsm_frame[16], 46 : uint16_t *values, uint16_t *num_values, uint16_t max_values); 47 : 48 : /**< Channel resolution, 0=unknown, 10=10 bit, 11=11 bit */ 49 : uint8_t channel_shift; 50 : 51 : // format guessing state 52 : uint32_t cs10; 53 : uint32_t cs11; 54 : uint32_t samples; 55 : 56 : // bind state machine 57 : enum { 58 : BIND_STATE_NONE, 59 : BIND_STATE1, 60 : BIND_STATE2, 61 : BIND_STATE3, 62 : BIND_STATE4, 63 : } bind_state; 64 : uint32_t bind_last_ms; 65 : uint32_t bind_mode_saved; 66 : 67 : uint16_t last_values[AP_DSM_MAX_CHANNELS]; 68 : 69 : struct { 70 : uint8_t buf[16]; 71 : uint8_t ofs; 72 : } byte_input; 73 : 74 : enum DSM_DECODE_STATE { 75 : DSM_DECODE_STATE_DESYNC = 0, 76 : DSM_DECODE_STATE_SYNC 77 : } dsm_decode_state; 78 : 79 : uint32_t last_frame_time_ms; 80 : uint32_t last_rx_time_ms; 81 : uint16_t chan_count; 82 : 83 : SoftSerial ss{115200, SoftSerial::SERIAL_CONFIG_8N1}; 84 : }; 85 : 86 : #endif // AP_RCPROTOCOL_DSM_ENABLED