2018-01-17 15:31:11 -04: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/>.
|
2018-07-20 01:25:40 -03:00
|
|
|
*
|
2018-01-17 15:31:11 -04:00
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_RCProtocol.h"
|
2018-11-04 23:56:00 -04:00
|
|
|
#include "SoftSerial.h"
|
2018-01-17 15:31:11 -04:00
|
|
|
|
2018-11-15 02:04:54 -04:00
|
|
|
#define AP_DSM_MAX_CHANNELS 12
|
|
|
|
|
2018-01-17 15:31:11 -04:00
|
|
|
class AP_RCProtocol_DSM : public AP_RCProtocol_Backend {
|
|
|
|
public:
|
|
|
|
AP_RCProtocol_DSM(AP_RCProtocol &_frontend) : AP_RCProtocol_Backend(_frontend) {}
|
|
|
|
void process_pulse(uint32_t width_s0, uint32_t width_s1) override;
|
2018-11-02 06:42:29 -03:00
|
|
|
void process_byte(uint8_t byte, uint32_t baudrate) override;
|
2018-04-10 03:14:54 -03:00
|
|
|
void start_bind(void) override;
|
|
|
|
void update(void) override;
|
2018-07-20 01:25:40 -03:00
|
|
|
|
2018-01-17 15:31:11 -04:00
|
|
|
private:
|
2018-11-26 02:04:01 -04:00
|
|
|
void _process_byte(uint32_t timestamp_ms, uint8_t byte);
|
2020-08-16 21:14:37 -03:00
|
|
|
void dsm_decode();
|
|
|
|
bool dsm_decode_channel(uint16_t raw, unsigned shift, unsigned *channel, unsigned *value);
|
2020-08-24 17:56:49 -03:00
|
|
|
void dsm_guess_format(bool reset, const uint8_t dsm_frame[16], unsigned frame_channels);
|
2020-08-16 21:14:37 -03:00
|
|
|
bool dsm_parse_byte(uint32_t frame_time_ms, uint8_t b, uint16_t *values,
|
2018-11-26 02:04:01 -04:00
|
|
|
uint16_t *num_values, uint16_t max_channels);
|
2020-08-16 21:14:37 -03:00
|
|
|
bool dsm_decode(uint32_t frame_time_ms, const uint8_t dsm_frame[16],
|
2018-01-17 15:31:11 -04:00
|
|
|
uint16_t *values, uint16_t *num_values, uint16_t max_values);
|
|
|
|
|
2020-08-16 21:14:37 -03:00
|
|
|
/**< Channel resolution, 0=unknown, 10=10 bit, 11=11 bit */
|
2018-11-26 02:04:01 -04:00
|
|
|
uint8_t channel_shift;
|
2020-08-16 21:14:37 -03:00
|
|
|
|
|
|
|
// format guessing state
|
|
|
|
uint32_t cs10;
|
|
|
|
uint32_t cs11;
|
|
|
|
uint32_t samples;
|
2018-11-04 23:56:00 -04:00
|
|
|
|
2018-04-10 03:14:54 -03:00
|
|
|
// bind state machine
|
|
|
|
enum {
|
|
|
|
BIND_STATE_NONE,
|
|
|
|
BIND_STATE1,
|
|
|
|
BIND_STATE2,
|
|
|
|
BIND_STATE3,
|
|
|
|
BIND_STATE4,
|
|
|
|
} bind_state;
|
|
|
|
uint32_t bind_last_ms;
|
2021-10-10 01:46:03 -03:00
|
|
|
uint32_t bind_mode_saved;
|
2018-07-20 01:25:40 -03:00
|
|
|
|
2018-11-15 02:04:54 -04:00
|
|
|
uint16_t last_values[AP_DSM_MAX_CHANNELS];
|
|
|
|
|
2018-10-31 21:21:26 -03:00
|
|
|
struct {
|
2020-08-16 21:14:37 -03:00
|
|
|
uint8_t buf[16];
|
2018-10-31 21:21:26 -03:00
|
|
|
uint8_t ofs;
|
|
|
|
} byte_input;
|
2018-11-04 23:56:00 -04:00
|
|
|
|
2020-08-16 21:14:37 -03:00
|
|
|
enum DSM_DECODE_STATE {
|
|
|
|
DSM_DECODE_STATE_DESYNC = 0,
|
|
|
|
DSM_DECODE_STATE_SYNC
|
|
|
|
} dsm_decode_state;
|
|
|
|
|
|
|
|
uint32_t last_frame_time_ms;
|
|
|
|
uint32_t last_rx_time_ms;
|
2018-11-26 02:04:01 -04:00
|
|
|
uint16_t chan_count;
|
|
|
|
|
2018-11-04 23:56:00 -04:00
|
|
|
SoftSerial ss{115200, SoftSerial::SERIAL_CONFIG_8N1};
|
2018-04-10 03:14:54 -03:00
|
|
|
};
|