#include "MCU_ARM.h"
#include "uart.h"
#include "uart1.h"
extern QSerialPort SerialPort;
UART *uart_mcu =new UART();
extern uint16_t CRC;
bool tag = true;

MCU_ARM::MCU_ARM():m_serialPort()
{
}

void MCU_ARM::StationData(QByteArray sharedata)
{
    QByteArray data;
    uint8_t CRC_lowData;
    uint8_t len = sharedata.size();
    uint8_t cout = 0;
    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x13;
    data[3] = 0x01;
    data[4] = len/256;
    data[5] = len%256;
    QByteArray finish =QByteArray::fromHex(sharedata.toHex());
    while(cout!=len)
    {
    data[6+cout] = finish[cout];
    cout++;
    }
    uart_mcu->ModBusCRC16(data);
    CRC_lowData = CRC;
    CRC = CRC>>8;
    data[6+len] = CRC_lowData;
    data[7+len] = CRC;
    uart_mcu->ShuJu_Write(data);
}

void MCU_ARM::setFanTiming(uint16_t starthour,uint16_t startminute,uint16_t timing){
    QByteArray data;
    uint8_t lowData;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x24;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x04;
    data[6] = 0x00;
    data[7] = starthour;
    data[8] = startminute;
    lowData = timing;
    timing = timing >> 8;
    data[9] = lowData;

    uart_mcu->ModBusCRC16(data);
    CRC_lowData = CRC;
    CRC = CRC>>8;
    data[10] = CRC_lowData;
    data[11] = CRC;
    uart_mcu->ShuJu_Write(data);
}

void MCU_ARM::setFanState(uint8_t state){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x23;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = state;
    uart_mcu->ModBusCRC16(data);
    CRC_lowData = CRC;
    CRC = CRC>>8;
    data[7] = CRC_lowData;
    data[8] = CRC;
    uart_mcu->ShuJu_Write(data);
}

void MCU_ARM::setEnable(uint8_t enable){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x12;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = enable;
    uart_mcu->ModBusCRC16(data);
    CRC_lowData = CRC;
    CRC = CRC>>8;
    data[7] = CRC_lowData;
    data[8] = CRC;
    uart_mcu->ShuJu_Write(data);
}

void MCU_ARM::setFanStartT(uint16_t temperature){
    QByteArray data;
    uint8_t lowData;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x22;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x02;
    lowData = temperature;
    temperature = temperature >> 8;
    data[6] = temperature;
    data[7] = lowData;
    uart_mcu->ModBusCRC16(data);
    CRC_lowData = CRC;
    CRC = CRC>>8;
    data[8] = CRC_lowData;
    data[9] = CRC;
    uart_mcu->ShuJu_Write(data);
}

void MCU_ARM::setWirelessT(int16_t temperature){
    temperature = 0;
    printf("this function unable to use, now...%d\n");
}

void MCU_ARM::setBuzzerState(uint8_t state){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    data[2] = 0x25;
    data[3] = 0x01;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = state;
    uart_mcu->ModBusCRC16(data);
    CRC_lowData = CRC;
    CRC = CRC>>8;
    data[7] = CRC_lowData;
    data[8] = CRC;
    uart_mcu->ShuJu_Write(data);
}

void MCU_ARM::testSerialPort(uint8_t uart){
    QByteArray data;
    uint8_t CRC_lowData;

    data[0] = 0xAA;
    data[1] = 0x55;
    if(uart == 1)
    {
    data[2] = 0x10;
    }
    else if(uart == 2)
    {
    data[2] = 0x11;
    }
    if(tag){
        data[3] = 0x01;
    } else {
        data[3] = 0x03;
    }
    tag = !tag;
    data[4] = 0x00;
    data[5] = 0x01;
    data[6] = 0x0;
    uart_mcu->ModBusCRC16(data);
    CRC_lowData = CRC;
    CRC = CRC>>8;
    data[7] = CRC_lowData;
    data[8] = CRC;
    uart_mcu->ShuJu_Write(data);
}

MCU_ARM::~MCU_ARM(){
}



