#include "bsp_relay.h"

/*开关控制电平，使用同一的电平控制方式*/
#define RELAY_GPIO_ON   GPIO_PIN_SET
#define RELAY_GPIO_OFF  GPIO_PIN_RESET

#define RELAY_1_ON          HAL_GPIO_WritePin (RELAY_1_GPIO_Port,    RELAY_1_Pin,    RELAY_GPIO_ON)
#define RELAY_1_OFF         HAL_GPIO_WritePin (RELAY_1_GPIO_Port,    RELAY_1_Pin,    RELAY_GPIO_OFF)

#define RELAY_2_ON          HAL_GPIO_WritePin (RELAY_2_GPIO_Port,    RELAY_2_Pin,    RELAY_GPIO_ON)
#define RELAY_2_OFF         HAL_GPIO_WritePin (RELAY_2_GPIO_Port,    RELAY_2_Pin,    RELAY_GPIO_OFF)

#define RELAY_3_ON          HAL_GPIO_WritePin (RELAY_3_GPIO_Port,    RELAY_3_Pin,    RELAY_GPIO_ON)
#define RELAY_3_OFF         HAL_GPIO_WritePin (RELAY_3_GPIO_Port,    RELAY_3_Pin,    RELAY_GPIO_OFF)

#define RELAY_4_ON          HAL_GPIO_WritePin (RELAY_4_GPIO_Port,    RELAY_4_Pin,    RELAY_GPIO_ON)
#define RELAY_4_OFF         HAL_GPIO_WritePin (RELAY_4_GPIO_Port,    RELAY_4_Pin,    RELAY_GPIO_OFF)

static void bsp_relay_init(void);
static void bsp_relay_task(void);
static void bsp_relay_set(u8 ch,u8 state);

bsp_relay_t relay =
{
    .init = bsp_relay_init,
    .task = bsp_relay_task,
    .set  = bsp_relay_set,
};

bsp_relay_t *p_relay = &relay;
/*其他外设初始化后快速闪烁，提示初始化完成*/
static void bsp_relay_init(void)
{
    RELAY_1_OFF;
    RELAY_2_OFF;
    RELAY_3_OFF;
    RELAY_4_OFF;
}

static void bsp_relay_task(void)
{
    (USR_ON == p_relay->state[0]) ?  RELAY_1_ON : RELAY_1_OFF;
    (USR_ON == p_relay->state[1]) ?  RELAY_2_ON : RELAY_2_OFF;
    (USR_ON == p_relay->state[2]) ?  RELAY_3_ON : RELAY_3_OFF;
    (USR_ON == p_relay->state[3]) ?  RELAY_4_ON : RELAY_4_OFF;
}
/*控制对应通道状态*/
static void bsp_relay_set(u8 ch,u8 state)
{
    if(ch < BSP_RELAY_CH_NUM)
    {
        p_relay->state[ch] = state;
    }
}
