LCOV - code coverage report
Current view: top level - AP_VideoTX - AP_Tramp.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 1 3 33.3 %
Date: 2026-09-09 11:02:08 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :    This program is free software: you can redistribute it and/or modify
       3             :    it under the terms of the GNU General Public License as published by
       4             :    the Free Software Foundation, either version 3 of the License, or
       5             :    (at your option) any later version.
       6             : 
       7             :    This program is distributed in the hope that it will be useful,
       8             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
       9             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      10             :    GNU General Public License for more details.
      11             : 
      12             :    You should have received a copy of the GNU General Public License
      13             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      14             : 
      15             :    Code by Andy Piper, ported from betaflight vtx_tramp
      16             : */
      17             : 
      18             : #pragma once
      19             : 
      20             : #include "AP_VideoTX_config.h"
      21             : 
      22             : #if AP_TRAMP_ENABLED
      23             : 
      24             : #include <AP_HAL/AP_HAL.h>
      25             : #include <AP_OSD/AP_OSD.h>
      26             : #include <AP_Param/AP_Param.h>
      27             : #include <AP_HAL/utility/RingBuffer.h>
      28             : #include "AP_VideoTX.h"
      29             : 
      30             : #define VTX_TRAMP_POWER_COUNT 5
      31             : 
      32             : #define VTX_TRAMP_MIN_FREQUENCY_MHZ 1000             //min freq in MHz
      33             : #define VTX_TRAMP_MAX_FREQUENCY_MHZ 5999             //max freq in MHz
      34             : // Maximum number of requests sent to try a config change
      35             : // Some VTX fail to respond to every request (like Matek FCHUB-VTX) so
      36             : // we sometimes need multiple retries to get the VTX to respond.
      37             : #define VTX_TRAMP_MAX_RETRIES (20)
      38             : // Race lock - settings can't be changed
      39             : #define TRAMP_CONTROL_RACE_LOCK (0x01)
      40             : 
      41             : #define VTX_TRAMP_UART_BAUD            9600
      42             : #define VTX_TRAMP_SMARTBAUD_MIN        9120     // -5%
      43             : #define VTX_TRAMP_SMARTBAUD_MAX        10080    // +5%
      44             : #define VTX_TRAMP_SMARTBAUD_STEP       120
      45             : 
      46             : class AP_Tramp
      47             : {
      48             : public:
      49             :     AP_Tramp();
      50         458 :     ~AP_Tramp() {}
      51             : 
      52             :     /* Do not allow copies */
      53             :     CLASS_NO_COPY(AP_Tramp);
      54             : 
      55             :     static AP_Tramp *get_singleton(void) {
      56             :         return singleton;
      57             :     }
      58             : 
      59             :     bool init(void);
      60             :     void update();
      61             : 
      62             :     // actual transmitted power reported by the VTX in the 'v' response
      63             :     uint16_t get_current_actual_power() const { return cur_act_power; }
      64             :     int16_t get_current_temp() const { return cur_temp; }
      65             : 
      66             : private:
      67             :     uint8_t checksum(uint8_t *buf);
      68             :     // Check if race lock is enabled
      69           0 :     bool is_race_lock_enabled(void) {
      70           0 :         return cur_control_mode & TRAMP_CONTROL_RACE_LOCK;
      71             :     }
      72             :     void send_command(uint8_t cmd, uint16_t param);
      73             :     char handle_response();
      74             :     void reset_receiver();
      75             :     char receive_response();
      76             :     void send_query(uint8_t cmd);
      77             :     void process_requests();
      78             :     bool is_device_ready();
      79             :     void set_frequency(uint16_t freq);
      80             :     void set_power(uint16_t power);
      81             :     void set_pit_mode(uint8_t onoff);
      82             :     // change baud automatically when request-response fails many times
      83             :     void update_baud_rate();
      84             : 
      85             :     // serial interface
      86             :     AP_HAL::UARTDriver *port;                  // UART used to send data to Tramp VTX
      87             : 
      88             :     //Pointer to singleton
      89             :     static AP_Tramp* singleton;
      90             : 
      91             :     const static uint16_t TRAMP_BUF_SIZE = 16;
      92             : 
      93             :     // Serial transmit and receive buffers
      94             :     uint8_t request_buffer[TRAMP_BUF_SIZE];
      95             :     uint8_t response_buffer[TRAMP_BUF_SIZE];
      96             : 
      97             :     // Module state machine
      98             :     enum class TrampStatus {
      99             :         // Offline - device hasn't responded yet
     100             :         TRAMP_STATUS_OFFLINE = 0,
     101             :         // Init - fetching current settings from device
     102             :         TRAMP_STATUS_INIT,
     103             :         // Online - device is ready and being monitored - freq/power/pitmode
     104             :         TRAMP_STATUS_ONLINE_MONITOR_FREQPWRPIT,
     105             :         // Online - device is ready and being monitored - temperature
     106             :         TRAMP_STATUS_ONLINE_MONITOR_TEMP,
     107             :         // Online - device is ready and config has just been updated
     108             :         TRAMP_STATUS_ONLINE_CONFIG
     109             :     };
     110             : 
     111             :     TrampStatus status = TrampStatus::TRAMP_STATUS_OFFLINE;
     112             : 
     113             :     void set_status(TrampStatus stat);
     114             : 
     115             :     // Device limits, read from device during init
     116             :     struct {
     117             :         uint32_t rf_freq_min;
     118             :         uint32_t rf_freq_max;
     119             :         uint32_t rf_power_max;
     120             :     } device_limits;
     121             : 
     122             :     uint16_t cur_act_power; // Actual power
     123             :     int16_t cur_temp;
     124             :     uint8_t cur_control_mode;
     125             :     bool _act_power_warned;
     126             : 
     127             :     // statistics
     128             :     uint16_t _packets_sent;
     129             :     uint16_t _packets_rcvd;
     130             : 
     131             :     // value for current baud adjust
     132             :     int32_t _smartbaud = VTX_TRAMP_UART_BAUD;
     133             :     enum class AutobaudDirection {
     134             :         UP = 1,
     135             :         DOWN = -1
     136             :     } _smartbaud_direction = AutobaudDirection::DOWN;
     137             : 
     138             :     // Retry count
     139             :     uint8_t retry_count = VTX_TRAMP_MAX_RETRIES;
     140             : 
     141             :     // retry counter only re-arms when one of these changes
     142             :     uint16_t _last_conf_freq;
     143             :     uint16_t _last_conf_power;
     144             :     uint16_t _last_conf_options;
     145             :     bool _power_warn_pending;
     146             : 
     147             :     // Receive state machine
     148             :     enum class ReceiveState {
     149             :         S_WAIT_LEN = 0,   // Waiting for a packet len
     150             :         S_WAIT_CODE,      // Waiting for a response code
     151             :         S_DATA,           // Waiting for rest of the packet.
     152             :     };
     153             : 
     154             :     ReceiveState receive_state = ReceiveState::S_WAIT_LEN;
     155             : 
     156             :     // Receive buffer index
     157             :     int16_t receive_pos;
     158             : 
     159             :     // Last action time
     160             :     uint32_t last_time_us;
     161             : };
     162             : 
     163             : #endif

Generated by: LCOV version 1.14