#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ----------------------------------------------------------------- */
#include <cmsis_os2.h>

#include "component/user_math.h"
#include "device.h"

/* Exported constants ------------------------------------------------------- */
#define AT9S_FRAME_LEN 25u

/* Exported types ----------------------------------------------------------- */
typedef struct __attribute__((packed))
{
    int16_t ch[4];   /* 摇杆原始值 */
    int16_t sw[8];   /* 开关/旋钮原始值 */
} AT9S_Raw_t;

/* 拨杆位置 */
typedef enum {
  AT9S_CMD_SW_ERR = 0,
  AT9S_CMD_SW_UP = 1,
  AT9S_CMD_SW_MID = 3,
  AT9S_CMD_SW_DOWN = 2,
} AT9S_CMD_SwitchPos_t;
typedef  struct
{
    float ch_l_x;   /* 左摇杆 X */
    float ch_l_y;   /* 左摇杆 Y（油门） */
    float ch_r_x;   /* 右摇杆 X */
    float ch_r_y;   /* 右摇杆 Y */

    /* 开关/旋钮离散化后状态 */
    AT9S_CMD_SwitchPos_t key_A;
	AT9S_CMD_SwitchPos_t key_B;
    AT9S_CMD_SwitchPos_t key_C;
	AT9S_CMD_SwitchPos_t key_D;
    AT9S_CMD_SwitchPos_t key_E;
    AT9S_CMD_SwitchPos_t key_F;
    AT9S_CMD_SwitchPos_t key_G;
    AT9S_CMD_SwitchPos_t key_H;
    float knob_left;
    float knob_right;
	float back_left;
	float back_right;
} AT9S_DataRC_t;
typedef  struct
{
    AT9S_DataRC_t rc;
} AT9S_Data_t;

typedef struct{	
    DEVICE_Header_t header;
    AT9S_Data_t data;
} AT9S_t;

/* Exported functions prototypes -------------------------------------------- */
int8_t  AT9S_Init(AT9S_t *at9s);
int8_t  AT9S_Restart(void);
int8_t  AT9S_StartDmaRecv(uint8_t *cmd_buffer);
bool    AT9S_WaitDmaCplt(uint32_t timeout);
void    AT9S_ParseRaw(const uint8_t raw[AT9S_FRAME_LEN], AT9S_t *out);

#ifdef __cplusplus
}
#endif