#include "r12ds.h"
#include "cmsis_os.h"
#include "user_task.h"
#include <string.h>


#ifdef r12ds_t

#ifdef DEBUG

CMD_RC_t cmd_rc;

uint8_t sbus_rx_buf[2][RC_FRAME_LENGTH];

#else 

static CMD_RC_t cmd_rc;

#endif



CMD_RC_t *R12ds_DataGet(){
	return &cmd_rc; 
}


void R12ds_HandleOffline(void) {
	 CMD_RC_t *rc;
	 rc = R12ds_DataGet();
    memset(rc, 0, sizeof(rc->ch_x) + sizeof(rc->mul) + sizeof(rc->ch_y) + sizeof(rc->ch_w) + sizeof(rc->key));
}

#endif
	
void Task_r12ds(void *argument)
{
   (void)argument;
	

  const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_R12DS;
#ifdef r12ds_t
	
    R12ds_DMA_Init(sbus_rx_buf[0],sbus_rx_buf[1],50);
#endif
	
      uint32_t tick = osKernelGetTickCount();


   while (1)
   {
		 
#ifdef r12ds_t

#ifdef DEBUG
		 
    task_runtime.stack_water_mark.r12ds =
        osThreadGetStackSpace(osThreadGetId());
		 
#endif
		 
		 sbus_to_rc(sbus_rx_buf[0],&cmd_rc);
				 
		 if(cmd_rc.offline ==1)
		 {
			 R12ds_HandleOffline();
		 }
		 
      osMessageQueueReset(task_runtime.msgq.cmd.raw.rc);
	    osMessageQueuePut(task_runtime.msgq.cmd.raw.rc,(&cmd_rc),0,0);
#endif
	 
	 	tick += delay_tick; /* 计算下一个唤醒时*/
		
		 osDelay(10);

   }



    
 }
