/******************/
/**中弘自定义协议**/
/******************/
#include "ZH_protocol.h"
#include "key.h"
static void (*ZH_send_uart)(uint8_t *buf, uint8_t len) = NULL;


static uint8_t CRC_check(uint8_t *data, uint8_t len)
{
		int i = 0;
	
		uint8_t crc_result = 0;
	
		for(i = 0; i < len; i++)
		{
				crc_result += *(data++);
		}
		
		return crc_result;
}

static void install_send_buf(void)
{
		ZH_protocol_i->send_buf[NET_ADDR]       = 0x01;
		ZH_protocol_i->send_buf[SWITCH]         = 0xFF;
		ZH_protocol_i->send_buf[MODE]           = 0xFF;
		ZH_protocol_i->send_buf[TEMPERATURE]    = 0xFF;
	  ZH_protocol_i->send_buf[WIND_SPEED]     = 0xFF;
	  ZH_protocol_i->send_buf[WIND_DIRECTION] = 0xFF;
	  ZH_protocol_i->send_buf[ROOM_TEMP]      = 0xFF;
	  ZH_protocol_i->send_buf[MALFUNCTION]    = 0xFF;
	  ZH_protocol_i->send_buf[STATE]          = 0xFF;
	  ZH_protocol_i->send_buf[CRC]            = 0xFF;
}

static void send_on_off(uint8_t state)
{
		install_send_buf();
	
		ZH_protocol_i->send_buf[SWITCH] = state; 
	
		ZH_protocol_i->ops_type         = SWITCH;
	
		ZH_protocol_i->send_buf[CRC]    = CRC_check(ZH_protocol_i->send_buf, 9);
	
		ZH_send_uart(ZH_protocol_i->send_buf, FRAME_LEN);
}

static void send_mode(uint8_t state)
{
		install_send_buf();
	
		ZH_protocol_i->send_buf[MODE] = state;
	
		ZH_protocol_i->ops_type       = MODE;
	
		ZH_protocol_i->send_buf[CRC]  = CRC_check(ZH_protocol_i->send_buf, 9);
	
		ZH_send_uart(ZH_protocol_i->send_buf, FRAME_LEN);
}

static void send_wind_speed(uint8_t state)
{
		install_send_buf();
	
		ZH_protocol_i->send_buf[WIND_SPEED] = state;
	
		ZH_protocol_i->ops_type             = WIND_SPEED;
	
		ZH_protocol_i->send_buf[CRC]        = CRC_check(ZH_protocol_i->send_buf, 9);
	
		ZH_send_uart(ZH_protocol_i->send_buf, FRAME_LEN);
}
static void send_wind_direct(uint8_t state)
{
		install_send_buf();
	
		ZH_protocol_i->send_buf[WIND_DIRECTION] = state;
	
		ZH_protocol_i->ops_type             = WIND_DIRECTION;
	
		ZH_protocol_i->send_buf[CRC]        = CRC_check(ZH_protocol_i->send_buf, 9);
	
		ZH_send_uart(ZH_protocol_i->send_buf, FRAME_LEN);
}
static void send_temperature(uint8_t state)
{
		install_send_buf();
	
		ZH_protocol_i->send_buf[TEMPERATURE] = state;
	
		ZH_protocol_i->ops_type              = TEMPERATURE;
	
		ZH_protocol_i->send_buf[CRC]         = CRC_check(ZH_protocol_i->send_buf, 9);
	
		ZH_send_uart(ZH_protocol_i->send_buf, FRAME_LEN);
}

static void send_poll_check(void)
{
		static uint8_t data[FRAME_LEN] = {0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
		
		data[9] = CRC_check(data, 9);
	
		ZH_send_uart(data, FRAME_LEN);
}

static void send_swicth_temperature_wind_mode(uint8_t On_OFF, uint8_t temperature, uint8_t windspeed,uint8_t mode,uint8_t wind_direct)
{
            install_send_buf();
	/*作者:wmj  此处若温度数据过大，则转换时数据并非赋予的数，
	     则当温度位表示其他意思时，则不能与温度的赋值相同*/
	    if(temperature==0xFF)
		{
			 ZH_protocol_i->send_buf[TEMPERATURE]  = 0xFF;
		}
		else
		{
		     ZH_protocol_i->send_buf[TEMPERATURE]  = temperature;//仿旋钮触发温度同步
		}
	      
		switch(windspeed)
		{
			case WIND_SPEED_AUTO: 		
				ZH_protocol_i->send_buf[WIND_SPEED]  = AUTOMATIC;
			break;
			
			case WIND_SPEED_LOW:
				ZH_protocol_i->send_buf[WIND_SPEED]  = LOW_SPEED;
			break;
			
			case WIND_SPEED_MID:
				ZH_protocol_i->send_buf[WIND_SPEED]  = INTERMEDIATE_SPEED;
			break;
			
			case WIND_SPEED_HIGH:
					ZH_protocol_i->send_buf[WIND_SPEED]  = HIGH_SPEED;
			break;
			
			default : 
				//ZH_protocol_i->send_buf[WIND_SPEED]  = AUTOMATIC;
			break;
		}
		
        ZH_protocol_i->send_buf[SWITCH]   = On_OFF;

		if(mode == SYSTEM_MODE_COOLING)
		{
				ZH_protocol_i->send_buf[MODE]  = REFRIGERATION;
		}
		else if(mode == SYSTEM_MODE_HEATING)
		{
				ZH_protocol_i->send_buf[MODE]  = HEATINF;
		}
		else if(mode == SYSTEM_MODE_DRY)
		{
				ZH_protocol_i->send_buf[MODE]  = XERANSIS;
		}
		else if(mode == SYSTEM_MODE_AIR)
		{
				ZH_protocol_i->send_buf[MODE]  = SUPPLY_AIR;
		}
		
		if(wind_direct == WIND_DIRECT_DOWN)
		{
			ZH_protocol_i->send_buf[WIND_DIRECTION]  = ANGLE_30;
		}
		else if(wind_direct == WIND_DIRECT_MIDDLE)
		{
			ZH_protocol_i->send_buf[WIND_DIRECTION]  = ANGLE_45;
		}
		else if(wind_direct == WIND_DIRECT_UP)
		{
			ZH_protocol_i->send_buf[WIND_DIRECTION]  = ANGLE_60;
		}
		
		ZH_protocol_i->send_buf[CRC]         = CRC_check(ZH_protocol_i->send_buf, 9);
		ZH_send_uart(ZH_protocol_i->send_buf, FRAME_LEN);
}

static void ZH_send(enum ops_type_e type, uint8_t state)
{
		switch((uint8_t)type)
		{
			case SWITCH:
				
				send_on_off(state);
				
				break;
				
			case MODE:
				
				send_mode(state);
				
				break;
				
			case WIND_SPEED:
				
				send_wind_speed(state);
			  
				break;
			
			case TEMPERATURE:
				
				send_temperature(state);
			  
				break;
			case WIND_DIRECTION:
				
				send_wind_direct(state);
			  
				break;
			case POLL_CHECK:
				
				send_poll_check();
			  
				break;
			
			default: break;
		}
}

static void ZH_receive(uint8_t re_data)
{
		int i = 0;
		int j = 0;
	
		static uint8_t re_len = 0;
		static uint16_t crc_receive_sum = 0;
		
		for(i = 0; i < BUF_COUNT; i++)
		{
				if(ZH_protocol_i->buf_handle_flag[i] == 0)
				{
						if(re_len == 0 && re_data == 0x01)
						{
								ZH_protocol_i->receive_buf[i][NET_ADDR] = re_data;
								
								re_len++;
							
								crc_receive_sum += re_data;
						}
						else if(re_len > 0 && re_len < 9)
						{
								ZH_protocol_i->receive_buf[i][re_len++] = re_data;
							
								crc_receive_sum += re_data;
						}
						else
						{
								if((re_len == 9) && (re_data == (crc_receive_sum & 0x00FF)))
								{
										ZH_protocol_i->receive_buf[i][CRC] = re_data;
									
										ZH_protocol_i->buf_handle_flag[i]  = 1;
									
										for(j = 0; j < FRAME_LEN; j++)
										{
												if(ZH_protocol_i->receive_buf[i][j] != ZH_protocol_i->poll_check_receive[j])
												{
														ZH_protocol_i->poll_check_receive[j] = ZH_protocol_i->receive_buf[i][j];
												}
										}
								}
								
								re_len = 0;
								crc_receive_sum    = 0;
						}
						
						break;
				}
		}
}

void ZH_uart_init(void (*send_uart)(uint8_t *buf, uint8_t len))
{
		ZH_send_uart = send_uart;
}


struct ZH_protocol_t ZH_protocol = 
{
		3,
		{0},
	  {0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
		{0},
		{0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0xFF,0xFF},
		
		NET_ADDR,
		
		ZH_uart_init,
		ZH_send,
		ZH_receive,
		send_swicth_temperature_wind_mode
};

struct ZH_protocol_t * ZH_protocol_i = &ZH_protocol;

