/*
  遥控器通信任务
*/

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

#include "remote_control.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;
#else
static DR16_t dr16;
static CMD_RC_t cmd_rc;
static LD_raw_t LD;
#endif

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

void Task_remote(void *argument) {
  (void)argument; /* 未使用，消除警告 */

	 const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_REMOTE;
	 uint32_t tick = osKernelGetTickCount(); /* 获取当前控制任务运行频率的tick*/
	
  REMOTE_Init(); /* 初始化遥控器 */

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

    if (REMOTE_WaitDmaCplt(30)) {
      REMOTE_ParseRC(&dr16, &cmd_rc,&LD);
    } 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);
  }
}
