#include "user_task.h"
#include "cmd.h"
#ifdef DEBUG

CMD_t cmd;
CMD_RC_t rc_ctrl;
CMD_NUC_t Nuc;
CMD_ACTION_t cmd_ops_out;

#else

static CMD_t cmd;
static CMD_RC_t rc_ctrl;
static CMD_NUC_t Nuc;	
static CMD_ACTION_t cmd_ops_out;

#endif 


void Task_cmd(void *argument){
   (void)argument; /*未使用传递参数 消除警告*/

 /* 计算到下一次调度任务所需要的tick数 */
  const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_CTRL_CMD;

    /**/
  CMD_Init(&cmd);
	
  uint32_t tick = osKernelGetTickCount(); /*控制任务运行频率的计时 */
   while(1){
#ifdef DEBUG
    /*记录任务所需要的栈空间*/
    task_runtime.stack_water_mark.cmd =
        osThreadGetStackSpace(osThreadGetId());	 
#endif

    osKernelLock(); /*锁住RTOS内核调度*/
		 
    /*将各任务接收到的原始数据解析为通用的控制命令*/
		//遥控器
		if(osMessageQueueGet(task_runtime.msgq.cmd.raw.rc, &rc_ctrl, 0, 0) == osOK)
      CMD_ParseRc(&cmd, &rc_ctrl);
    //码盘 
    if(osMessageQueueGet(task_runtime.msgq.cmd.raw .ops_9, &cmd_ops_out, 0, 0) == osOK)
		CMD_ParseAction (&cmd ,&cmd_ops_out);
    //nuc
		if(osMessageQueueGet(task_runtime.msgq.cmd.raw.nuc, &Nuc,0 ,0) ==osOK)
      CMD_ParseNuc(&cmd,&Nuc);
		
		CMD_CtrlSet(&cmd,&rc_ctrl);
		
    osKernelUnlock(); /* 同上 解锁RTOS内核 */	
		
    /*将需要与其他任务共享的数据放到消息队列里  */
		//发送给底盘
     osMessageQueueReset(task_runtime.msgq.cmd.chassis);
     osMessageQueuePut(task_runtime.msgq.cmd.chassis,&cmd,0,0);
		//发送给选点
		osMessageQueueReset(task_runtime.msgq.cmd.calc);
     osMessageQueuePut(task_runtime.msgq.cmd.calc,&cmd,0,0);
		//发送给nuc
		osMessageQueueReset(task_runtime.msgq.cmd.nuc);
     osMessageQueuePut(task_runtime.msgq.cmd.nuc,&cmd,0,0);
		
    tick += delay_tick; /*计算下一个唤醒时刻*/
    osDelayUntil(tick); /*绝对延时 等待下一个唤醒时刻 */


}
		
}



