#include <rtthread.h>
#include "flash.h"
#include "main.h"
#include "ad.h"
#include "plan.h"
#include "button.h"
#include "buffer.h"
#include "display.h"
#include "xBeeAppEscapingMode.h"

rt_sem_t thread_beep_sem = RT_NULL;

int WeightConst_JZ = 0;
uint8_t WeightConst_JZ_ = 0;
uint32_t WeightConst_JZZ = 0;    //校正称量的当前位
uint8_t WeightConst_JZZ_ = 8;         //校正称量的当前位
uint32_t WeightConst_ZD = 3000;  //最大称量值
uint8_t WeightConst_ZD_ = 8;          //最大称量的当前位

uint8_t CanButton=0;

struct button_state_t button_state = {0,0,0,0,0};

//static rt_uint8_t MenuItem__=0; //当前菜单值序号


void sendshowCC(void)
{
    XbeeConst_FRemoteTxBuf[0] = 7;
    XbeeConst_FRemoteTxBuf[1] = 0x7E;
    XbeeConst_FRemoteTxBuf[2] = 0x05;
    XbeeConst_FRemoteTxBuf[3] = XbeeConst_RemoteAddress;
    XbeeConst_FRemoteTxBuf[4] = 0xB2;
    XbeeConst_FRemoteTxBuf[5] = XbeeConst_UseAPI;
    XbeeConst_FRemoteTxBuf[6] = XbeeConst_FrameNum++;
    XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
    for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++) {
        XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
            XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];
    }
    XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];
		rt_sem_take(uart2_lock, RT_WAITING_FOREVER);			
    setSendFrame_t(0x01,0x01,0xC1);
    XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);			
    for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
        usart2_send_data[i-1] = XbeeConst_PTxBuf0[i];		

		HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
		rt_sem_release(uart2_lock);
}

static void CheckMenuButton_entry(void *parameter) {
	while(1)
	{
		if (button_state.b1>0)	button_state.b1 ++;
		if (button_state.b2>0)	button_state.b2 ++;
		if (button_state.b3>0)	button_state.b3 ++;
		if (button_state.b4>0)	button_state.b4 ++;
		if (button_state.b5>0)	button_state.b5 ++;

		if (button_state.b1>=5)	button_state.b1 = 0;
		if (button_state.b2>=5)	button_state.b2 = 0;
		if (button_state.b3>=5)	button_state.b3 = 0;
		if (button_state.b4>=5)	button_state.b4 = 0;
		if (button_state.b5>=5)	button_state.b5 = 0;


		//  if (WeightConst_TQ==14)  ad_sample=Ad_Sample1();//get_weight();
		if (WeightConst_SBType<5)
		{
				if ((HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin) > 0) && button_state.b1 == 0)	{
						beep();
						if (MenuItem==0)
						{
								if (XbeeConst_DeviceAddress>1)  XbeeConst_DeviceAddress--; else  XbeeConst_DeviceAddress = 10;
								write_Flash("CH", &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress));							
							  WeightConst_Weight_display[6] = XbeeConst_DeviceAddress + 0x30;
						}
						else if (MenuItem==1)
						{
								if (WeightConst_TQ>1)  WeightConst_TQ--;  else  WeightConst_TQ = 14;                    
								write_Flash("TQ", &WeightConst_TQ, sizeof(WeightConst_TQ));
						}
						else if (MenuItem==2)
						{
								if (XbeeConst_NetAddress>0) XbeeConst_NetAddress--;  
								else
								{
										if (WeightConst_SBType==4) XbeeConst_NetAddress=2; else XbeeConst_NetAddress=13;
								}										
								setNetAddress();
								write_Flash("WL", &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress));
						}
						else if (MenuItem==3 && (WeightConst_SBType!=4&&WeightConst_SBType!=6))
						{
								if (WeightConst_SBType>1)  WeightConst_SBType--;  else WeightConst_SBType=2;                    
								write_Flash("PM", &WeightConst_SBType, sizeof(WeightConst_SBType));
						}
						else if (MenuItem==4)
						{
								if (WeightConst_ChanCheShu>1) WeightConst_ChanCheShu--;  else WeightConst_ChanCheShu=10;
								write_Flash("CC", &WeightConst_ChanCheShu, sizeof(WeightConst_ChanCheShu));
						}
						else if (MenuItem==5)
						{
								if (XbeeConst_UseAPI>0) XbeeConst_UseAPI--; else XbeeConst_UseAPI=3;
								setAPI();
								write_Flash("API", &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI));                   
						}
						else if (MenuItem==6)
						{
								if (WeightConst_JZZ_ == 0 && WeightConst_JZZ>0)         WeightConst_JZZ--;
								else if (WeightConst_JZZ_==1&& WeightConst_JZZ>10)      WeightConst_JZZ = WeightConst_JZZ - 10;
								else if (WeightConst_JZZ_==2&& WeightConst_JZZ>100)     WeightConst_JZZ = WeightConst_JZZ - 100;
								else if (WeightConst_JZZ_==3&& WeightConst_JZZ>1000)    WeightConst_JZZ = WeightConst_JZZ - 1000;
								else if (WeightConst_JZZ_==4 && WeightConst_JZZ>10000)  WeightConst_JZZ = WeightConst_JZZ - 10000;
						}
						else if (MenuItem==7)
						{
								if (WeightConst_ZD_ == 0 & Maxval>0)         {  Maxval--; }
								else if (WeightConst_ZD_==1 & Maxval>10)     {  Maxval = Maxval - 10;    }
								else if (WeightConst_ZD_==2 & Maxval>100)    {  Maxval = Maxval - 100;   }
								else if (WeightConst_ZD_==3 & Maxval>1000)   {  Maxval = Maxval - 1000;  }
								else if (WeightConst_ZD_==4 & Maxval>10000)  {  Maxval = Maxval - 10000; }
						}
						else if (MenuItem==8)
						{									
								if (ad_FS>0)   ad_FS--;   else ad_FS=8;
								write_Flash("ad_FS", &ad_FS, sizeof(ad_FS));          
						}
						else if (MenuItem==9)
						{
								if (ad_Point>0)  ad_Point--; else  ad_Point=3;
								write_Flash("ad_Point", &ad_Point, sizeof(ad_Point));          
						}
						else if (MenuItem==10)
						{									
								if (ad_Inteval== 1) ad_Inteval=5; else if (ad_Inteval==2)  ad_Inteval=1;
								else if (ad_Inteval==5) ad_Inteval=2; else ad_Inteval=1;
								write_Flash("ad_Inteval", &ad_Inteval, sizeof(ad_Inteval));       
						}
						else if (MenuItem==11)
						{									
								if (WeightConst_BLE>0) WeightConst_BLE --;
								else WeightConst_BLE = 3;
								write_Flash("BLE", &WeightConst_BLE, sizeof(WeightConst_BLE)); 
								//----------------------------setBT();
						}
						button_state.b1 = 1;
						button_state.b2 = 0;
						button_state.b3 = 0;
						button_state.b4 = 0;
						button_state.b5 = 0;
				}

				else if ((HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)) && (WeightConst_SBType != 4&&WeightConst_SBType != 6) && 	button_state.b2 == 0) {
						beep();
						if (MenuItem==0)
						{
								if (XbeeConst_DeviceAddress<10)	XbeeConst_DeviceAddress++; else XbeeConst_DeviceAddress = 1 ;
								write_Flash("CH", &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress));
						}
						else if (MenuItem==1)
						{
								if (WeightConst_TQ<14)	WeightConst_TQ++; else WeightConst_TQ = 1 ;
								write_Flash("TQ", &WeightConst_TQ, sizeof(WeightConst_TQ));

						}
						else if (MenuItem==2)
						{
								if (XbeeConst_NetAddress<13 && (WeightConst_SBType!=4&&WeightConst_SBType!=6))
										XbeeConst_NetAddress++;
								else if (XbeeConst_NetAddress<2 && (WeightConst_SBType==4))
										XbeeConst_NetAddress++;
								else
										XbeeConst_NetAddress=0;

								setNetAddress();
								write_Flash("WL", &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress));
						}
						else if (MenuItem==3)
						{
								if (WeightConst_SBType<2 && (WeightConst_SBType!=4&&WeightConst_SBType!=6))
										WeightConst_SBType++;              
								write_Flash("PM", &WeightConst_SBType, sizeof(WeightConst_SBType));
						}
						else if (MenuItem==4)
						{
								if (WeightConst_ChanCheShu<10)	WeightConst_ChanCheShu++;
								else	WeightConst_ChanCheShu=1;
								write_Flash("CC", &WeightConst_ChanCheShu, sizeof(WeightConst_ChanCheShu));

								WeightConst_Weight_display[6] = WeightConst_ChanCheShu + 0x30;
						}
						else if (MenuItem==5)
						{
								if (XbeeConst_UseAPI<3)	XbeeConst_UseAPI++;	else	XbeeConst_UseAPI=0;
								setAPI();
								write_Flash("API", &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI));       
						}
						else if (MenuItem==6)
						{
								if (WeightConst_JZZ_ == 0)	  WeightConst_JZZ++;
								else if (WeightConst_JZZ_==1)	WeightConst_JZZ = WeightConst_JZZ + 10;
								else if (WeightConst_JZZ_==2)	WeightConst_JZZ = WeightConst_JZZ + 100;
								else if (WeightConst_JZZ_==3)	WeightConst_JZZ = WeightConst_JZZ + 1000;
								else if (WeightConst_JZZ_==4)	WeightConst_JZZ = WeightConst_JZZ + 10000;
						}
						else if (MenuItem==7)
						{
								if (WeightConst_ZD_ == 0)	    Maxval++;
								else if (WeightConst_ZD_==1)  Maxval = Maxval + 10;
								else if (WeightConst_ZD_==2)	Maxval = Maxval + 100;
								else if (WeightConst_ZD_==3)	Maxval = Maxval + 1000;
								else if (WeightConst_ZD_==4)	Maxval = Maxval + 10000;
						}
						else if (MenuItem==8)
						{
								if (ad_FS<8)	ad_FS++;		else	ad_FS=0;
								write_Flash("ad_FS", &ad_FS, sizeof(ad_FS));        
						}
						else if (MenuItem==9)
						{
								if (ad_Point<3)	ad_Point++;	else	ad_Point=0;
								write_Flash("ad_Point", &ad_Point, sizeof(ad_Point));        
						}
						else if (MenuItem==10)
						{
							  if (ad_Inteval== 1) ad_Inteval=2; else if (ad_Inteval==2)  ad_Inteval=5;
								else if (ad_Inteval==5) ad_Inteval=1; else ad_Inteval=1;
								write_Flash("ad_Inteval", &ad_Inteval, sizeof(ad_Inteval));   
						}
						else if (MenuItem==11)
						{
								if (WeightConst_BLE<3)	WeightConst_BLE ++;
								else WeightConst_BLE = 0;
								write_Flash("BLE", &WeightConst_BLE, sizeof(WeightConst_BLE)); 
						}
						button_state.b1 = 0;
						button_state.b2 = 1;
						button_state.b3 = 0;
						button_state.b4 = 0;
						button_state.b5 = 0;

				}
				else if ((HAL_GPIO_ReadPin(B3_GPIO_Port, B3_Pin) > 0) && 	button_state.b3 == 0) {

						if (MenuItem==6)
						{
								if (WeightConst_JZZ_==4)	WeightConst_JZZ_ = 0;
								else WeightConst_JZZ_++;
								beep();
						}
						else if (MenuItem==7)
						{
								if (WeightConst_ZD_==4)	WeightConst_ZD_ = 0;
								else WeightConst_ZD_++;
								beep();
						}

						button_state.b1 = 0;
						button_state.b2 = 0;
						button_state.b3 = 1;
						button_state.b4 = 0;
						button_state.b5 = 0;
				}

				else if ((HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0) && (WeightConst_SBType == 4) && 	button_state.b4 == 0) {
						if (WeightConst_TQ<14)
								if (MenuItem==5)	MenuItem = 11;
						    else if (MenuItem==11)	MenuItem = 0;
								else MenuItem++;
						else if (MenuItem==11)	MenuItem = 0;
						else MenuItem++;

						if (MenuItem==6) WeightConst_JZZ_ = 0;  //切换菜单后重置
						else if (MenuItem==7) WeightConst_ZD_ = 0;
						if (MenuItem==7)
						{
        				if (WeightConst_JZZ>0) setFullCal(WeightConst_JZZ);
						}
	          else if (MenuItem==8)  ArgSave();
						beep();
						button_state.b1 = 0;
						button_state.b2 = 0;
						button_state.b3 = 0;
						button_state.b4 = 1;
						button_state.b5 = 0;
				}

				else if ((HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0) && (WeightConst_SBType != 4 && WeightConst_SBType != 6) && 	button_state.b4 == 0) {
						if (WeightConst_TQ < 14)
						{
								if (MenuItem == 5)	MenuItem = 11;
								else if (MenuItem == 11)	MenuItem = 0;
								else MenuItem++;
						}
						else if (MenuItem==11)	MenuItem = 0;
						else MenuItem++;

						if (MenuItem == 6) WeightConst_JZZ_ = 0;  //切换菜单后重置
						else if (MenuItem == 7) WeightConst_ZD_ = 0;
						if (MenuItem==7)		setFullCal(WeightConst_JZZ);
						else if (MenuItem==8)					ArgSave();
						beep();
						button_state.b1 = 0;
						button_state.b2 = 0;
						button_state.b3 = 0;
						button_state.b4 = 1;
						button_state.b5 = 0;
				}
		}
		rt_thread_mdelay(200);
	}
}

#define THREAD_PRIORITY         3
#define THREAD_TIMESLICE        5
static  rt_thread_t tid1 = RT_NULL;

void MenuButtonthread(void)
{
    tid1 = rt_thread_create("CheckMB",
                            CheckMenuButton_entry, RT_NULL,
                            0x400,
                            THREAD_PRIORITY, THREAD_TIMESLICE);

    /* 如果获得线程控制块，启动这个线程 */
    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);
}

static void beep_off(void)
{
	HAL_GPIO_WritePin(beep_GPIO_Port, beep_Pin, GPIO_PIN_RESET);
}

static void beep_on(void)
{
	HAL_GPIO_WritePin(beep_GPIO_Port, beep_Pin, GPIO_PIN_SET);
}

void alarm_off(void)
{
	HAL_GPIO_WritePin(alarm_GPIO_Port, alarm_Pin, GPIO_PIN_RESET);
}

void alarm_on(void)
{
	HAL_GPIO_WritePin(alarm_GPIO_Port, alarm_Pin, GPIO_PIN_SET);
}

void beep(void)
{
	rt_sem_release(thread_beep_sem); 
}

static void thread_beep_entry(void *parameter)
{	
	 while(1)
	 {
			 rt_sem_take(thread_beep_sem, RT_WAITING_FOREVER);   //等待串口消息
       beep_on();
		   rt_thread_mdelay(400);
		   beep_off();
   }
}

static  rt_thread_t tid_beep = RT_NULL;

void thread_beep_init(void)
{
	  thread_beep_sem = rt_sem_create("thread_beep_sem", 0, RT_IPC_FLAG_FIFO);
    tid_beep = rt_thread_create("beep",
                            thread_beep_entry, RT_NULL,
                            0x100,
                            THREAD_PRIORITY, THREAD_TIMESLICE);

    /* 如果获得线程控制块，启动这个线程 */
    if (tid_beep != RT_NULL)
        rt_thread_startup(tid_beep);
}


static unsigned int button4_press=0;
static unsigned int button4_press_=0;
static void CheckButton_entry(void *parameter) {
    while (1) {
        button4_press_ = 200;

        if (WeightConst_SBType == 2 || WeightConst_SBType == 7)
        {
            uart4_send_data[18]=0;
            for (int i = 2; i <18; i++)   //校验和
                uart4_send_data[18]=uart4_send_data[18]+uart4_send_data[i];
						rt_sem_take(uart4_lock, RT_WAITING_FOREVER);
					  HAL_UART_Transmit_DMA(&huart4, uart4_send_data, 19);
						rt_sem_release(uart4_lock);
        }
        if (button_state.b1>0)	button_state.b1 ++;
				if (button_state.b2>0)	button_state.b2 ++;
				if (button_state.b3>0)	button_state.b3 ++;
				if (button_state.b4>0)	button_state.b4 ++;

				if (button_state.b1>=15)	button_state.b1 = 0;
				if (button_state.b2>=15)	button_state.b2 = 0;
				if (button_state.b3>=5)	button_state.b3 = 0;
				if (button_state.b4>=5)	button_state.b4 = 0;

        if ((HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin) > 0) && button_state.b1 == 0 && CanButton==1)	{
            if (WeightConst_SBType != 2 && WeightConst_SBType != 7) {
                if (pf_comp.data.pfsort!=0xFE) {
                    if (WeightConst_DELAYON)
                    {
                      //  beep();
                      //  WeightConst_DELAYTIME = (pf_comp.data.delaytime*60)+1;
                    }
                    else if (pf_comp.data.needBegin==1 && WeightConst_WeightHaveBegin==0)
                    {
                        beep();
												rt_sem_take(weight_lock, RT_WAITING_FOREVER);
                        WeightConst_WeightBegin = WeightConst_WeightCur;
												rt_sem_release(weight_lock);
											  CompleteTarget_order = pf_comp.data.pfsort;
											  write_Flash("completeorder", &CompleteTarget_order, sizeof(CompleteTarget_order));	
											  write_Flash("begintime", &TimeConst, sizeof(TimeConst));											
                        write_Flash("beginweight", &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin)); 
                        getTargetWeight();
                    }
                    else if (tsdb_recordcount('p')>0 && delayKeying == 0 && WeightConst_Product_Run)
						      	{			
												rt_sem_release(delay_key_sem); 													
                        sendkey1(0xF2, 0);
										}
                }
            }
            else
            {
                beep();
                ChancheShow();
                XbeeConst_FRemoteTxBuf[0] = 7;
                XbeeConst_FRemoteTxBuf[1] = 0x7E;
                XbeeConst_FRemoteTxBuf[2] = 0x05;
                if (isCC)
                    XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_DeviceAddress&0x0F) << 4 ) |(XbeeConst_RemoteAddress&0xf);
                else
                    XbeeConst_FRemoteTxBuf[3] = XbeeConst_RemoteAddress&0xf;
                if (XbeeConst_UseAPI==1||XbeeConst_UseAPI==3)
                    XbeeConst_FRemoteTxBuf[4] = 0xA2;
                else
                    XbeeConst_FRemoteTxBuf[4] = 0x14;
                XbeeConst_FRemoteTxBuf[5] = 0x01;
                XbeeConst_FRemoteTxBuf[6] = XbeeConst_FrameNum++;;
                XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
                for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++) {
                    XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
                        XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];
                }
                XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];

                if (XbeeConst_UseAPI>0) {
										rt_sem_take(uart2_lock, RT_WAITING_FOREVER);						
                    setSendFrame_t(0x01,0x01,0xC1);
                    XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);		
                    for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
                        usart2_send_data[i-1] = XbeeConst_PTxBuf0[i];	
  									HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
										rt_sem_release(uart2_lock);
                }
                else	{
									  rt_sem_take(uart3_lock, RT_WAITING_FOREVER);
                    for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
                        usart3_send_data[i-1] = XbeeConst_FRemoteTxBuf[i];
										HAL_UART_Transmit_DMA(&huart3, usart3_send_data, XbeeConst_FRemoteTxBuf[0]);
										rt_sem_release(uart3_lock);
                }
            }
						button_state.b1 = 1;
						button_state.b2 = 0;
						button_state.b3 = 0;
						button_state.b4 = 0;
        }

        if ((HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0) && button_state.b2 == 0 && CanButton==1) {
					  if (((WeightConst_SBType != 2 && WeightConst_SBType != 7)))
            {   
							if(!WeightConst_DELAYON)
                {
                    if (tsdb_recordcount('k')==0)
                        sendkey2(0xF4);
                }
            }
            else {
                beep();
                ChancheShow();
                XbeeConst_FRemoteTxBuf[0] = 7;
                XbeeConst_FRemoteTxBuf[1] = 0x7E;
                XbeeConst_FRemoteTxBuf[2] = 0x05;
                if (isCC)
                    XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_DeviceAddress&0x0F) << 4 ) | (XbeeConst_RemoteAddress&0xf);
                else
                    XbeeConst_FRemoteTxBuf[3] = XbeeConst_RemoteAddress&0xf;

                if (XbeeConst_UseAPI==1||XbeeConst_UseAPI==3)
                    XbeeConst_FRemoteTxBuf[4] = 0xA2;
                else
                    XbeeConst_FRemoteTxBuf[4] = 0x14;
                XbeeConst_FRemoteTxBuf[5] = 0x02;
                XbeeConst_FRemoteTxBuf[6] = XbeeConst_FrameNum++;
                XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
                for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++) {
                    XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
                        XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];
                }
                XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];

                if (XbeeConst_UseAPI>0) {									
										rt_sem_take(uart2_lock, RT_WAITING_FOREVER);	
                    setSendFrame_t(0x01,0x01,0xC1);
                    XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);
                    for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
                        usart2_send_data[i-1] = XbeeConst_PTxBuf0[i];					
										HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
										rt_sem_release(uart2_lock);
                }
                else
										rt_sem_take(uart3_lock, RT_WAITING_FOREVER);
                    for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
                        usart3_send_data[i-1] = XbeeConst_FRemoteTxBuf[i];
										HAL_UART_Transmit_DMA(&huart3, usart3_send_data, XbeeConst_FRemoteTxBuf[0]);
										rt_sem_release(uart3_lock);
            }

            button_state.b1 = 0;
						button_state.b2 = 1;
						button_state.b3 = 0;
						button_state.b4 = 0;
        }

        if ((HAL_GPIO_ReadPin(B3_GPIO_Port, B3_Pin) > 0) && button_state.b3 == 0) {
            if (WeightConst_SBType == 2 || WeightConst_SBType == 7)
            {
                if (XbeeConst_RemoteAddress == 1)  XbeeConst_RemoteAddress = WeightConst_ChanCheShu;
                else XbeeConst_RemoteAddress--;

                //	if ((WeightConst_ChanCheShu==7) && XbeeConst_RemoteAddress>2 && XbeeConst_RemoteAddress<5)
                //		setNetAddress_byapi(1);
                //	else setNetAddress_byapi(0);
                ChancheShow();
                if (XbeeConst_UseAPI==3) sendshowCC();
                beep();
            }
             else {             					
								if (HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin) > 0)
								{
									if(WeightConst_TQ==14)
									{
											if (button_state.b4>0) button4_press = button4_press + button4_press_;
											else button4_press=1;
									}
									if(button4_press>1500){
										beep();
										button4_press = 0;
										setZero();
										setZeroCal();
									}
								}                
                else
                {
									  beep();
									  rt_thread_mdelay(1000);
									  if (HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin) > 0 && WeightConst_SBType == 1 && WeightConst_Product_Run==1){
											  rt_sem_take(display_lock, RT_WAITING_FOREVER);
												for (int i = 0; i <40; i++)
													 WeightConst_feed[i] = WeightConst_product[i];   //显示一秒牛舍
												rt_sem_release(display_lock);
												while(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin) > 0){														
													  rt_thread_mdelay(200);	
												}
												
												rt_sem_take(display_lock, RT_WAITING_FOREVER);
												rt_memset(WeightConst_feed,0x20,40);													
										    for (int i = 0; i <= WeightConst_PlantempList[1]-11; i++) {
												  WeightConst_feed[i] = WeightConst_PlantempList[i+4];  //饲料名称
												}
												rt_sem_release(display_lock);
									  }									  
										else
										{											
											if (WeightConst_displayTotal == 1) WeightConst_displayTotal = 0;
											else WeightConst_displayTotal = 1;
										}
                }
            }
            button_state.b1 = 0;
						button_state.b2 = 0;
						button_state.b3 = 1;
						button_state.b4 = 0;
        }
        if ((HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0) && button_state.b4 == 0 ) {
            if (WeightConst_SBType != 2 && WeightConst_SBType != 7) {
//							  if (isTag==1)
//								{
//									 if (WriteTag_state <2 && pf_comp.data.needBegin == 1 && WeightConst_WeightHaveBegin==0)
//									 {
//										 beep();
//										 WriteTag();										 
//									 }
//								}
                if (WeightConst_SBType==6)
                {
                    if (WeightConst_remoteDisplay==0)
                        WeightConst_remoteDisplay = 1;
                    else
                        WeightConst_remoteDisplay = 0;
                    beep();
                }
								
            }
            else
            {
                if (XbeeConst_RemoteAddress==WeightConst_ChanCheShu)
                    XbeeConst_RemoteAddress = 1;
                else
                    XbeeConst_RemoteAddress++;

                //	if ((WeightConst_ChanCheShu==7)  && XbeeConst_RemoteAddress>2 && XbeeConst_RemoteAddress<5)
                //		setNetAddress_byapi(1);
                //	else setNetAddress_byapi(0);

                ChancheShow();
                if (XbeeConst_UseAPI==3) sendshowCC();
                beep();
            }
            button_state.b1 = 0;
						button_state.b2 = 0;
						button_state.b3 = 0;
						button_state.b4 = 1;
        }
        else if (button_state.b4==0)
            button4_press = 0;
				rt_thread_mdelay(200);
    }
}

void Button_thread(void)
{
    tid1 = rt_thread_create("Button",
                            CheckButton_entry, RT_NULL,
                            0x400,
                            3, 5);

    /* 如果获得线程控制块，启动这个线程 */
    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);
}
