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

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

#include "dr16.h"
#include "user_task.h"

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */

#ifdef dr16_t

#ifdef DEBUG
DR16_t dr16;
CMD_RC_t cmd_rc;
#else
static DR16_t dr16;
static CMD_RC_t cmd_rc;
#endif

#endif

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

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

#ifdef dr16_t

  DR16_Init(&dr16); /* 初始化dr16 */
#endif

  while (1) {
#ifdef dr16_t
		
#ifdef DEBUG
    /*  */
    task_runtime.stack_water_mark.dr16 = osThreadGetStackSpace(osThreadGetId());
#endif
		
    /* 开启DMA */
    DR16_StartDmaRecv(&dr16);

    if (DR16_WaitDmaCplt(30)) {
      /* 转换 */
			DR16_ParseRaw(&dr16);
      DR16_ParseRC(&dr16, &cmd_rc);
    } else {
      /* 处理遥控器离线 */
      DR16_HandleOffline(&dr16, &cmd_rc);
    }
    osMessageQueueReset(task_runtime.msgq.cmd.raw.rc);
    osMessageQueuePut(task_runtime.msgq.cmd.raw.rc, &cmd_rc, 0, 0);
  }
#endif	

		 	tick += delay_tick; /* 计算下一个唤醒时*/
		
		 osDelay(10);

}
