/*
    rc Task

*/

/* Includes ----------------------------------------------------------------- */
#include "cmsis_os2.h"
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "device/dr16.h"
#include "device/rc_can.h"
#include "module/config.h"
#include "module/shoot.h"
#include "module/gimbal.h"
#include <stdbool.h>
// #include
/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
DR16_t dr16;
RC_CAN_t rc_can;
Shoot_CMD_t for_shoot;
Gimbal_CMD_t cmd_for_gimbal;
/* USER STRUCT END */

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

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

  /* USER CODE INIT BEGIN */
  DR16_Init(&dr16);
  RC_CAN_Init(&rc_can, &Config_GetRobotParam()->rc_can_param);
  /* USER CODE INIT END */

  while (1) {
    /* USER CODE BEGIN */
    DR16_StartDmaRecv(&dr16);
    if (DR16_WaitDmaCplt(20)) {
      DR16_ParseData(&dr16);

    } else {
      DR16_Offline(&dr16);
    }
    for_shoot.online = dr16.header.online;
    switch (dr16.data.sw_r) {
    case DR16_SW_UP:
      for_shoot.ready = false;
      for_shoot.firecmd = false;
      break;
    case DR16_SW_MID:
      for_shoot.ready = true;
      for_shoot.firecmd = false;
      break;
    case DR16_SW_DOWN:
      for_shoot.ready = true;
      for_shoot.firecmd = true;
      break;
    default:
      for_shoot.ready = false;
      for_shoot.firecmd = false;
      break;
    }
    rc_can.data.joy.ch_l_x = dr16.data.ch_l_x;
    rc_can.data.joy.ch_l_y = dr16.data.ch_l_y;
    rc_can.data.joy.ch_r_x = dr16.data.ch_r_x;
    rc_can.data.joy.ch_r_y = dr16.data.ch_r_y;
    rc_can.data.sw.sw_l = (RC_CAN_SW_t)dr16.data.sw_l;    
    rc_can.data.sw.sw_r = (RC_CAN_SW_t)dr16.data.sw_r;
    rc_can.data.sw.ch_res = dr16.data.ch_res;

    RC_CAN_SendData(&rc_can,  RC_CAN_DATA_JOYSTICK);
    RC_CAN_SendData(&rc_can,  RC_CAN_DATA_SWITCH);

    switch (dr16.data.sw_l) {
    case DR16_SW_UP:
      cmd_for_gimbal.mode = GIMBAL_MODE_RELAX;
      cmd_for_gimbal.delta_yaw = 0.0f;
      cmd_for_gimbal.delta_pit = 0.0f;
      break;
    case DR16_SW_MID:
      cmd_for_gimbal.mode = GIMBAL_MODE_ABSOLUTE;
      cmd_for_gimbal.delta_yaw = -dr16.data.ch_r_x*5.0f;
      cmd_for_gimbal.delta_pit = dr16.data.ch_r_y*5.0f;
      break;
    case DR16_SW_DOWN:
      cmd_for_gimbal.mode = GIMBAL_MODE_ABSOLUTE;
      cmd_for_gimbal.delta_yaw = -dr16.data.ch_r_x* 5.0f;
      cmd_for_gimbal.delta_pit = dr16.data.ch_r_y*5.0f;
      break;
    default:
      cmd_for_gimbal.mode = GIMBAL_MODE_RELAX;
      cmd_for_gimbal.delta_yaw = 0.0f;
      cmd_for_gimbal.delta_pit = 0.0f;
      break;
    }
    osMessageQueueReset(task_runtime.msgq.gimbal.cmd);
    osMessageQueuePut(task_runtime.msgq.gimbal.cmd, &cmd_for_gimbal, 0, 0);

    osMessageQueueReset(task_runtime.msgq.shoot.shoot_cmd);
    osMessageQueuePut(task_runtime.msgq.shoot.shoot_cmd, &for_shoot, 0, 0);
    /* USER CODE END */
  }
}