#include "DWIN.h"
#include "usart.h"
#include <stdio.h>	//printf
#include <string.h>
#include "main.h"
uint32_t g_dwinbuffer[64] ; // 当用DMA发送的时候 帧头要四字节对齐

void SendMLXDWIN(uint16_t address, uint8_t id, uint8_t max, uint8_t min, uint8_t ave) 
{
    
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;
    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分：ID 和固定字符串
    index += sprintf((char *)&buffer[index], "%02d# %3d   %3d   %3d", id, max, min, ave);

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);
}

void SendPressureDWIN(uint16_t address, uint8_t id, uint16_t value) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分：ID 和固定字符串
	index += sprintf((char *)&buffer[index], "Pr: %02d# %4d", id, value);

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;
		
		HAL_UART_Transmit_DMA(&huart2, buffer, index);

}

void SendH2DWIN(uint16_t address, uint8_t id, float value) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分：ID 和固定字符串
	index += sprintf((char *)&buffer[index], "H2: %02d# %3.2f", id, value);

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);
}

void SendCODWIN(uint16_t address, uint8_t id, float value) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分：ID 和固定字符串
	index += sprintf((char *)&buffer[index], "CO: %02d# %3.2f", id, value);

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);
}

void SendofflineDWIN(uint16_t address, uint8_t id) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;
	
		uint8_t offlinemessage[] = {0x6F ,0x66 ,0x66 ,0x6C ,0x69 ,0x6E ,0x65 ,0x21};
		const uint8_t*data = offlinemessage;
    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;
		index += sprintf((char *)&buffer[index], "%02d#  ", id);
    
		for (uint8_t i = 0; i < 8; i++) 
		{
        buffer[index++] = data[i];
    }
		
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);
}

void SendWENBENDWIN(uint16_t address, const char* data) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分：ID 和固定字符串
	  index += sprintf((char *)&buffer[index], "%s", data);

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);
		
}

void SendHEXNDWIN(uint16_t address, uint16_t data) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址部分（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分（高字节在前）
    buffer[index++] = (data >> 8) & 0xFF;
    buffer[index++] = data & 0xFF;

    // 更新字节数（从写指令 0x82 开始到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);

}

void Send_uint8HEX_DWIN(uint16_t address, const uint8_t* data, uint8_t data_length) 
{
    uint8_t index = 0;
    uint8_t* buffer = (uint8_t*)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分（多字节）
    for (uint8_t i = 0; i < data_length; i++) {
        buffer[index++] = data[i];
    }

    // 更新字节数（从 0x82 到最后一个字节）
    buffer[length_index] = index - length_index - 1;

    // 发送
    HAL_UART_Transmit_DMA(&huart2, buffer, index);
}


void DWIN_SWITCH_PAGES(uint16_t address, uint16_t data) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址部分（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

		buffer[index++] = 0x5A;
		buffer[index++] = 0x01;
	
    // 数据部分（高字节在前）
    buffer[index++] = (data >> 8) & 0xFF;
    buffer[index++] = data & 0xFF;

    // 更新字节数（从写指令 0x82 开始到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);

}

void DWIN_PLAY_MUSIC(uint16_t address, uint16_t data) 
{
	// 5AA5 07 82 00A0 0574 FF00
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址部分（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;
	
    // 数据部分（高字节在前）
    buffer[index++] = (data >> 8) & 0xFF;
    buffer[index++] = data & 0xFF;

		buffer[index++] = 0x80;
		buffer[index++] = 0x00;
		
    // 更新字节数（从写指令 0x82 开始到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    HAL_UART_Transmit_DMA(&huart2, buffer, index);

}

void SendASCIIHEXNDWIN(uint16_t address, const uint8_t* data, uint8_t data_length) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址部分（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分（逐字节添加）
    for (uint8_t i = 0; i < data_length; i++) {
        buffer[index++] = data[i];
    }

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数
    buffer[length_index] = index - length_index - 1;

		HAL_UART_Transmit_DMA(&huart2, buffer, index);

}
void ReadHEXNDWIN(uint16_t address, uint16_t data) 
{
		uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;
	
    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 读指令 83
    buffer[index++] = 0x83;

    // 地址部分（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分（高字节在前）
//    buffer[index++] = (data >> 8) & 0xFF;
    buffer[index++] = data & 0xFF;

    // 更新字节数
    buffer[length_index] = index - length_index - 1;
		
		HAL_UART_Transmit_DMA(&huart2, buffer, index); 

		
}

void SendDWINThreshold(uint16_t address,uint16_t value) 
{
    uint8_t index = 0, *buffer;
		buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 数据部分：ID 和固定字符串
		index += sprintf((char *)&buffer[index], "%4d", value);

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;
		
		HAL_UART_Transmit_DMA(&huart2, buffer, index);

}

//void ConvertToASCII(uint8_t* input, uint8_t* output, size_t length) {
//    for (int i = 0; i < length; i++) {
//        // 将每个字节转换为 ASCII 字符
//        output[i*2] = (input[i] / 16) + '0';   // 高位转换为字符
//        output[i*2 + 1] = (input[i] % 16) + '0'; // 低位转换为字符
//    }
//}

void SendDWINterminal(uint16_t address, const uint8_t* data) 
{
    uint8_t index = 0, *buffer;
    buffer = (uint8_t *)g_dwinbuffer;

    // 帧头
    buffer[index++] = 0x5A;
    buffer[index++] = 0xA5;

    // 先占位字节数
    uint8_t length_index = index;
    buffer[index++] = 0x00;

    // 写指令 82
    buffer[index++] = 0x82;

    // 地址（高字节在前）
    buffer[index++] = (address >> 8) & 0xFF;
    buffer[index++] = address & 0xFF;

    // 将每个字节转换为对应的 ASCII 字符并放入 buffer
    uint8_t ascii_data[8 * 2]; // 存储 ASCII 字符数组，每个字节会被转为两个 ASCII 字符
    ConvertToASCII((uint8_t*)data, ascii_data, 8);

    // 将转换后的 ASCII 数据添加到 buffer
    for (int i = 0; i < 8 * 2; i++) {
        buffer[index++] = ascii_data[i];
    }

    // 帧尾
    buffer[index++] = 0xFF;
    buffer[index++] = 0xFF;

    // 更新字节数（从0x82到帧尾，包括帧尾）
    buffer[length_index] = index - length_index - 1;

    // 发送数据
    HAL_UART_Transmit_DMA(&huart2, buffer, index);
}

void ConvertToASCII(uint8_t* input, uint8_t* output, size_t length) 
{
    for (int i = 0; i < length; i++) {
        // 将每个字节转换为 ASCII 字符
        output[i*2] = (input[i] / 16) + '0';   // 高位转换为字符
        output[i*2 + 1] = (input[i] % 16) + '0'; // 低位转换为字符
    }
}

