#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "main.h"

#define KEY_ENABLED(bit) ((rc_data()->Kv_get.key_en_table & (1 << (bit - 1))) != 0)

typedef enum
{
  S1_BUTTON = 0,
  S2_BUTTON,
  S3_BUTTON,
  S4_BUTTON,
  S5_BUTTON,
  S6_BUTTON,
  S7_BUTTON,
  S8_BUTTON,
  S9_BUTTON,
  S10_BUTTON,
  S11_BUTTON,
  S12_BUTTON,
  BUTTON_MAX
} user_button_t;

static flex_button_t user_button[BUTTON_MAX];

/**
 *@brief  按键1回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_1_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:

    break;
  case FLEX_BTN_PRESS_CLICK:
    rc_data()->Lock_state = !rc_data()->Lock_state;
    rc_data()->Lock_time_tick = 0;
    LEDClass.Set(BLACK);
    rc_data()->isLEDTog = DISABLE;
    rc_data()->isSend = DISABLE;
    ASR_Power_OFF;
    rc_data()->Asr_state = LOCK;

    break;
  case FLEX_BTN_PRESS_LONG_START:
    BOARD_Power_OFF;

    break;
  }
}

/**
 *@brief  按键2回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_2_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(2))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->Key_data[1] = 1;
      rc_data()->isLEDTog = ENABLE;
    }

    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}

/**
 *@brief  按键3回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_3_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(3))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
      rc_data()->Key_data[0] = 3;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键4回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_4_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(4))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
      rc_data()->Key_data[0] = 4;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}

/**
 *@brief  按键5回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_5_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(5))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
      rc_data()->Key_data[0] = 5;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键6回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_6_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(6))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
      rc_data()->Key_data[0] = 6;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键7回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_7_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(7))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
      rc_data()->Key_data[0] = 7;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键8回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_8_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(8))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
      rc_data()->Key_data[0] = 8;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键9回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_9_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(9))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
      rc_data()->Key_data[2] = 1;
			rc_data()->Key_data[3] = 0;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键10回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_10_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(10))
    {
      rc_data()->Lock_time_tick = 0;
      rc_data()->isLEDTog = ENABLE;
			rc_data()->Key_data[2] = 0;
      rc_data()->Key_data[3] = 1;
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键11回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_11_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    if (KEY_ENABLED(11))
    {
      rc_data()->Lock_time_tick = 0;
      if (rc_data()->Asr_state == UNLOCK && rc_data()->Lock_state == UNLOCK)
      {
        ASR_Power_OFF;
        LEDClass.Set(BLACK);
        rc_data()->Asr_state = LOCK;
      }
      else if (rc_data()->Asr_state == LOCK && rc_data()->Lock_state == UNLOCK)
      {
        ASR_Power_ON;
        LEDClass.Set(BLACK);
        rc_data()->Asr_state = UNLOCK;
      }
    }
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}
/**
 *@brief  按键12回调函数
 *@param  *btn 按键结构体
 *@retval None
 */
static void btn_12_cb(flex_button_t *btn)
{
  switch (btn->event)
  {
  case FLEX_BTN_PRESS_DOWN:
    break;
  case FLEX_BTN_PRESS_CLICK:
    rc_data()->Lock_time_tick = 0;
    rc_data()->isSend = ENABLE;
    break;
  case FLEX_BTN_PRESS_LONG_START:
    break;
  }
}

// 按键001平读取接口
static uint8_t button_s1_read(void)
{
  return GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
}

// 按键02电平读取接口
static uint8_t button_s2_read(void)
{
  return GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11);
}

// 按键03平读取接口
static uint8_t button_s3_read(void)
{
  return GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12);
}

// 按键04电平读取接口
static uint8_t button_s4_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7);
}
// 按键05电平读取接口
static uint8_t button_s5_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
}
// 按键06电平读取接口
static uint8_t button_s6_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6);
}
// 按键07电平读取接口
static uint8_t button_s7_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}
// 按键08电平读取接口
static uint8_t button_s8_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5);
}
// 按键09电平读取接口
static uint8_t button_s9_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15);
}
// 按键10电平读取接口
static uint8_t button_s10_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3);
}
// 按键11电平读取接口
static uint8_t button_s11_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1);
}
// 按键12电平读取接口
static uint8_t button_s12_read(void)
{
  return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12);
}

/**
 *@brief  按键初始化
 *@param  None
 *@retval None
 */
void user_button_init(void)
{
  /*注册按键Callback函数*/
  memset(&user_button[0], 0x0, sizeof(user_button));

  user_button[S1_BUTTON].usr_button_read = button_s1_read;
  user_button[S1_BUTTON].cb = (flex_button_response_callback)btn_1_cb;

  user_button[S2_BUTTON].usr_button_read = button_s2_read;
  user_button[S2_BUTTON].cb = (flex_button_response_callback)btn_2_cb;

  user_button[S3_BUTTON].usr_button_read = button_s3_read;
  user_button[S3_BUTTON].cb = (flex_button_response_callback)btn_3_cb;

  user_button[S4_BUTTON].usr_button_read = button_s4_read;
  user_button[S4_BUTTON].cb = (flex_button_response_callback)btn_4_cb;

  user_button[S5_BUTTON].usr_button_read = button_s5_read;
  user_button[S5_BUTTON].cb = (flex_button_response_callback)btn_5_cb;

  user_button[S6_BUTTON].usr_button_read = button_s6_read;
  user_button[S6_BUTTON].cb = (flex_button_response_callback)btn_6_cb;

  user_button[S7_BUTTON].usr_button_read = button_s7_read;
  user_button[S7_BUTTON].cb = (flex_button_response_callback)btn_7_cb;

  user_button[S8_BUTTON].usr_button_read = button_s8_read;
  user_button[S8_BUTTON].cb = (flex_button_response_callback)btn_8_cb;

  user_button[S9_BUTTON].usr_button_read = button_s9_read;
  user_button[S9_BUTTON].cb = (flex_button_response_callback)btn_9_cb;

  user_button[S10_BUTTON].usr_button_read = button_s10_read;
  user_button[S10_BUTTON].cb = (flex_button_response_callback)btn_10_cb;

  user_button[S11_BUTTON].usr_button_read = button_s11_read;
  user_button[S11_BUTTON].cb = (flex_button_response_callback)btn_11_cb;

  user_button[S12_BUTTON].usr_button_read = button_s12_read;
  user_button[S12_BUTTON].cb = (flex_button_response_callback)btn_12_cb;

  /*按键检测IO初始化*/
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_13 | GPIO_Pin_6 | GPIO_Pin_14 | GPIO_Pin_7 | GPIO_Pin_12 | GPIO_Pin_1 | GPIO_Pin_15 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_12 | GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  user_button[0].pressed_logic_level = 1;
  user_button[0].click_start_tick = 20;
  user_button[0].short_press_start_tick = 80;
  user_button[0].long_press_start_tick = 200;
  user_button[0].long_hold_start_tick = 300;
  flex_button_register(&user_button[0]);

  for (uint8_t i = 1; i < BUTTON_MAX; i++)
  {
    user_button[i].pressed_logic_level = 0;
    user_button[i].click_start_tick = 20;
    user_button[i].short_press_start_tick = 80;
    user_button[i].long_press_start_tick = 200;
    user_button[i].long_hold_start_tick = 300;
    flex_button_register(&user_button[i]);
  }
}
