#include "app_lsdn.h"
#include "RTE_GPIO.h"
#include "GenDelay.h"
#include "../UART/UART.h"
#include "Key.h"
#include <string.h>

_AimaDataText Aimadata;

// 假设的GPIO操作函数
void set_pin_high(uint8_t mode)
{
}
void set_pin_low(uint8_t mode)
{
}
void lsdn_delay_us(uint32_t us)
{
    Gen_TimeDelay(us, 50u);
}

// 发送同步信号
void send_sync(uint8_t mode) {
    set_pin_low(mode);
    lsdn_delay_us(10000);   // 高电平1ms (T2)
    set_pin_high(mode);
    lsdn_delay_us(1000);  // 低电平10ms (T1≥10ms)
}

// 发送单个比特（逻辑"1"或"0"）
void send_bit(uint8_t mode, uint8_t bit) {
    if (bit) {
        
        set_pin_low(mode);
        lsdn_delay_us(500);  // 低电平0.5ms (逻辑"1"的T5)
        set_pin_high(mode);
        lsdn_delay_us(1500);  // 高电平1.5ms (逻辑"1"的T6)
    } else {
        
        set_pin_low(mode);
        lsdn_delay_us(1500);   // 低电平1.5ms (逻辑"0"的T7)
        set_pin_high(mode);
        lsdn_delay_us(500);   // 高电平0.5ms (逻辑"0"的T8)
    }
}

// 发送停止信号
void send_stop(uint8_t mode) {
    
    set_pin_low(mode);
    lsdn_delay_us(5000);   // 低电平50ms (N>50，取最小值50ms)

    set_pin_high(mode);
    lsdn_delay_us(50000);    // 高电平5ms (T9)
}

// 发送完整数据（按字节发送，高位优先）
void lsdn_send_data(uint8_t mode, const uint8_t *data, uint32_t len) {
    send_sync(mode); // 发送同步信号
    for (uint32_t i = 0; i < len; i++) {
        uint8_t byte = data[i];
        for (int j = 0; j < 8; j++) { // 高位先发
            send_bit(mode, (byte >> j) & 0x01);
        }
    }
    send_stop(mode); // 发送停止信号
}






uint8_t c_YXT_sum(uint8_t *buf,uint8_t len)
{
	uint8_t   i=0;
	uint16_t ret = 0;
	for(i=0;i<len;i++)
	{
		ret += buf[i];
	}
	return ret;
}

uint8_t crc8_calculate(const uint8_t *data, uint8_t length)
{
	uint8_t crc = 0x00;  // 初始值
	const uint8_t polynomial = 0x07;  // CRC-8标准多项式
	for (uint32_t i = 0; i < length; i++)
	{
		crc ^= data[i];
		for(uint8_t j = 0; j < 8; j++)
		{
			if(crc & 0x80)
			{
				crc = (crc << 1) ^ polynomial;
			}
			else
			{
				crc <<= 1;
			}
		}
	}
	return crc;
}


uint8_t Aima_SLF_DataBuf[50];

uint8_t flag1 = 0;
// mode  = 0,485接口；mode=1，一键启动接口
void Aima_SLF(uint8_t mode, uint8_t cmd, uint8_t data, uint8_t data_len)
{
    Aima_SLF_DataBuf[0] = 0x5A;
    Aima_SLF_DataBuf[1] = 0xA5;     //帧头
    Aima_SLF_DataBuf[2] = 0x02;     //从机指令
    Aima_SLF_DataBuf[3] = 0x11;
    Aima_SLF_DataBuf[4] = 0x01;     //设备类型
    Aima_SLF_DataBuf[5] = 0x01;     //功能码
    Aima_SLF_DataBuf[6] = 0x03;     //数据长度
    Aima_SLF_DataBuf[7] = 0xFF;     //数据内容
    Aima_SLF_DataBuf[8] = 0xFF;     //数据内容
    Aima_SLF_DataBuf[9] = 0x00;     //数据内容
    Aima_SLF_DataBuf[10] = 0xC6;       //校验
    Aima_SLF_DataBuf[11] = 0xFF;        //帧尾
    lsdn_send_data(mode, Aima_SLF_DataBuf, 12);
    // 5C 2f
    // 0101 1100 0010 1111
}

uint8_t AimaDateUart[Aima_Data_Length];
void AIMA_Uart_Send(void)
{
    AimaDateUart[0]  = 0x5A;
    AimaDateUart[1]  = 0xA5;     //帧头
    AimaDateUart[2]  = 0x02;
    AimaDateUart[3]  = 0x11;
    AimaDateUart[4]  = 0x01;     //设备地址
    AimaDateUart[5]  = 0x01;     //功能码
    AimaDateUart[6]  = 0x03;     //数据长度
    AimaDateUart[7]  = Aimadata.Msg[0];
    AimaDateUart[8]  = Aimadata.Msg[1];
    AimaDateUart[9]  = Aimadata.Msg[2];     //数据
    AimaDateUart[10]  = crc8_calculate(&AimaDateUart[6], (AimaDateUart[6] + 1));     //校验
    AimaDateUart[11] = 0xFF;     //帧尾
    // UART_Ch2_Send_Multiple_Byte(AimaDateUart, sizeof(AimaDateUart));
}

uint8_t Scroll_Wheel;
uint16_t KeySendTime;
void Key_Judgment_Service(void)
{
    // memset(Aimadata.Msg, 0xFF, Aima_Data_Length);
    Aimadata.Msg[0] = 0xFF;
    Aimadata.Msg[1] = 0xFF;

    if(Key_Mode == 1)       //左键
    {
        Aimadata.Sig.media_left_key = 0x00;
    }
    else if(Key_Mode == 2)      //右键
    {
        Aimadata.Sig.media_right_key = 0x00;
    }
    else if((Key_Mode == 4))      //中键
    {
        Aimadata.Sig.media_middle_key = 0x00;
    }
    else
    {
        ;
    }

    if((Key_Mode == 1) || (Key_Mode == 2))
    {
        if(KeySendTime <= 10)
        {
            KeySendTime++;
        }
        else
        {
            Key_Mode = 0;
            KeySendTime = 0;
        }
    }
    else if(Key_Mode == 4)
    {
        if(KeySendTime <= 4000)
        {
            KeySendTime++;
        }
        else
        {
            Key_Mode = 5;
        }
    }
    else
    {
        KeySendTime = 0;
    }
    Aimadata.Sig.media_key_pos = Scroll_Wheel;
}














