#include "stm32f103.h"
#include "com_CAN.h"
#include "tc_tcu.h"
#include "tcXHHW.h"

int ccu_STM32_init(stm32f103_t* dev, const char* can_name, const int bitrate, int enable)
{
    if (dev)
    {
        pthread_mutex_init(&dev->mutex, NULL);
        dev->quit = 0;
        dev->SA = 0x10;
        dev->DA = 0x11;
        dev->led[0] = 1;
        dev->led[1] = 1;
        dev->led[2] = 1;
        dev->led[3] = 1;
        dev->GUNA_State = 1;
        dev->GUNB_State = 1;
        return 0;
        // return com_CAN_init(&dev->can, can_name, bitrate);
    }
    return -1;
}

void ccu_STM32_exit(stm32f103_t* dev)
{
    if (dev)
    {
        pthread_mutex_destroy(&dev->mutex);
        // com_CAN_exit(&dev->can);
    }
}

int ccu_STM32_TransmitVoltage(stm32f103_t* dev, int enable)
{
    // if (dev)
    // {
    //     uint8_t cmd[2]={0x5,0x0};
    //     if (enable) cmd[1] = 0x1; else cmd[1] = 0x0;
    //     return com_CAN_send_PDU1(&dev->can, 0x00, 0x10, dev->DA, dev->SA, cmd, 2);
    // }
    return -1;
}

int ccu_STM32_TransmitState(stm32f103_t* dev, int enable)
{
    // if (dev)
    // {
    //     uint8_t cmd[2]={0x4,0x0};
    //     if (enable) cmd[1] = 0x1; else cmd[1] = 0x0;
    //     return com_CAN_send_PDU1(&dev->can, 0x00, 0x10, dev->DA, dev->SA, cmd, 2);
    // }
    return -1;
}

int ccu_STM32_ControlContactor(stm32f103_t* dev, int idx, int enable)
{
    if (dev)
    {
        // if (idx>3) idx = 3;
        // if (idx<0) idx = 0;
        // uint8_t cmd[2]={0x3,0x0};
        // cmd[0] = idx;
        // if (enable) cmd[1] = 0x1; else cmd[1] = 0x0;
        // return com_CAN_send_PDU1(&dev->can, 0x00, 0x10, dev->DA, dev->SA, cmd, 2);
        set_KMD_out(idx, enable);
        return 0;
    }
    return -1;
}

int ccu_STM32_ControlLED(stm32f103_t* dev, int idx, int enable)
{
    if (dev)
    {
        // if (idx>9) idx = 9;
        // if (idx<6) idx = 6;
        // uint8_t cmd[2]={0x3,0x0};
        // cmd[0] = idx;
        // if (enable) cmd[1] = 0x1; else cmd[1] = 0x0;
        // return com_CAN_send_PDU1(&dev->can, 0x00, 0x10, dev->DA, dev->SA, cmd, 2);
        switch(idx)
        {
            case 6: set_GUNLED_on(1, enable); break;//GREEN
            case 7: set_GUNLED_on(0, enable); break;//RED
            case 8: set_GUNLED_on(2, enable); break;//RED
            case 9: set_GUNLED_on(3, enable); break;//GREEN
        }
    }
    return -1;
}

void ccu_STM32_LED(stm32f103_t* dev)
{
    //if (dev->count % 2 == 0)
    {
        pthread_mutex_lock(&dev->mutex);
        switch (dev->GUNA_State)
        {
        case 0:ccu_GUNA_LED_OnOff(0);break;
        case 1:ccu_GUNA_LED_OnOff(1);break;
        default:ccu_GUNA_LED_Toggle();break;
        }
        switch (dev->GUNB_State)
        {
        case 0:ccu_GUNB_LED_OnOff(0);break;
        case 1:ccu_GUNB_LED_OnOff(1);break;
        default:ccu_GUNB_LED_Toggle();break;
        }
        pthread_mutex_unlock(&dev->mutex);
    }
}

void* ccu_STM32_loop(void* argv)
{
    stm32f103_t* dev = (stm32f103_t*)argv;
    if (dev)
    {
        struct can_frame frame;
        ccu_STM32_TransmitVoltage(dev, 1);
        ccu_STM32_TransmitState(dev, 1);
        ccu_STM32_ControlContactor(dev, 0, 0);
        ccu_STM32_ControlContactor(dev, 1, 0);
        ccu_STM32_ControlContactor(dev, 2, 0);
        ccu_STM32_ControlContactor(dev, 3, 0);
        ccu_STM32_ControlLED(dev, 6, 1);dev->led[0] = 1;
        ccu_STM32_ControlLED(dev, 7, 1);dev->led[1] = 1;
        ccu_STM32_ControlLED(dev, 8, 1);dev->led[2] = 1;
        ccu_STM32_ControlLED(dev, 9, 1);dev->led[3] = 1;
        while (dev->quit == 0)
        {
            // memset(&frame, 0, sizeof(struct can_frame));
            // if (com_CAN_read(&dev->can, &frame, 1000) == sizeof(frame))
            msleep(1000);
            {
                pthread_mutex_lock(&dev->mutex);
                dev->state[0] = get_KMDFB(0); //1：吸合，0：断开
                dev->state[1] = get_KMDFB(1); //1：吸合，0：断开
                dev->state[2] = get_KMDFB(2); //1：吸合，0：断开
                dev->state[3] = get_KMDFB(3); //1：吸合，0：断开
                dev->count++;
                pthread_mutex_unlock(&dev->mutex);
                ccu_STM32_LED(dev);
                //printi("K0:%d, K1:%d, K2:%d, K3:%d\n", dev->state[0], dev->state[1], dev->state[2], dev->state[3]);
                if (dev->state[0]==1 && dev->state[1]==1) { ccu_Update_GUNA_ContactorState(1); }
                if (dev->state[2]==1 && dev->state[3]==1) { ccu_Update_GUNB_ContactorState(1); }
                if (dev->state[0]==0 && dev->state[1]==0) { ccu_Update_GUNA_ContactorState(0); }
                if (dev->state[2]==0 && dev->state[3]==0) { ccu_Update_GUNB_ContactorState(0); }
            }
        }
    }
    return NULL;
}
