/* Includes ------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>

#include "cmd.h"
#include "fifo.h"
#include "common.h"
#include "ca_cau.h"
#include "serial.h"
#include "modbus.h"
#include "hwgpio.h"
#include "ca_main.h"
#include "ca_CL106d.h"


//#include "ca_CL106d.h"
//#include "serial.h"
//#include "hwgpio.h"

cl106d_ctrl_t cl106d_ctrl;

int cl106d_checksum(unsigned char *pdata, int len)
{
    int i;
    unsigned char check = 0;
    for(i = 1;i < len;i++)
    {
        check ^= *(pdata + i);
    }
    pdata[len] = check;
    return 0;
}

int cl106d_packet(unsigned char *pbuf)
{
    ClHead_t *head = (ClHead_t *)pbuf;
    head->ucHead = 0x81;
    head->ucRxID = 0x01;
    head->ucTxID = 0x07;
    head->ucLength = 0x06;
    head->ucCmd = 0xc9;
    int len = 5;
    cl106d_checksum((unsigned char *)head, len);
    len++;
    return len;
}

/**********************************************************************
* 函数名称:     cl106d_connet
* 功能描述:
* 输入参数: none
*
* 输出参数: none
* 返 回 值: 0:failure 1:success
* 其它说明:
* 修改日期: 2023/12/08
***********************************************************************/
int cl106d_connet()
{
    int i;
    int ret = 0;
    int len = cl106d_packet(cl106d_ctrl.send_buf);

    for (i = 0; i < 3; i++)
    {
        GPIO_ENABLE_SEND(1);
        usleep(1000);
        serial_write(cl106d_ctrl.fd, cl106d_ctrl.send_buf, len);
        print_raw_data(cl106d_ctrl.send_buf, len);
        usleep(1000*6);
        GPIO_ENABLE_SEND(0);

        len = serial_read(cl106d_ctrl.fd, cl106d_ctrl.recv_buf, sizeof(cl106d_ctrl.recv_buf));
        printh("[%s:%d] len=%d\n", __FUNCTION__, __LINE__, len);
        print_raw_data(cl106d_ctrl.recv_buf, len);
        if (len > 0)
        {
            ret = 1;
            break;
        }
    }
    return ret;
}

/**********************************************************************
* 函数名称:     cl106d_setpower
* 功能描述:
* 输入参数: none
*
* 输出参数: none
* 返 回 值: 0:failure 1:success
* 其它说明:
* 修改日期: 2023/12/08
***********************************************************************/
int cl106d_setpower(float vol,float cur)
{
    int ret = 0;
    printh("cl106dSetPower vol:%.5f cul:%.5f",vol,cur);
    unsigned char cmdExample[] = {
        0x81,0x01,0x07,0x49,0xA3,0x05,0x46,0x3F,
        0x00,0x00,0x00,0x00,//#QUc 相位值放大 1 万倍取整： 120 * 10000 = 1200000 = 0x124F80
        0x00,0x00,0x00,0x00,//#QUb
        0x00,0x00,0x00,0x00,//#QUa
        0x00,0x00,0x00,0x00,//#QIc
        0x00,0x00,0x00,0x00,//#QIb
        0x00,0x00,0x00,0x00,//#QIa  32 Byte
        0xff,
        0x00,0x00,0x00,0x00,0x00,//#UC 57.7 = 577000 * 10-4（577000 = 0x8CDE8， -4 = FC）
        0x00,0x00,0x00,0x00,0x00,//#UB
        0x00,0x00,0x00,0x00,0xfc,//#UA  [43]    4位小数
        0x00,0x00,0x00,0x00,0x00,//#IC
        0x00,0x00,0x00,0x00,0x00,//#IB
        0x00,0x00,0x00,0x00,0xfa,//#IA  [58]    6位小数
        0x20,0xA1,0x07,0x00,//#F 频率值放大 1 万倍取整： 50 * 10000 = 500000 = 0x7A120
        0x07,0x07,0x3f,0x3f,0x00, 
        0x00//CS #校验和字节，替换为实际计算异或和  41Byte
    }; //73Byte

    unsigned int *pVal = (unsigned int *)&cmdExample[43];
    *pVal = (unsigned int)(vol * 10000);

    unsigned int *pcul = (unsigned int *)&cmdExample[58];
    *pcul = (unsigned int)(cur * 1000000);
    memcpy(cl106d_ctrl.send_buf,cmdExample,73);
    cl106d_checksum(cl106d_ctrl.send_buf, 72);

    int i = 0;
    for ( i = 0; i < 10; i++)
    {
        GPIO_ENABLE_SEND(1);
        usleep(1000);
        serial_write(cl106d_ctrl.fd, cl106d_ctrl.send_buf, 73);
        usleep((1040)*73);
        usleep(1000);
        print_raw_data(cl106d_ctrl.send_buf, 73);
        GPIO_ENABLE_SEND(0);
        int len = serial_read(cl106d_ctrl.fd, cl106d_ctrl.recv_buf, sizeof(cl106d_ctrl.recv_buf));
        printh("[%s:%d] len=%d\n", __FUNCTION__, __LINE__, len);
        print_raw_data(cl106d_ctrl.recv_buf, len);
        if (len  > 0) 
        {
            ret = 1;
            break;
        }
        else
        {
            GPIO_SHUTDOWN_PWR();
        }
    }
    return ret;
}

int cl106d_init()
{
    memset(&cl106d_ctrl,0,sizeof(cl106d_ctrl_t));
    //2 打开串口
    cl106d_ctrl.fd = serial_open("/dev/ttymxc3", 9600);
    if (cl106d_ctrl.fd < 0)
    {
        printh("[%s:%d]serial_open failed!\n", __FUNCTION__, __LINE__);
        return -1;
    }   
    return 0;
}
