/*
    Task2 Task
    遥控器任务，主要是大疆dr16和乐迪遥控器
*/

/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "device/dr16.h"
#include "device/ledi.h"

/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
 #ifdef DEBUG
DR16_t dr16;
// CMD_RC_t cmd_rc;
 LD_t LD;
 #else
 static DR16_t dr16;
 static CMD_RC_t cmd_rc;
 static LD_t LD;
 #endif


/* USER STRUCT END */

/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_Task2(void *argument) {
  (void)argument; /* 未使用argument，消除警告 */

  
  /* 计算任务运行到指定频率需要等待的tick数 */
  const uint32_t delay_tick = osKernelGetTickFreq() / TASK2_FREQ;

  osDelay(TASK2_INIT_DELAY); /* 延时一段时间再开启任务 */

  uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
  /* USER CODE INIT BEGIN */
        DR16_Init(&dr16);

  /* USER CODE INIT END */
  
  while (1) {
    tick += delay_tick; /* 计算下一个唤醒时刻 */
    /* USER CODE BEGIN */
      /*开启dma接收遥控器数据*/
    DR16_StartDmaRecv(&dr16);
    /*上传DR16数据到消息队列*/
    if(DR16_WaitDmaCplt(30)){
      DR16_ParseData(&dr16);
    } else {
      DR16_Offline(&dr16);
    }
    
      osMessageQueueReset(task_runtime.msgq.dr16_data);
      osMessageQueuePut(task_runtime.msgq.dr16_data, &dr16, 0, 0);

    /* USER CODE END */
    osDelayUntil(tick); /* 运行结束，等待下一次唤醒 */
  }
  
}