#include <rtthread.h>
#include "main.h"
#include "flash.h"
#include "display.h"
#include "buffer.h"
#include "button.h"
#include "usart.h"
#include <stdlib.h>
#include "weight.h"
#include "plan.h"
#include "daScreen.h"
#include "xBeeAppEscapingMode.h"

rt_sem_t display_lock = RT_NULL;
struct rt_event display_event;

uint8_t MenuItem=0; //当前菜单
uint8_t WeightConst_Feed_display[40];   //显示标题
uint8_t WeightConst_Weight_display[8] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};   //显示重量
uint8_t WeightConst_Weight_display_old[8] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};   //显示重量

static signed long WeightConst_Weight_displayW;   //显示重量
unsigned char WeightConst_displayTotal = 0;  //0差重，1实重
unsigned char WeightConst_remoteDisplay = 0;
unsigned char RemoteSendDisplay = 1;

const uint8_t MenuItemList[12][8] = {{0xC9,0xE8,0xB1,0xB8,0xB5,0xD8,0xD6,0xB7}, //设备地址
    {0xCC,0xE1,0xC8,0xA1,0xB7,0xBD,0xCA,0xBD}, //提取方式
    {0xCD,0xF8,0xC2,0xE7,0xB1,0xE0,0xBA,0xC5}, //网络编号
    {0xC9,0xE8,0xB1,0xB8,0xC0,0xE0,0xD0,0xCD}, //设备类型
    {0x54,0x4D,0x52,0xCA,0xFD,0xC1,0xBF,0x20}, //铲车数量
    {0x41,0x50,0x49,0x20,0x20,0x20,0x20,0x20}, //API

    {0xD0,0xA3,0xD5,0xFD,0xD6,0xD8,0xC1,0xBF}, //校正重量
    {0xD7,0xEE,0xB4,0xF3,0xB3,0xC6,0xC1,0xBF}, //最大称量
    {0xC2,0xCB,0xB2,0xA8,0xC9,0xEE,0xB6,0xC8}, //滤波深度
    {0xD0,0xA1,0xCA,0xFD,0xB5,0xE3,0x20,0x20}, //小数点
    {0xB7,0xD6,0xB6,0xC8,0xD6,0xB5,0x20,0x20}, //分度值
    {0xB8,0xF4,0xC0,0xEB,0xD6,0xD8,0xC1,0xBF}  //重量隔离
};

const uint8_t MenuTQTypeList[15][8] = {
    {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20},
    {0x4E,0x45,0x57,0x4C,0x61,0x62,0x65,0x6C}, //NewLabel
    {0x5A,0x4D,0x33,0x30,0x33,0x20,0x20,0x20}, //AWT_ZM303
    {0x58,0x4B,0x33,0x31,0x39,0x30,0x20,0x20}, //XK3190_A27E()
    {0x54,0x4D,0x52,0x20,0x32,0x20,0x20,0x20}, //TMR2
    {0x54,0x4D,0x52,0x20,0x33,0x20,0x20,0x20}, //TMR3
    {0x45,0x5A,0x33,0x34,0x30,0x30,0x20,0x20}, //EZ3400
    {0x41,0x44,0x32,0x35,0x30,0x20,0x20,0x20}, //AD250  TQ
    {0x50,0x54,0x4D,0x20,0x20,0x20,0x20,0x20}, //PTM
    {0x54,0x2D,0x32,0x30,0x30,0x30,0x20,0x20}, //T_2000
    {0x53,0x4C,0x31,0x30,0x30,0x30,0x30,0x20}, //XL10000
    {0x58,0x4B,0x33,0x31,0x39,0x30,0x43,0x38}, //P_XK3190_C8();
    {0x54,0x44,0x49,0x32,0x30,0x30,0x49,0x20}, //TDI200I
    {0x49,0x44,0x35,0x31,0x31,0x20,0x20,0x20}, //ID511;
    {0xD7,0xD4,0xB6,0xAF,0x20,0x20,0x20,0x20} //自动
};

const uint8_t MenuSBTypeList[5][8] = {
    {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20},
    {0xBF,0xD8,0xD6,0xC6,0xC6,0xF7,0x20,0x20}, //控制器
    {0xD4,0xB6,0xB3,0xCC,0xCF,0xD4,0xCA,0xBE}, //远程显示
    {0xB4,0xF3,0xC6,0xC1,0x20,0x20,0x20,0x20},  //大屏
    {0xB4,0xF3,0xC6,0xC1,0x31,0x20,0x20,0x20}  //LED大屏
	};
static uint8_t TDisplay = 1;
	
static void MenuDisplay_entry(void *parameter) {
	while(1)
	{
		if (MenuItem<11)
				for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示重量
						WeightConst_Weight_display[SendDisplay_i]=0x20;
		if (MenuItem==0)
		{
		  	if (XbeeConst_DeviceAddress>9) {
					WeightConst_Weight_display[6] = (XbeeConst_DeviceAddress/10)%10 + 0x30;
					WeightConst_Weight_display[5] = XbeeConst_DeviceAddress%10 + 0x30;
				}
			  else WeightConst_Weight_display[6] = XbeeConst_DeviceAddress + 0x30;
		}
		else if (MenuItem==1)
		{
				for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示标题
						WeightConst_Weight_display[SendDisplay_i] = MenuTQTypeList[WeightConst_TQ][7-SendDisplay_i];
		}
		else if (MenuItem==2)
		{
				if (XbeeConst_NetAddress>99) WeightConst_Weight_display[7] = XbeeConst_NetAddress/100 + 0x30;
				if (XbeeConst_NetAddress>9) WeightConst_Weight_display[6] = (XbeeConst_NetAddress/10)%10 + 0x30;
				WeightConst_Weight_display[5] = XbeeConst_NetAddress%10 + 0x30;
		}
		else if (MenuItem==3)
		{
				for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示标题
						WeightConst_Weight_display[SendDisplay_i] = MenuSBTypeList[WeightConst_SBType][7-SendDisplay_i];
		}
		else if (MenuItem==4)
		{
			if (WeightConst_ChanCheShu>9) {
					WeightConst_Weight_display[6] = (WeightConst_ChanCheShu/10)%10 + 0x30;
					WeightConst_Weight_display[5] = WeightConst_ChanCheShu%10 + 0x30;
			}
			else WeightConst_Weight_display[6] = WeightConst_ChanCheShu + 0x30;
		}
		else if (MenuItem==5)
		{
				WeightConst_Weight_display[6] = XbeeConst_UseAPI + 0x30;
		}
		else if (MenuItem==6)
		{   TDisplay++;
				WeightConst_Weight_display[4] = (WeightConst_JZZ_==4 && TDisplay%2? 0x20:(WeightConst_JZZ>9999 ? (WeightConst_JZZ/10000)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[3] = (WeightConst_JZZ_==3 && TDisplay%2? 0x20:(WeightConst_JZZ>999 ? (WeightConst_JZZ/1000)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[2] = (WeightConst_JZZ_==2 && TDisplay%2? 0x20:(WeightConst_JZZ>99 ? (WeightConst_JZZ/100)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[1] = (WeightConst_JZZ_==1 && TDisplay%2? 0x20:(WeightConst_JZZ>9 ? (WeightConst_JZZ/10)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[0] = (WeightConst_JZZ_==0 && TDisplay%2? 0x20:(WeightConst_JZZ%10 + 0x30));
		}
		else if (MenuItem==7)
		{   TDisplay++;
				WeightConst_Weight_display[4] = (WeightConst_ZD_==4 && TDisplay%2? 0x20:(Maxval>9999 ? (Maxval/10000)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[3] = (WeightConst_ZD_==3 && TDisplay%2? 0x20:(Maxval>999 ? (Maxval/1000)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[2] = (WeightConst_ZD_==2 && TDisplay%2? 0x20:(Maxval>99 ? (Maxval/100)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[1] = (WeightConst_ZD_==1 && TDisplay%2? 0x20:(Maxval>9 ? (Maxval/10)%10 : 0x00) + 0x30);
				WeightConst_Weight_display[0] = (WeightConst_ZD_==0 && TDisplay%2? 0x20:(Maxval%10 + 0x30));
		}
		else if (MenuItem==8)
		{
				WeightConst_Weight_display[6] = ad_FS + 0x30;
		}
		else if (MenuItem==9)
		{
				WeightConst_Weight_display[6] = ad_Point + 0x30;
		}
		else if (MenuItem==10)
		{
				WeightConst_Weight_display[6] = ad_Inteval + 0x30;
		}
		else if (MenuItem==11)
		{
				WeightConst_Weight_display[6] = WeightConst_BLE + 0x30;
		}

		if (WeightConst_SBType<3 || WeightConst_SBType==6) {

				uart4_send_data[0] = 0x7E;
				uart4_send_data[1] = 0x45;

				for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示标题
						uart4_send_data[2 + SendDisplay_i] =  MenuItemList[MenuItem][SendDisplay_i];

				for (int SendDisplay_i = 7; SendDisplay_i >=0; SendDisplay_i--)   //显示重量
						uart4_send_data[10 + SendDisplay_i] =  WeightConst_Weight_display[7-SendDisplay_i];
				// USART_Send_DMA(displaySerial,18);
				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);

		}
		else if (WeightConst_SBType==3||WeightConst_SBType==7) {
				
				for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示菜单标题
				{
						WeightConst_feed[ SendDisplay_i] =  MenuItemList[MenuItem][SendDisplay_i];
				}
				for (int SendDisplay_i = 7; SendDisplay_i >=0; SendDisplay_i--)   //显示当前值
				{
						WeightConst_feed[8 + SendDisplay_i] =  WeightConst_Weight_display[7-SendDisplay_i];
				}
				//----------------------------
//				get_msg(0,//Xstart
//								12,//Ystart
//								2,//size
//								0,//reset
//								0,//speed
//								16,//字符个数
//								3,//界面数
//								0,//id
//								0xbF,//Xend
//								0,
//								&WeightConst_feed[0]//字码
//							 );
		}
		else {
				for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示标题
						WeightConst_feed[SendDisplay_i] =  MenuItemList[MenuItem][SendDisplay_i];
			//----------------------------
				//display_menu();
				if(isn!=1) ;//display_Now();
				else {
						display_Current_noweight_();
						display_Now_(WeightConst_WeightCurTem);
				}
		}
		rt_thread_mdelay(400);
	}
}


static rt_thread_t display_tid = RT_NULL;
void threadMenuDisplay_init(void)
{	
	  //thread_EmptyBuffer_sem = rt_sem_create("thread_EmptyBuffer_sem", 0, RT_IPC_FLAG_FIFO);
    display_tid = rt_thread_create("MenuDisp",
                            MenuDisplay_entry, RT_NULL,
                            0x200,
                            4, 5);

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

void getDisplay(void)
{
	  rt_sem_take(display_lock, RT_WAITING_FOREVER);
		if	(tsdb_recordcount('k')>0 || XbeeConst_CanSend==0)
				TDisplay++;  //是否有按键需要传输
		else  
				TDisplay = 1;
		
		rt_memset(WeightConst_Feed_display, 0x20, 40);			
    
		if(WeightConst_DELAYON)
		{
				WeightConst_Feed_display[0]=0xC7;
				WeightConst_Feed_display[1]=0xEB;
				WeightConst_Feed_display[2]=0xBD;
				WeightConst_Feed_display[3]=0xC1;
				WeightConst_Feed_display[4]=0xB0;
				WeightConst_Feed_display[5]=0xE8;
				WeightConst_Feed_display[6]=0x20;
				WeightConst_Feed_display[7]=0x20;
		}
		else if (!isWeight){			
			  
				for (int i = 0; i < 8; i++)   //显示标题
				{
						if	(TDisplay%2==0 && i>5) WeightConst_Feed_display[i]=0x3E;
						else  WeightConst_Feed_display[i] = WeightConst_feed[i];
				}
				
		}
		
		if(WeightConst_DELAYON)
		{
				WeightConst_Weight_display[0]=0xEB;
				WeightConst_Weight_display[1]=0xC3;
				WeightConst_Weight_display[2]=0x20;
				WeightConst_Weight_display[3]=0x20;
				WeightConst_Weight_display[4]=0x20;
				WeightConst_Weight_display[5]=0x20;
				WeightConst_Weight_display[6]=0x20;
				WeightConst_Weight_display[7]=0x20;
				WeightConst_Weight_displayW = (pf_comp.data.delaytime * 60 - WeightConst_DELAYTIME);
				for (int SendDisplay_i = 2; SendDisplay_i < 8; SendDisplay_i++) {
						if (SendDisplay_i>2)	WeightConst_Weight_displayW=WeightConst_Weight_displayW/10;

						if (abs(WeightConst_Weight_displayW)>0)
								WeightConst_Weight_display[SendDisplay_i] = abs((WeightConst_Weight_displayW)%10)+'0';
						else break;
				}
		}
		else
		{
				if (WeightConst_Product_Run>0 && WeightConst_displayTotal==0  && pf_comp.data.pfsort!=0xFE && WeightConst_WeightHaveBegin==1)
						WeightConst_Weight_displayW = WeightConst_WeightCurTem - WeightConst_WeightTarget;
				else
						WeightConst_Weight_displayW = WeightConst_WeightCurTem;

				for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示重量
						WeightConst_Weight_display[SendDisplay_i]=0x20;


				if (WeightConst_Product_Run>0 && WeightConst_displayTotal==0  && pf_comp.data.pfsort!=0xFE && WeightConst_WeightHaveBegin==1)
				{
						if ((WeightConst_Weight_displayW<=0 && pf_comp.data.addorout==0) ||
										(WeightConst_Weight_displayW>=0 && pf_comp.data.addorout == 1)
							 ) {  //少
								WeightConst_Weight_display[6]=0xD9;
								WeightConst_Weight_display[7]=0xC9;
						}
						else {
								WeightConst_Weight_display[6]=0xE0;  //多
								WeightConst_Weight_display[7]=0xB6;
						}

				}
				else	{
						if (WeightConst_Weight_displayW<0) {
								WeightConst_Weight_display[6]=0x2D;  //-
								WeightConst_Weight_display[7]=0x20;
						}
						else {
								WeightConst_Weight_display[6]=0x20;  //
								WeightConst_Weight_display[7]=0x20;
						}
				}

				if (WeightConst_Weight_displayW==0) {
						WeightConst_Weight_display[0]=0x30;
				}
				else
				{
						for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++) {
								if (SendDisplay_i>0)
										WeightConst_Weight_displayW=WeightConst_Weight_displayW/10;

								if (abs(WeightConst_Weight_displayW)>0)
										WeightConst_Weight_display[SendDisplay_i] = abs(WeightConst_Weight_displayW%10)+'0';
								else break;
						}

						if (WeightConst_WeightWS>0) {
								for (int SendDisplay_i = 4; SendDisplay_i >= WeightConst_WeightWS; SendDisplay_i--)
										if (WeightConst_Weight_display[SendDisplay_i]!=0x20)
												WeightConst_Weight_display[SendDisplay_i+1]=WeightConst_Weight_display[SendDisplay_i];

								WeightConst_Weight_display[WeightConst_WeightWS]=0x2E;
								if (WeightConst_Weight_display[WeightConst_WeightWS+1]==0x20) WeightConst_Weight_display[WeightConst_WeightWS+1] = 0x30;
								for (int SendDisplay_i = 0; SendDisplay_i < WeightConst_WeightWS; SendDisplay_i++)
										if (WeightConst_Weight_display[SendDisplay_i]==0x20) WeightConst_Weight_display[SendDisplay_i] = 0x30;
						}
				}


				if (WeightConst_WeightHaveBegin==0 && pf_comp.data.needBegin == 1)
				{
						if (WeightConst_SBType==1||WeightConst_SBType==6) {
								//for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //如果没有开始则空显示重量
								//	WeightConst_Weight_display[SendDisplay_i]=0x20;
								WeightConst_Weight_display[5]=WeightConst_Weight_display[6];
								WeightConst_Weight_display[6]=0xF8;
								WeightConst_Weight_display[7]=0xCB;
						}
						else if (WeightConst_SBType==3) {
								WeightConst_Weight_display[5]=WeightConst_Weight_display[6];
								WeightConst_Weight_display[6]=0xF8;
								WeightConst_Weight_display[7]=0xCB;
								/*
								WeightConst_Weight_display[0]=0xCC;
								WeightConst_Weight_display[1]=0xC5;
								WeightConst_Weight_display[2]=0xD3;
								WeightConst_Weight_display[3]=0xB3;
								WeightConst_Weight_display[4]=0xD5;
								WeightConst_Weight_display[5]=0xBF;
								WeightConst_Weight_display[6]=0xE5;
								WeightConst_Weight_display[7]=0xC7;//清空秤盘
								*/
						}
						else if (WeightConst_SBType==4||WeightConst_SBType==6) {
								WeightConst_Weight_display[0]=0xBC;
								WeightConst_Weight_display[1]=0xCA;
								WeightConst_Weight_display[2]=0xAA;
								WeightConst_Weight_display[3]=0xBF;
								WeightConst_Weight_display[4]=0xCF;
								WeightConst_Weight_display[5]=0xC8;
								WeightConst_Weight_display[6]=0xB7;
								WeightConst_Weight_display[7]=0xC8;//确认开始
						};
				};
		}
		rt_sem_release(display_lock);
}

static void sendRemote(void *parameter)
{
	while(1)
	{
    if (RemoteSendDisplay == 1 )
    {
        if ((flag_uart2_send==0 && (XbeeConst_UseAPI==1 || XbeeConst_UseAPI==3))||
                (flag_uart3_send==0 && XbeeConst_UseAPI==0))
        {
            XbeeConst_FRemoteTxBuf[0] = 23;
            XbeeConst_FRemoteTxBuf[1] = 0x7E;
            XbeeConst_FRemoteTxBuf[2] = 0x15;
            XbeeConst_FRemoteTxBuf[3] = XbeeConst_DeviceAddress;
            if (XbeeConst_UseAPI==1 || XbeeConst_UseAPI==3)
                XbeeConst_FRemoteTxBuf[4] = 0xA1;
            else
            {
                if (WeightConst_ChanCheShu>9)
                    XbeeConst_FRemoteTxBuf[4] = 0xA1;
                else
                    XbeeConst_FRemoteTxBuf[4] = 0xF1;
            }
            XbeeConst_FRemoteTxBuf[5] = 0x7E;
            XbeeConst_FRemoteTxBuf[6] = 0x45;

            for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++) {
                XbeeConst_FRemoteTxBuf[7+SendDisplay_i] =  WeightConst_feed[SendDisplay_i];//显示标题
                XbeeConst_FRemoteTxBuf[15+SendDisplay_i] =  WeightConst_Weight_display[7-SendDisplay_i];//显示重量
            }

            XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0x00;
            for (int SendDisplay_i = 1; SendDisplay_i < XbeeConst_FRemoteTxBuf[0]; SendDisplay_i++) {
                XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
                    XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[SendDisplay_i];
            }
            XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];

            if (XbeeConst_UseAPI==1 || XbeeConst_UseAPI==3) {
							  rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
                if (XbeeConst_UseAPI==1)
                    setSendFrame_t(0x01,0x01,0xC1);
                else setSendFrame_t(0x02,0x01,0xC1);
                XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);
								
                for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_PTxBuf0[0]; SendWifi_i++)  //将缓冲区发出
                    usart2_send_data[SendWifi_i-1] = XbeeConst_PTxBuf0[SendWifi_i];
                if (XbeeConst_UseAPI==1 || (XbeeConst_UseAPI==3 && ccsend==1))
								{	
                    HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);//xbeeSerial			
										rt_thread_mdelay(50);							
								}
								rt_sem_release(uart2_lock);
            }
            else
            {
                for (int SendWifi_i = 1; SendWifi_i <= XbeeConst_FRemoteTxBuf[0]; SendWifi_i++)  //将缓冲区发出
                    usart3_send_data[SendWifi_i-1] = XbeeConst_FRemoteTxBuf[SendWifi_i];							
								rt_sem_take(uart3_lock, RT_WAITING_FOREVER);
                HAL_UART_Transmit_DMA(&huart3, usart3_send_data, XbeeConst_FRemoteTxBuf[0]); //remoteSerial
								rt_thread_mdelay(50);
								rt_sem_release(uart3_lock);
            }
        }
    }
		rt_thread_mdelay(600);
	}
}

void ChancheShow(void) {
    uart4_send_data[0] = 0x7E;
    uart4_send_data[1] = 0x45;
    uart4_send_data[2] = XbeeConst_RemoteAddress + '0';

    for (int i = 3; i < 18; i++) //将缓冲区发出
        uart4_send_data[i] = 0x20;
	
		rt_sem_take(uart4_lock, RT_WAITING_FOREVER);				
	  HAL_UART_Transmit_DMA(&huart4, uart4_send_data, 12);
	  rt_sem_release(uart4_lock);
}

static void Display_entry(void *parameter) {
	  rt_uint32_t e;
    while (1) {
        getDisplay();
        if (WeightConst_SBType<3||WeightConst_SBType==6) {
					  uart4_send_data[0] = 0x7E;
            uart4_send_data[1] = 0x45;
				  	for (int i = 0; i < 8; i++)   //显示标题
								uart4_send_data[i+2] = WeightConst_Feed_display[i];
						
            for (int i = 7; i >=0; i--)   //显示重量
                uart4_send_data[i + 10] = WeightConst_Weight_display[7-i];

            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);
        }
				else if (WeightConst_SBType==4)
				{
						if (rt_event_recv(&display_event, (EVENT_clearall | EVENT_Current_noweight 
																 | EVENT_PlanName | EVENT_Current | EVENT_Stop) , 
												       RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 10, &e) == RT_EOK)
						{
						
								if (e & EVENT_clearall)
								{
										if(isn==1 ) {
												clearScreenAll_();
										}
										else
										{
											;
										}
								}
								if (e & EVENT_Current_noweight)
								{
										if(isn==1 ) {
												display_Current_noweight_();
										}
										else
										{
											;
										}
								}
								if (e & EVENT_PlanName)
								{
										if(isn==1 ) {
												display_productName_(0);
										}
										else
										{
											;
										}
								}
								if (e & EVENT_Current)
								{
										if(isn==1 ) {
												 display_Current_(WeightConst_Weight_displayW);
										}
										else
										{
											;
										}
								}
								
								if (e & EVENT_Stop)
								{
										if(isn==1 ) {
												;
										}
										else
										{
											;
										}
								}						
						}
						display_Now_(WeightConst_WeightCurTem);
					
				}
//        if (WeightConst_SBType==3 || WeightConst_SBType==7) {            
//						// if (WeightConst_Weight_display_old[SendDisplay_i] != WeightConst_Weight_display[SendDisplay_i])
//						{
//								get_msg(92,//Xstart
//												4,//Ystart
//												2,//size
//												0,//reset
//												0,//speed
//												8,//字符个数
//												3,//界面数
//												1,//id
//												0xBF,//Xend
//												1,
//												&WeightConst_Weight_display[0]//字码
//											 );
//								break;
//						}
//            for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++)   //显示原料名称
//            {
//                WeightConst_Weight_display_old[SendDisplay_i] = WeightConst_Weight_display[SendDisplay_i];
//            }
//        }
//        if (WeightConst_SBType==4||WeightConst_SBType==6)  {
//            if (WeightConst_Product_Run) {
//                if(isn!=1 && (ScreenBufSize()<100)) {
//                    display_Now();
//                    display_Total(WeightConst_WeightCurTem);
//                }
//                else if(isn==1) display_Now_(WeightConst_WeightCurTem);
//            }
//            else if(isn==1 && WeightConst_Run==1) display_Now_(WeightConst_WeightCurTem);
//        }

//        if (SendRemote_timer>700 && RemoteSendDisplay &&
//                ((flag_uart2_send==0 && (XbeeConst_UseAPI==1 || XbeeConst_UseAPI==3))||
//                 (flag_uart3_send==0 && XbeeConst_UseAPI==0)))
//        {
//            SendRemote_timer = 0;
          
//        }
          rt_thread_mdelay(400);
			}
}

static void Display_clear_entry(void *parameter) {
	  uint8_t i;
		for(i=0;i<4;i++){
			display_KPT_TMRWatch_();
			rt_thread_mdelay(1000);
		}
	
    while (1) {
       if (WeightConst_Run<1)  display_KPT_TMRWatch_();          
			 rt_thread_mdelay(10*1000);
		}
}

void threadDisplay_init(void)
{	
	  //thread_EmptyBuffer_sem = rt_sem_create("thread_EmptyBuffer_sem", 0, RT_IPC_FLAG_FIFO);
    display_tid = rt_thread_create("Display",
                            Display_entry, RT_NULL,
                            0x300,
                            5, 5);

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

		
static rt_thread_t display_clear_tid = RT_NULL;
void threadDisplay_delay_init(void)
{	
		display_clear_tid = rt_thread_create("Displaydelay",
														Display_clear_entry, RT_NULL,
														0x100,
														5, 5);

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

static rt_thread_t remotedisplay_tid = RT_NULL;
void threadRemoteDisplay_init(void)
{	
	  //thread_EmptyBuffer_sem = rt_sem_create("thread_EmptyBuffer_sem", 0, RT_IPC_FLAG_FIFO);
    remotedisplay_tid = rt_thread_create("RemoteD",
                            sendRemote, RT_NULL,
                            0x300,
                            5, 5);

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