#include "Remote.h"
#include "main.h"
#include "BSP_USART.h"


//===============变量区
extern UART_HandleTypeDef huart3;					//usart.c
extern DMA_HandleTypeDef hdma_usart3_rx;	//usart.c

const RC_ctrl_t *local_rc_ctrl;	
static uint8_t sbus_rx_buf[2][SBUS_RX_BUF_NUM];	//双缓冲区
RC_ctrl_t rc_ctrl;


//===============内部函数
const RC_ctrl_t *remote_GetControlPoint(void)
{
    return &rc_ctrl;
}

static void remote_SbusToRC(volatile const uint8_t *sbus_buf, RC_ctrl_t *rc_ctrl)
{
    if (sbus_buf == NULL || rc_ctrl == NULL)
    {
        return;
    }

    rc_ctrl->rc.ch[0] = (sbus_buf[0] | (sbus_buf[1] << 8)) & 0x07ff;        //!< Channel 0
    rc_ctrl->rc.ch[1] = ((sbus_buf[1] >> 3) | (sbus_buf[2] << 5)) & 0x07ff; //!< Channel 1
    rc_ctrl->rc.ch[2] = ((sbus_buf[2] >> 6) | (sbus_buf[3] << 2) |          //!< Channel 2
                         (sbus_buf[4] << 10)) &0x07ff;
    rc_ctrl->rc.ch[3] = ((sbus_buf[4] >> 1) | (sbus_buf[5] << 7)) & 0x07ff; //!< Channel 3
    rc_ctrl->rc.s[0] = ((sbus_buf[5] >> 4) & 0x0003);                  //!< Switch left
    rc_ctrl->rc.s[1] = ((sbus_buf[5] >> 4) & 0x000C) >> 2;                       //!< Switch right
    rc_ctrl->mouse.x = sbus_buf[6] | (sbus_buf[7] << 8);                    //!< Mouse X axis
    rc_ctrl->mouse.y = sbus_buf[8] | (sbus_buf[9] << 8);                    //!< Mouse Y axis
    rc_ctrl->mouse.z = sbus_buf[10] | (sbus_buf[11] << 8);                  //!< Mouse Z axis
    rc_ctrl->mouse.press_l = sbus_buf[12];                                  //!< Mouse Left Is Press ?
    rc_ctrl->mouse.press_r = sbus_buf[13];                                  //!< Mouse Right Is Press ?
    rc_ctrl->key.v = sbus_buf[14] | (sbus_buf[15] << 8);                    //!< KeyBoard value
    rc_ctrl->rc.ch[4] = sbus_buf[16] | (sbus_buf[17] << 8);                 //NULL

    rc_ctrl->rc.ch[0] -= RC_CH_VALUE_OFFSET;
    rc_ctrl->rc.ch[1] -= RC_CH_VALUE_OFFSET;
    rc_ctrl->rc.ch[2] -= RC_CH_VALUE_OFFSET;
    rc_ctrl->rc.ch[3] -= RC_CH_VALUE_OFFSET;
    rc_ctrl->rc.ch[4] -= RC_CH_VALUE_OFFSET;
}


//===============公共函数
void Remote_Init(void)//遥控器初始化
{
  local_rc_ctrl = remote_GetControlPoint();
  DBUS_DMA_Init(sbus_rx_buf[0], sbus_rx_buf[1], SBUS_RX_BUF_NUM);
}

void Remote_UART_IDLE_Callback(void)
{
    static uint16_t this_time_rx_len = 0;

    // 清除 IDLE 标志（注意：原代码误清 PEFLAG，应清 IDLE）
    __HAL_UART_CLEAR_IDLEFLAG(&huart3);

    if ((hdma_usart3_rx.Instance->CR & DMA_SxCR_CT) == RESET)
    {
        /* 当前使用的是 Memory 0 */
        __HAL_DMA_DISABLE(&hdma_usart3_rx);

        this_time_rx_len = SBUS_RX_BUF_NUM - hdma_usart3_rx.Instance->NDTR;
        hdma_usart3_rx.Instance->NDTR = SBUS_RX_BUF_NUM;
        hdma_usart3_rx.Instance->CR |= DMA_SxCR_CT;  // 切换到 Memory 1
        __HAL_DMA_ENABLE(&hdma_usart3_rx);

        if (this_time_rx_len == RC_FRAME_LENGTH)
        {
            remote_SbusToRC(sbus_rx_buf[0], &rc_ctrl);
        }
    }
    else
    {
        /* 当前使用的是 Memory 1 */
        __HAL_DMA_DISABLE(&hdma_usart3_rx);

        this_time_rx_len = SBUS_RX_BUF_NUM - hdma_usart3_rx.Instance->NDTR;
        hdma_usart3_rx.Instance->NDTR = SBUS_RX_BUF_NUM;
        hdma_usart3_rx.Instance->CR &= ~DMA_SxCR_CT; // 切换回 Memory 0
        __HAL_DMA_ENABLE(&hdma_usart3_rx);

        if (this_time_rx_len == RC_FRAME_LENGTH)
        {
            remote_SbusToRC(sbus_rx_buf[1], &rc_ctrl);
        }
    }
}
