#include "qd_u1oa_xsb.h"


const uint8_t dg_num[10]={0xfa,0xc0,0x9e,0xd6,0xe4,0x76,0x7e,0xc2,0xfe,0xf6};//ÊýÂë¹Ü×ÖÄ£

struct device_panel panel;


void Sand_Byte_to_595(uint8_t h,uint8_t l)
{
	ds_in_or_out(1);
	hc595_ds(0);
	hc595_sc(0);
	hc595_sh(0);
	for(char a=0;a<8;a++)
	{
		if((h<<a)&0x80)
		{
			hc595_ds(1);
		}else
		{
			hc595_ds(0);
		}
		hc595_sc(1);
		hc595_sc(0);
	}
	for(char a=0;a<8;a++)
	{
		if((l<<a)&0x80)
		{
			hc595_ds(1);
		}else
		{
			hc595_ds(0);
		}
		hc595_sc(1);
		hc595_sc(0);
	}	
	hc595_sh(1);
	hc595_sh(0);
}


void panel_scan_server()
{
	/*
	static uint32_t temptime=0;
	
	if(HAL_GetTick()>temptime)
	{
		temptime+=1000;
		panel.now_temp=rand()%100;
		panel.set_temp=rand()%100;
	}
	if(HAL_GetTick()&0x200)
	{
		panel.led_green=1;
		panel.led_red=0;
	}else
	{
		panel.led_green=0;
		panel.led_red=1;
	}
	*/
	if(panel.now_temp==0xff)
	{
		Sand_Byte_to_595(0xff&~dg1,0);
		Sand_Byte_to_595(0xff&~dg2,0);
	}else
	{
		Sand_Byte_to_595(0xff&~dg1,dg_num[panel.now_temp/10]);
		Sand_Byte_to_595(0xff&~dg2,dg_num[panel.now_temp%10]);
	}
	
	if(panel.set_temp==0xff)
	{
		Sand_Byte_to_595(0xff&~dg3,0);
		Sand_Byte_to_595(0xff&~dg4,0);
	}else
	{
		Sand_Byte_to_595(0xff&~dg3,dg_num[panel.set_temp/10]);
		Sand_Byte_to_595(0xff&~dg4,dg_num[panel.set_temp%10]);
	}
	
	
	uint8_t ledtemp=0;
	if(panel.led_green==1)
	{
		ledtemp|=l_green;
	}
	if(panel.led_red==1)
	{
		ledtemp|=l_red;
	}
	Sand_Byte_to_595(0xff&~l_com,ledtemp);
	
	Sand_Byte_to_595(~s1,0);
	panel.flag_s1=Read_Ds();
	Sand_Byte_to_595(~s2,0);
	panel.flag_s2=Read_Ds();
	Sand_Byte_to_595(~s3,0);
	panel.flag_s3=Read_Ds();
/****************************°´Å¥ÂË²¨*/	
	
	if(panel.flag_s1_==0&&panel.flag_s1==1)
	{
		panel.flag_s1_=1;
	}
	if(panel.flag_s1_==1&&panel.flag_s1==0)
	{
		panel.flag_s1_=0;
		panel.i_s1=0;
		panel.flag_s1_t=0;
		
	}
	if(panel.flag_s1_==1)
	{
		panel.i_s1++;
		if(panel.i_s1>=b_i)
		{
			panel.i_s1=0;
			if(panel.flag_s1==1)
			{
				panel.flag_s1_t=1;
			}
		}
		
	}
	
	
	if(panel.flag_s2_==0&&panel.flag_s2==1)
	{
		panel.flag_s2_=1;
	}
	if(panel.flag_s2_==1&&panel.flag_s2==0)
	{
		panel.flag_s2_=0;
		panel.i_s2=0;
		panel.flag_s2_t=0;
		
	}
	if(panel.flag_s2_==1)
	{
		panel.i_s2++;
		if(panel.i_s2>=b_i)
		{
			panel.i_s2=0;
			if(panel.flag_s2==1)
			{
				panel.flag_s2_t=1;
			}
		}
		
	}

	if(panel.flag_s3_==0&&panel.flag_s3==1)
	{
		panel.flag_s3_=1;
	}
	if(panel.flag_s3_==1&&panel.flag_s3==0)
	{
		panel.flag_s3_=0;
		panel.i_s3=0;
		panel.flag_s3_t=0;
		
	}
	if(panel.flag_s3_==1)
	{
		panel.i_s3++;
		if(panel.i_s3>=b_i)
		{
			panel.i_s3=0;
			if(panel.flag_s3==1)
			{
				panel.flag_s3_t=1;
			}
		}
		
	}	
}