/*
  DR16接收机通信任务
*/

/* Includes ----------------------------------------------------------------- */
#include <string.h>

#include "rc.h"
#include "user_task.h"

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
#ifdef DEBUG
DR16_t dr16;
CMD_RC_t cmd_rc;
LD_raw_t LD_raw;
LD_Data_t  LD;
#else
static DR16_t dr16;
static CMD_RC_t cmd_rc;
static LD_raw_t LD_raw;
static LD_Data_t  LD;


#endif

/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */

/**
 * \brief dr16接收机
 *
 * \param argument 未使用
 */
void Task_rc(void *argument) {
  (void)argument; /* 未使用，消除警告 */
	const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_RC;

  uint32_t tick = osKernelGetTickCount();
  RC_SBUS_Init(); /* 初始化 */

  while (1) {
#ifdef DEBUG
    /*  */
    task_runtime.stack_water_mark.rc = osThreadGetStackSpace(osThreadGetId());
#endif
    /* 开启DMA */
    RC_StartDmaRecv();
    if (RC_WaitDmaCplt(30)) {
      			
      RC_ParseRC(&dr16,&LD_raw,&LD, &cmd_rc);
    } else {
      /* 处理遥控器离线*/
			
			

     DR16_HandleOffline(&dr16, &cmd_rc);
     LD_HandleOffline(&LD,&cmd_rc);
     
			
			
    }
    osMessageQueueReset(task_runtime.msgq.cmd.raw.rc);
    osMessageQueuePut(task_runtime.msgq.cmd.raw.rc, &cmd_rc, 0, 0);
			tick += delay_tick;
        osDelayUntil(tick);
		 
  }
}
