/*
    rc Task

*/

/* Includes ----------------------------------------------------------------- */
#include "cmsis_os2.h"
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "device/dr16.h"
#include "module/config.h"
#include "module/balance_chassis.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;
Chassis_CMD_t cmd_for_chassis;
// 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);
  /* USER CODE INIT END */

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

    } else {
      DR16_Offline(&dr16);
    }
    switch (dr16.data.sw_l) {

    case DR16_SW_UP:
        cmd_for_chassis.mode = CHASSIS_MODE_RELAX;
        break;
    case DR16_SW_MID:
        cmd_for_chassis.mode = CHASSIS_MODE_RECOVER;
        break;
    case DR16_SW_DOWN:
        cmd_for_chassis.mode = CHASSIS_MODE_WHELL_LEG_BALANCE;
        break;
    default:
        cmd_for_chassis.mode = CHASSIS_MODE_RELAX;
        break;
    }

    osMessageQueueReset(task_runtime.msgq.Chassis_cmd); // 重置消息队列，防止阻塞
    osMessageQueuePut(task_runtime.msgq.Chassis_cmd, &cmd_for_chassis, 0, 0); // 非阻塞发送底盘控制命令
    

    // 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;
    // }

    /* USER CODE END */
  }
}