#include "bsp_74HC4067.h"

/*两片74HC4067，一片TX  一片RX  每片16通道*/

#define BSP_74HC4067_CH_MAX 16

/*TX*/
#define TX_EN_ENABLE      HAL_GPIO_WritePin (TX_EN_GPIO_Port,    TX_EN_Pin,    GPIO_PIN_RESET)
#define TX_EN_DISENABLE   HAL_GPIO_WritePin (TX_EN_GPIO_Port,    TX_EN_Pin,    GPIO_PIN_SET)

/*RX*/
#define RX_EN_ENABLE      HAL_GPIO_WritePin (RX_EN_GPIO_Port,    RX_EN_Pin,    GPIO_PIN_RESET)
#define RX_EN_DISENABLE   HAL_GPIO_WritePin (RX_EN_GPIO_Port,    RX_EN_Pin,    GPIO_PIN_SET)


/*LED通道指示*/
#define LED_CH0_ON        HAL_GPIO_WritePin (RX_S3_GPIO_Port,    RX_S3_Pin,    GPIO_PIN_RESET)
#define LED_CH0_OFF       HAL_GPIO_WritePin (RX_S3_GPIO_Port,    RX_S3_Pin,    GPIO_PIN_SET)

#define BSP_LOW     GPIO_PIN_RESET
#define BSP_HIG     GPIO_PIN_SET

#define BSP_LED_ON  GPIO_PIN_RESET
#define BSP_LED_OFF GPIO_PIN_SET

//static GPIO_TypeDef* bsp_74HC4067_TX_SW_GPIO[4] = {TX_S0_GPIO_Port,TX_S1_GPIO_Port,TX_S2_GPIO_Port,TX_S3_GPIO_Port};
//static uint16_t bsp_74HC4067_TX_SW_Pin[4]       = {TX_S0_Pin,      TX_S1_Pin,      TX_S2_Pin,      TX_S3_Pin};

//static GPIO_TypeDef* bsp_74HC4067_RX_SW_GPIO[4] = {RX_S0_GPIO_Port,RX_S1_GPIO_Port,RX_S2_GPIO_Port,RX_S3_GPIO_Port}; 
//static uint16_t bsp_74HC4067_RX_SW_Pin[4]       = {RX_S0_Pin,      RX_S1_Pin,      RX_S2_Pin,      RX_S3_Pin};

//static GPIO_TypeDef* bsp_74HC4067_LED_CH_GPIO[BSP_74HC4067_CH_MAX] = {LED_CH1_GPIO_Port,    LED_CH2_GPIO_Port,    LED_CH3_GPIO_Port,    LED_CH4_GPIO_Port,    LED_CH5_GPIO_Port,    LED_CH6_GPIO_Port,    LED_CH7_GPIO_Port,    LED_CH8_GPIO_Port,    LED_CH9_GPIO_Port,    LED_CH10_GPIO_Port,    LED_CH11_GPIO_Port,    LED_CH12_GPIO_Port,    LED_CH13_GPIO_Port,    LED_CH14_GPIO_Port,    LED_CH15_GPIO_Port,    LED_CH16_GPIO_Port}; 
//static uint16_t bsp_74HC4067_LED_CH_Pin[BSP_74HC4067_CH_MAX]       = {LED_CH1_Pin,          LED_CH2_Pin,          LED_CH3_Pin,          LED_CH4_Pin,          LED_CH5_Pin,          LED_CH6_Pin,          LED_CH7_Pin,    LED_CH8_Pin,    LED_CH9_Pin,    LED_CH10_Pin,    LED_CH11_Pin,    LED_CH12_Pin,                                        LED_CH13_Pin,          LED_CH14_Pin,          LED_CH15_Pin,           LED_CH16_Pin};

/*通道转换，硬件实际连接的通道与芯片定义通道不一致*/
static u8 bsp_74HC4067_TX_CH_Conv[BSP_74HC4067_CH_MAX] = {13,14,15,12,11,10,9, 8, 7, 4, 3, 2, 1, 0, 6, 5};
static u8 bsp_74HC4067_RX_CH_Conv[BSP_74HC4067_CH_MAX] = {13,14,15,8, 9, 10,11,12,0, 1, 2, 3, 4, 5, 6, 7};


static void bsp_74HC4067_Init(void);
static void bsp_74HC4067_Set_CH(u8 CH);
static u8 bsp_74HC4067_Get_CH(void);

static u8 bsp_74HC4067_CH;

bsp_74HC4067_t UartCH_Config =
{
    .init  = bsp_74HC4067_Init,
    .ch_set = bsp_74HC4067_Set_CH,
    .ch_get = bsp_74HC4067_Get_CH,
};
/*其他外设初始化后快速闪烁，提示初始化完成*/
static void bsp_74HC4067_Init(void)
{
    bsp_74HC4067_CH = 0;
//    TX_EN_ENABLE;
//    RX_EN_ENABLE;
    bsp_74HC4067_Set_CH(0);
}


//static u8 CH;
static void bsp_74HC4067_Set_CH(u8 CH1)
{
    u8 CH = 0;
    if(CH >= BSP_74HC4067_CH_MAX)
    {
        return ;
    }
    u8 i,TX_CH,RX_CH;
    bsp_74HC4067_CH = CH;
    TX_CH = bsp_74HC4067_TX_CH_Conv[CH];
    RX_CH = bsp_74HC4067_RX_CH_Conv[CH];
    /*选择对应的通道输出*/
    for(i=0;i<4;i++)
    {
        if((TX_CH >> i) & 0x01)
        {
//            HAL_GPIO_WritePin(bsp_74HC4067_TX_SW_GPIO[i],bsp_74HC4067_TX_SW_Pin[i],BSP_HIG);
        }
        else
        {
//            HAL_GPIO_WritePin(bsp_74HC4067_TX_SW_GPIO[i],bsp_74HC4067_TX_SW_Pin[i],BSP_LOW);
        }
        if((RX_CH >> i) & 0x01)
        {
//            HAL_GPIO_WritePin(bsp_74HC4067_RX_SW_GPIO[i],bsp_74HC4067_RX_SW_Pin[i],BSP_HIG);
        }
        else
        {
//            HAL_GPIO_WritePin(bsp_74HC4067_RX_SW_GPIO[i],bsp_74HC4067_RX_SW_Pin[i],BSP_LOW);
        }
    }
    /*开启对应指示灯*/
    for(i=0;i<BSP_74HC4067_CH_MAX;i++)
    {
        if(i == CH)
        {
//            HAL_GPIO_WritePin(bsp_74HC4067_LED_CH_GPIO[i],bsp_74HC4067_LED_CH_Pin[i],BSP_LED_ON);
        }
        else
        {
//            HAL_GPIO_WritePin(bsp_74HC4067_LED_CH_GPIO[i],bsp_74HC4067_LED_CH_Pin[i],BSP_LED_OFF);
        }
    }
   // HAL_Delay(20);
}

static u8 bsp_74HC4067_Get_CH(void)
{
    return bsp_74HC4067_CH;
}
