/*
       DR16接收机

*/

/* Includes ----------------------------------------------------------------- */
#include "dr16.h"

#include <string.h>

#include "bsp_usart.h"
#include "error_detect.h"


#ifdef dr16_t
//#define 
/* Private define ----------------------------------------------------------- */
#define DR16_CH_VALUE_MIN (364u)
#define DR16_CH_VALUE_MID (1024u)
#define DR16_CH_VALUE_MAX (1684u)

/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */

static osThreadId_t thread_alert;
uint8_t cbuf[36]; //此处设置为两帧字节的长度 若为一帧会出现乱码的情况
/* Private function  -------------------------------------------------------- */
static void DR16_RxCpltCallback(void) {
  osThreadFlagsSet(thread_alert, SIGNAL_DR16_RAW_REDY);
//	detect_hook(DR16_TOE);
}

static bool DR16_DataCorrupted(const DR16_t *dr16) {
  if (dr16 == NULL) return DEVICE_ERR_NULL;

  if ((dr16->data.ch_r_x < DR16_CH_VALUE_MIN) ||
      (dr16->data.ch_r_x > DR16_CH_VALUE_MAX))
    return true;

  if ((dr16->data.ch_r_y < DR16_CH_VALUE_MIN) ||
      (dr16->data.ch_r_y > DR16_CH_VALUE_MAX))
    return true;

  if ((dr16->data.ch_l_x < DR16_CH_VALUE_MIN) ||
      (dr16->data.ch_l_x > DR16_CH_VALUE_MAX))
    return true;

  if ((dr16->data.ch_l_y < 1000u) ||
      (dr16->data.ch_l_y > DR16_CH_VALUE_MAX))
    return true;

  if (dr16->data.sw_l == 0) return true;

  if (dr16->data.sw_r == 0) return true;

  return false;
}
/* Exported functions ------------------------------------------------------- */
int8_t DR16_Init(DR16_t *dr16) {
  if (dr16 == NULL) return DEVICE_ERR_NULL;
  if ((thread_alert = osThreadGetId()) == NULL) return DEVICE_ERR_NULL;
  
  BSP_UART_RegisterCallback(BSP_UART_REMOTE, BSP_UART_IDLE_LINE_CB,
                            DR16_RxCpltCallback);
  return DEVICE_OK;
}

int8_t DR16_Restart(void) {
  __HAL_UART_DISABLE(BSP_UART_GetHandle(BSP_UART_REMOTE));
  __HAL_UART_ENABLE(BSP_UART_GetHandle(BSP_UART_REMOTE));
  return DEVICE_OK;
}

int8_t DR16_StartDmaRecv(DR16_t *dr16) {
  if (HAL_UARTEx_ReceiveToIdle_DMA(BSP_UART_GetHandle(BSP_UART_REMOTE),
                           (uint8_t *)cbuf,
                           sizeof(cbuf)) == HAL_OK)
    return DEVICE_OK;
  return DEVICE_ERR;
}

bool DR16_WaitDmaCplt(uint32_t timeout) {
  return (osThreadFlagsWait(SIGNAL_DR16_RAW_REDY, osFlagsWaitAll, timeout) ==
          SIGNAL_DR16_RAW_REDY);
}

int8_t DR16_ParseRaw(DR16_t *dr16)
{
	if(dr16 ==NULL) return DEVICE_ERR_NULL;
	 dr16->data.ch_r_x = (cbuf[0] | (cbuf[1] <<8));
	 dr16->data.ch_r_y = ((cbuf[1] >> 3)| (cbuf[2] << 5));
	 dr16->data.ch_l_x = ((cbuf[2] >>6) | (cbuf[3] << 2) | (cbuf[4] <<10));
	 dr16->data.ch_l_y = ((cbuf[4] >>1) | (cbuf[5] <<7));
	 dr16->data.sw_r = ((cbuf[5] >>4));
   dr16->data.sw_l = ((cbuf[5] >> 4) & 0x000C) >> 2;             
	
	return 1;
}

int8_t DR16_ParseRC(const DR16_t *dr16, CMD_RC_t *rc) {
  if (dr16 == NULL) return DEVICE_ERR_NULL;
 
  if (DR16_DataCorrupted(dr16)) {
		return DEVICE_ERR;
  } else {
    memset(rc, 0, sizeof(*rc));
  }
  float full_range = (float)(DR16_CH_VALUE_MAX - DR16_CH_VALUE_MIN);

  rc->ch_r_x = 2 * ((float)dr16->data.ch_r_x - DR16_CH_VALUE_MID) / full_range;
  rc->ch_r_y = 2 * ((float)dr16->data.ch_r_y - DR16_CH_VALUE_MID) / full_range;
  rc->ch_l_x = 2 * ((float)dr16->data.ch_l_x - DR16_CH_VALUE_MID) / full_range;
  rc->ch_l_y = 2 * ((float)dr16->data.ch_l_y - DR16_CH_VALUE_MID) / full_range;

  rc->sw_l = (CMD_SwitchPos_t)dr16->data.sw_l;
  rc->sw_r = (CMD_SwitchPos_t)dr16->data.sw_r;

  rc->key = dr16->data.key;

//  rc->ch_res = ((float)dr16->data.res - DR16_CH_VALUE_MID) / full_range;
  return DEVICE_OK;
}

int8_t DR16_HandleOffline(const DR16_t *dr16, CMD_RC_t *rc) {
  if (dr16 == NULL) return DEVICE_ERR_NULL;
  if (rc == NULL) return DEVICE_ERR_NULL;

  (void)dr16;
  memset(rc, 0, sizeof(*rc));
  return 0;
}


#endif