/*
    cmd Task
    
*/

/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "module/gimbal.h"
#include "module/shoot_control.h"
#include "device/lightbridge2.h"
#include "module/config.h"
/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
DEVICE_LIGHTBRIDGE2_t lightbridge2_from_rc;
Shoot_CMD_t cmd_for_shoot;
Gimbal_CMD_t cmd_for_gimbal;
/* USER STRUCT END */

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

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

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

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

  /* USER CODE INIT END */
  
  while (1) {
    tick += delay_tick; /* 计算下一个唤醒时刻 */
    /* USER CODE BEGIN */
		osMessageQueueGet(task_runtime.msgq.cmd.rc, &lightbridge2_from_rc, NULL, 0);
	 	  
	switch (lightbridge2_from_rc.data.key_G) {
    case LIGHTBRIDGE2_CMD_SW_DOWN:
      cmd_for_gimbal.mode = GIMBAL_MODE_RELAX;
      cmd_for_gimbal.delta_yaw = 0.0f;
      cmd_for_gimbal.delta_pit = 0.0f;
      break;
    case LIGHTBRIDGE2_CMD_SW_MID:
      cmd_for_gimbal.mode = GIMBAL_MODE_ABSOLUTE;
      cmd_for_gimbal.delta_yaw = -lightbridge2_from_rc.data.ch_l_x * 2.0f;
      cmd_for_gimbal.delta_pit = -lightbridge2_from_rc.data.ch_l_y * 1.5f;
      break;
    case LIGHTBRIDGE2_CMD_SW_UP:
      cmd_for_gimbal.mode = GIMBAL_MODE_ABSOLUTE;
      cmd_for_gimbal.delta_yaw = -lightbridge2_from_rc.data.ch_l_x * 2.0f;
      cmd_for_gimbal.delta_pit = -lightbridge2_from_rc.data.ch_l_y * 1.5f;
      break;
    default:
      cmd_for_gimbal.mode = GIMBAL_MODE_RELAX;
      cmd_for_gimbal.delta_yaw = 0.0f;
      cmd_for_gimbal.delta_pit = 0.0f;
      break;
    }

	switch (lightbridge2_from_rc.data.key_C) {
    case LIGHTBRIDGE2_CMD_SW_DOWN:
		  cmd_for_shoot.online = lightbridge2_from_rc.online;
      cmd_for_shoot.ready = true;
      cmd_for_shoot.firecmd = true;
      break;
    case LIGHTBRIDGE2_CMD_SW_MID:
		  cmd_for_shoot.online = lightbridge2_from_rc.online;
      cmd_for_shoot.ready = true;
      cmd_for_shoot.firecmd = false;
      break;
    case LIGHTBRIDGE2_CMD_SW_UP:
		  cmd_for_shoot.online = lightbridge2_from_rc.online;
      cmd_for_shoot.ready = false;
      cmd_for_shoot.firecmd = false;
      break;
    default:
		  cmd_for_shoot.online = lightbridge2_from_rc.online;
      cmd_for_shoot.ready = false;
      cmd_for_shoot.firecmd = false;
      break;
    }
		
    osMessageQueuePut(task_runtime.msgq.gimbal.cmd, &cmd_for_gimbal, 0, 0);
    osMessageQueuePut(task_runtime.msgq.shoot.cmd, &cmd_for_shoot, 0, 0);
    /* USER CODE END */
    osDelayUntil(tick); /* 运行结束，等待下一次唤醒 */
  }
  
}