/* Includes ----------------------------------------------------------------- */
#include "bsp\gpio.h"

#include <gpio.h>
#include <main.h>

/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
static void (*GPIO_Callback[16])(void);

/* Private function  -------------------------------------------------------- */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
  for (uint8_t i = 0; i < 16; i++) {
    if (GPIO_Pin & (1 << i)) {
      if (GPIO_Callback[i]) {
        GPIO_Callback[i]();
      }
    }
  }
}

/* Exported functions ------------------------------------------------------- */

/**
 * @brief 注册 GPIO 中断回调函数
 * @param pin 
 * @param callback 
 * @return 
 */
int8_t BSP_GPIO_RegisterCallback(uint16_t pin, void (*callback)(void)) {
  if (callback == NULL) return BSP_ERR_NULL;

  for (uint8_t i = 0; i < 16; i++) {
    if (pin & (1 << i)) {
      GPIO_Callback[i] = callback;
      break;
    }
  }
  return BSP_OK;
}

int8_t BSP_GPIO_EnableIRQ(uint16_t pin) {
  switch (pin) {
    case key1_Pin:
      HAL_NVIC_EnableIRQ(key1_EXTI_IRQn);
      break;

    case key2_Pin:
      HAL_NVIC_EnableIRQ(key2_EXTI_IRQn);
      break;

      /*
      case XXX_Pin:
              HAL_NVIC_EnableIRQ(XXX_IRQn);
              break;
      */

    default:
      return BSP_ERR;
  }
  return BSP_OK;
}

int8_t BSP_GPIO_DisableIRQ(uint16_t pin) {
  switch (pin) {
    case key1_Pin:
        HAL_NVIC_DisableIRQ(key1_EXTI_IRQn);
        break;
    case key2_Pin:
        HAL_NVIC_DisableIRQ(key2_EXTI_IRQn);
        break;
      /*
      case XXX_Pin:
              HAL_NVIC_EnableIRQ(XXX_IRQn);
              break;
      */

    default:
      return BSP_ERR;
  }
  return BSP_OK;
}
