#include "rs485.h"
#include "SysTick.h"
#include "USART.h"
#include "Relays.h"
#include "myfreertos.h"

u8 prev_sendbuf[41] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; 
u8 sendbuf[41] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; 
u8 sendbuf_crc[20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
extern u8 global_buffer[64];
extern u8 RS485_RX_BUF_COPY[128];

extern u8 now_stage;
extern u8 chour;
extern u8 cminute;
extern u8 total[];
extern u8 hot_clod_flag;
extern u8 humidity_flag;
extern u8 ALARM;
extern u8 NUM;
u8 TEM1;
u8 TEM2;
u8 HUM1;
u8 HUM2;
u8 CO2_1;
u8 CO2_2;
u16 RED_LIGHT;
u16 WHITE_LIGHT;
u16 BLUE_LIGHT;
u8 red_light1;
u8 red_light2;
u8 white_light1;
u8 white_light2;
u8 blue_light1;
u8 blue_light2;
u8 crc_num1 = 0xFF;
u8 crc_num2 = 0xFF;
// extern u8 TEM1;
// extern u8 TEM2;
// extern u8 HUM1;
// extern u8 HUM2;
// extern u8 CO2_1;
// extern u8 CO2_2;
extern int T;
extern int H;
extern int C;
extern PID pid;
// extern u16 RED_LIGHT;
// extern u16 WHITE_LIGHT;
// extern u16 BLUE_LIGHT;
// extern u8 red_light1;
// extern u8 red_light2;
// extern u8 white_light1;
// extern u8 white_light2;
// extern u8 blue_light1;
// extern u8 blue_light2;
// CRC verifies the information added later on
const u8 auchCRCHi[] = {
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40};

const u8 auchCRCLo[] = {
	0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
	0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
	0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
	0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
	0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
	0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
	0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
	0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
	0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
	0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
	0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
	0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
	0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40};

u8 RS485_RX_BUF[128];

u8 RS485_RX_CNT = 0;

u8 RS485_RX_Flag = 0;

/**
 * return 1 if diff, 0 if same
*/
u8 cmp_str(u8 *prev, u8*curr, u8 len) {
	for (u8 i = 0; i < len; i++) {
		if (prev[i] != curr[i]) {
			return 1;
		}
	}
	return 0;
}

void cp_str_to_prev(u8 *prev, u8*curr, u8 len) {
	for (u8 i = 0; i < len; i++) {
		prev[i] = curr[i];
	}
}

void GetCRC16(u8 *puchMsg, u16 usDataLen, u8 *uchCRCHi, u8 *uchCRCLo)
{
	*uchCRCHi = 0xFF;
	*uchCRCLo = 0xFF;
	u32 uIndex;
	while (usDataLen--)
	{
		uIndex = *uchCRCHi ^ *puchMsg++;
		*uchCRCHi = *uchCRCLo ^ auchCRCHi[uIndex];
		*uchCRCLo = auchCRCLo[uIndex];
	}
	// return ((uchCRCHi<< 8)  | (uchCRCLo)) ;
}
/**
 * Calculate CRC value using lookup table method and verify
 * @return int 1 means success, 0 means false
*/
u8 CRC16_check(u8 *puchMsg, u16 usDataLen)
{
	u8 uchCRCHi = 0xFF;
	u8 uchCRCLo = 0xFF;
	u32 uIndex;
	while (usDataLen--)
	{
		uIndex = uchCRCHi ^ *puchMsg++;
		uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex];
		uchCRCLo = auchCRCLo[uIndex];
	}
	if (uchCRCHi == *puchMsg++ && uchCRCLo == *puchMsg++)
	{
		return 1;
	}
	else
		return 0;
}
void USART1_IRQHandler(void)
{
	u8 res;

	while (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // receive data
	{
		res = USART_ReceiveData(USART1); // read received data
		if (RS485_RX_CNT < 128)
		{
			RS485_RX_BUF[RS485_RX_CNT] = res; // Record received values
			RS485_RX_CNT++;					  // Received data count increased by 1
		}
	}
}

void SN74CB3Q3253_Init(void)
{

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 

	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; // PB5 PB6 PB7
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;					
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;					// IO??????50MHz
	GPIO_Init(GPIOB, &GPIO_InitStructure);								
	GPIO_ResetBits(GPIOB, GPIO_Pin_7);									// PB7?????
}

// RS485_1_Init->J6 ??PB5->S1,PB6->S0,PB7->USART_OE
// SN74CB3Q3253??��?????? - ??��???????????????????OE S1 S0???????001???1B2 2B2???

// pclk1:PCLK1??????(Mhz)
// bound:??????
void RS485_1_Init(u32 bound)
{

	SN74CB3Q3253_Init();
	GPIO_ResetBits(GPIOB, GPIO_Pin_5); // S1?????????
	GPIO_SetBits(GPIOB, GPIO_Pin_6);   // S0?????????

	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);						 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;		 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;		// PA9
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;			  // PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);	
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); 

	USART_InitStructure.USART_BaudRate = bound;										
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;				
	USART_InitStructure.USART_StopBits = USART_StopBits_1;							
	USART_InitStructure.USART_Parity = USART_Parity_No;								
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;					
	USART_Init(USART1, &USART_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;		  
	// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
	// // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //???????2??
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5; 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
	NVIC_Init(&NVIC_InitStructure);					

	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
	USART_Cmd(USART1, ENABLE);					   

	RS485_1_TX_EN = 0; 
}

// RS485_2_Init->J7 ??PB5->S1,PB6->S0,PB7->USART_OE
// SN74CB3Q3253??��?????? - ??��???????????????????OE S1 S0???????000???1B1 2B1???
void RS485_2_Init(u32 bound)
{

	SN74CB3Q3253_Init();
	GPIO_ResetBits(GPIOB, GPIO_Pin_5 | GPIO_Pin_6);

	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);						 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;		 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;		// PA9
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;			  // PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);	
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); 

	USART_InitStructure.USART_BaudRate = bound;										
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;				
	USART_InitStructure.USART_StopBits = USART_StopBits_1;							
	USART_InitStructure.USART_Parity = USART_Parity_No;								
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;					
	USART_Init(USART1, &USART_InitStructure);
	; 

	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;		  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5; 
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //???????2??
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
	NVIC_Init(&NVIC_InitStructure);					

	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
	USART_Cmd(USART1, ENABLE);					   

	RS485_2_TX_EN = 0; 
}

// RS485_3_Init->J7 ??PB5->S1,PB6->S0,PB7->USART_OE
// SN74CB3Q3253??��?????? - ??��???????????????????OE S1 S0???????010???1B3 2B3???
void RS485_3_Init(u32 bound)
{

	SN74CB3Q3253_Init();
	GPIO_ResetBits(GPIOB, GPIO_Pin_6); // S0?????????
	GPIO_SetBits(GPIOB, GPIO_Pin_5);   // S1?????????

	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);						 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;		 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;		// PA9
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;			  // PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);	
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); 

	USART_InitStructure.USART_BaudRate = bound;										
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;						
	USART_InitStructure.USART_StopBits = USART_StopBits_1;							
	USART_InitStructure.USART_Parity = USART_Parity_No;								
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;					
	USART_Init(USART1, &USART_InitStructure);
	;

	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;		  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
	// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //???????2??
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
	NVIC_Init(&NVIC_InitStructure);					

	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
	USART_Cmd(USART1, ENABLE);					   

	RS485_3_TX_EN = 0; 
}

void RS485_1_Send_Data(u8 *buf, u8 len)
{
	u8 t;

	RS485_1_TX_EN = 1;

	for (t = 0; t < len; t++)
	{
		while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
			;
		USART_SendData(USART1, buf[t]);
	}

	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
		;
	RS485_RX_CNT = 0;

	RS485_1_TX_EN = 0;
}

void RS485_1_Send_Data_1(u8 *buf, u8 len)
{
	u8 t;

	RS485_1_TX_EN = 1;

	for (t = 0; t < len; t++)
	{
		while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
			;
		USART_SendData(USART1, buf[t]);
	}

	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
		;
	// RS485_RX_CNT=0;

	RS485_1_TX_EN = 0;
}

void RS485_1_Send_Data_2(void)
{
	sendbuf[0] = 0xEE;
	sendbuf[1] = 0xB5;
	sendbuf[2] = 0x05;
	
	sendbuf[37] = 0xFF;
	sendbuf[38] = 0xFC;
	sendbuf[39] = 0xFF;
	sendbuf[40] = 0xFF;
	// u8 a;
	if (T <= 1000 && T >= -1000)
	{
		if (T < 0) {
			T = (~(T - 1)) & 0xFFFF;
		}
		TEM1 = (T & 0xFF00) >> 8;
		TEM2 = T & 0x00FF;
	}
	if (H <= 1000)
	{
		HUM1 = (H & 0xFF00) >> 8;
		HUM2 = H & 0x00FF;
	}
	sendbuf[3] = TEM1;
	sendbuf[4] = TEM2;
	sendbuf[5] = HUM1;
	sendbuf[6] = HUM2;
	RED_LIGHT = total[9 + 18 * (now_stage - 1)] << 8 | total[10 + 18 * (now_stage - 1)];
	BLUE_LIGHT = total[11 + 18 * (now_stage - 1)] << 8 | total[12 + 18 * (now_stage - 1)];
	WHITE_LIGHT = total[13 + 18 * (now_stage - 1)] << 8 | total[14 + 18 * (now_stage - 1)];
	if (RED_LIGHT <= 1000)
	{
		red_light1 = total[9 + 18 * (now_stage - 1)];
		red_light2 = total[10 + 18 * (now_stage - 1)];
	}
	if (BLUE_LIGHT <= 1000)
	{
		blue_light1 = total[11 + 18 * (now_stage - 1)];
		blue_light2 = total[12 + 18 * (now_stage - 1)];
	}
	if (WHITE_LIGHT <= 1000)
	{
		white_light1 = total[13 + 18 * (now_stage - 1)];
		white_light2 = total[14 + 18 * (now_stage - 1)];
	}
	sendbuf[7] = red_light1;
	sendbuf[8] = red_light2;
	sendbuf[9] = blue_light1;
	sendbuf[10] = blue_light2;
	sendbuf[11] = white_light1;
	sendbuf[12] = white_light2;
	if (C <= 5000)
	{
		CO2_1 = (C & 0xFF00) >> 8;
		CO2_1 = C & 0x00FF;
	}
	sendbuf[13] = CO2_1;
	sendbuf[14] = CO2_2;
	if (now_stage > 0 && now_stage < 7 && total[3 + 18 * (now_stage - 1)] < 24 && total[4 + 18 * (now_stage - 1)] < 60 && chour < 24 && cminute < 60)
	{
		sendbuf[15] = now_stage;					   // current state
		sendbuf[16] = total[3 + 18 * (now_stage - 1)]; // stage hour
		sendbuf[17] = total[4 + 18 * (now_stage - 1)]; // stage minute
		sendbuf[18] = chour;						   // run hour
		sendbuf[19] = cminute;						   // run minute
	}
	// now_stage = 1;
	sendbuf[15] = now_stage;					   // current state
	sendbuf[16] = total[3 + 18 * (now_stage - 1)]; // stage hour
	sendbuf[17] = total[4 + 18 * (now_stage - 1)]; // stage minute
	sendbuf[18] = chour;						   // run hour
	sendbuf[19] = cminute;						   // run minute
	sendbuf[20] = hot_clod_flag; // hot clod state
	sendbuf[21] = humidity_flag; // humidity state
	sendbuf[22] = ALARM;		 // ALARM state

	// sendbuf[23] = (int)pid.Kp / 256;			  // Kp 110
	// sendbuf[24] = (int)pid.Kp % 256;			  // Kp 110
	// sendbuf[25] = ((int)(pid.Ki * 100000)) / 256; // Ti 0.001
	// sendbuf[26] = ((int)(pid.Ki * 100000)) % 256; // Ti 0.001
	// sendbuf[27] = (int)pid.Kd / 256;			  // Td 340
	// sendbuf[28] = (int)pid.Kd % 256;			  // Td 340

	// TODO::temp
	sendbuf[23] = (int)(-pid.SEk) / 256; // SEk
	sendbuf[24] = (int)(-pid.SEk) % 256;			  
	sendbuf[25] = (int)pid.Ki / 256; // compressor speed
	sendbuf[26] = (int)pid.Ki % 256;
	sendbuf[27] = (int)pid.Kd / 256;			  //  heater percent
	sendbuf[28] = (int)pid.Kd % 256;			  

	// num = (((pid.OUT * 350) / pid.pwmcycle) - 1);

	int out1 = pid.OUT;
	// speed count
	int speed_count = out1 / 200.0 * (6000 - 1500) + 1500;
	if (speed_count > 6000)
	{
		speed_count = 6000;
	}
	sendbuf[29] = (speed_count) / 256;
	sendbuf[30] = (speed_count) % 256;
	// sendbuf[29] = ((int)(pid.OUT * 1000)) / 256;
	// sendbuf[30] = ((int)(pid.OUT * 1000)) % 256;

	// bufcut_Init(sendbuf_crc, sendbuf, 3, 31);
	// GetCRC16(sendbuf_crc, 28, &crc_num1, &crc_num2);


	int tem_offset_10times = (int)(pid.tem_offset * 10);

	if (tem_offset_10times < 0) {
		tem_offset_10times = (~(tem_offset_10times - 1)) & 0xFFFF;
	}
	sendbuf[31] = tem_offset_10times / 256; // tem offset
	sendbuf[32] = tem_offset_10times % 256;

	sendbuf[33] = ((int)(pid.tem_threshold * 10)) / 256; // tem threshold
	sendbuf[34] = ((int)(pid.tem_threshold * 10)) % 256;

	GetCRC16(sendbuf, 35, &crc_num1, &crc_num2);
	sendbuf[35] = crc_num1;
	sendbuf[36] = crc_num2;

	// u8 tmpabc[] = {0xAA, 0xBB, 0xCC};
	// RS485_1_Send_Data(tmpabc, 3);
	// only when modify happen, then send
	if (cmp_str(prev_sendbuf, sendbuf, 41) != 0) {

		RS485_1_Send_Data(sendbuf, 41);

		cp_str_to_prev(prev_sendbuf, sendbuf, 41);
	}

}

/**
 * sync environment params
*/	
void RS485_1_Send_Data_3(void)
{
	u8 batchbuf[10] = {0xEE, 0xB5, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
	u8 t;

	RS485_1_TX_EN = 1;
	batchbuf[3] = NUM;
	NUM += 1;
	if (NUM > 6)
		NUM = 1;
	GetCRC16(batchbuf, 4, batchbuf + 4, batchbuf + 5);
	for (t = 0; t < 10; t++)
	{
		while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
			;
		USART_SendData(USART1, batchbuf[t]);
	}

	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
		;
	// RS485_RX_CNT=0;

	RS485_1_TX_EN = 0;
}
//	void RS485_3_Send_Data_2(void)
//{
//	delay_ms(50);
//	u8 sendbuf[21]={0xEE,0xB5,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFC,0xFF,0xFF};//???????????????????
//	u8 a;
//	GPIO8_Init();
//	GPIO_SetBits(GPIOB,GPIO_Pin_8);
//	RS485_3_TX_EN=1;
//	sendbuf[3]=t;
//	sendbuf[4]=total[3 + 18 * (t - 1)];
//	sendbuf[5]=total[4 + 18 * (t - 1)];
//	sendbuf[6]=chour;
//	sendbuf[7]=cminute;
//	sendbuf[8]=total[9 + 18 * (t - 1)];
//	sendbuf[9]=total[10 + 18 * (t - 1)];
//	sendbuf[10]=total[11 + 18 * (t - 1)];
//	sendbuf[11]=total[12 + 18 * (t - 1)];
//	sendbuf[12]=total[13 + 18 * (t - 1)];
//	sendbuf[13]=total[14 + 18 * (t - 1)];
//	sendbuf[14]=hot_clod_flag;
//	sendbuf[15]= humidity_flag;
//	sendbuf[16]=ALARM;
//
//
//  	for(a=0;a<21;a++)
//	{
//		while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
//		USART_SendData(USART1,sendbuf[a]);
//	}

//  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
//	RS485_RX_CNT=0;
//  GPIO_ResetBits(GPIOB,GPIO_Pin_8);
//	RS485_3_TX_EN=0;
//}

void RS485_3_Send_Data(u8 *buf,u8 len)
{
	u8 t;
	// GPIO8_Init();
	// GPIO_SetBits(GPIOB,GPIO_Pin_8);
	RS485_3_TX_EN=1;
  	for(t=0;t<len;t++)
	{
		while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
		USART_SendData(USART1,buf[t]);
	}

	while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
	// RS485_RX_CNT=0;
	//   GPIO_ResetBits(GPIOB,GPIO_Pin_8);
	RS485_3_TX_EN=0;
}

void RS485_Receive_Data(u8 *buf, u8 *len)
{
	u8 rxlen = RS485_RX_CNT;
	u8 i = 0;
	*len = 0;
	delay_ms(10);
	if (rxlen == RS485_RX_CNT && rxlen)
	{
		for (i = 0; i < rxlen; i++)
		{
			buf[i] = RS485_RX_BUF[i];
		}
		*len = RS485_RX_CNT;
		RS485_RX_CNT = 0;
	}
}
