<repo_name>heathzj/70M<file_sep>/Drivers/External-Peripherals/oled_c1.h
#ifndef OLED_C1_H
#define OLED_C1_H

#include "define.h"
#include "TCPIP Stack/TCPIP.h"

#define I2C_nLOLOCK 0x01u
#define I2C_nALC	0x02u
#define I2C_nMUTE	0x04u
#define nLCDLEN 40

#define nBYTE_COMPARE 0


typedef struct {
const char* pcTitle;
char cTitleIndex;
char* pcContent;
}tstLCDMenu;

extern char FirmwareVersion[4]; 

extern char LOStatusString1[9];
extern char LOStatusString2[9]; 

extern char RFFreqString1[12];
extern char RFFreqString2[12]; 
extern char AttenString1[8];
extern char AttenString2[8];

extern char IPString[16];
extern char MNString[18];
extern char SNString[9];

extern char s18VDC[4];
extern char s24VDC[4];

extern char sLNBREF[4];
extern char sBUCREF[4];


extern char sUCPower[10];
extern char sDCPower[10];
extern char sUPC_StPwr[10];
extern char sDNC_StPwr[10];


extern char sALCa[4];
extern char sALCb[4];
extern char sMutea[9];
extern char sMuteb[9];

extern unsigned char EditMode;
extern unsigned char EditPosition;
extern unsigned char FirstPressKey;
extern char LCDPrintBuffer[2][40];
extern char LCDBlinkBuffer[2][40];


extern const rom char acLCDTitle[30][nLCDLEN];
extern const rom char acLCDBlinkTitle[23][nLCDLEN];

typedef struct{
	void (*pvfPrintMenu)(void);
	void (*pvfSetValue)(void);
	void (*pvfMoveCursorLeft)(void);
	void (*pvfMoveCursorRight)(void);

	BYTE u8RightMenu;
	BYTE u8LeftMenu;
	BYTE u8UpMenu;
	BYTE u8DownMenu; 
    
    BYTE u8BlinkPos; /* Start position for blinking text */
    BYTE u8BlinkLen; /* Length for blinking text */
}tstMenuDisplay;


typedef union
{
    unsigned long u32Freq;
    unsigned char u8Freq[4];
}tunFreq;

typedef union
{
    unsigned short int u16Atten;
    unsigned char u8Atten[2];
}tunAtten;


typedef struct
{
	unsigned char u8Updateflag;   //BYTE 0
	unsigned char u8CtrlStatus;   //BYTE 1 0x1(bit0) = Lock status, 0x2(bit1) = ALC, 0x4(bit2) = Mute
	tunFreq       unRfFreq;     //BYTE 2-5 consider union here
    tunAtten      unAtten;      //BYTE 6-7 consider union here
    unsigned char u8WtPower;    //BYTE 8 Write outputpower to modules 
    unsigned char u8RdPower;    //BYTE 9 Read outputpower from modules

}tstI2CMessage;

/********************************************************************************************************************
byte 0: 
			bit0: write flag 1 by master, cleared by software
			bit1: AGC_OFFON off=1, on=0, write by master
  			bit2: lock status
			bit3: Rx_enbl, enable=1, disable=0;
byte 1,2,3,4: 4 bytes for RF frequency in kHz
byte 5,6: when AGC_OFFON=1(off),2bytes for user setable attenuation dB X 10, 0-250
			when AGC_OFFON=0(on), output power setting in dBm X10 +500,
			when it was written as 0xFFFF, nothing will be changed.
byte 7,8: output power detect, output_power dBm X10+500
byte 9: value=0-12	Gain compensation table address, 
							0=950MHz, 1=1050MHz, 2=1150MHz, 3=1250MHz, 4=1350MHz, 5=1450MHz, 
							6=1550MHz, 7=1650MHz, 8=1750MHz, 9=1850MHz, 10=1950MHz, 11=2050MHz, 
							12=2150MHz
			value=13-25	Power compensation table address,
							13=950MHz, 14=1050MHz, 15=1150MHz, 16=1250MHz, 17=1350MHz, 18=1450MHz, 
							19=1550MHz, 20=1650MHz, 21=1750MHz, 22=1850MHz, 23=1950MHz, 24=2050MHz, 
							25=2150MHz
			value=26-56	 output power detection table address, from -15dBm to +15dBm
							26=-15dBm, 27=-14dBm, 28=-13dBm, 29=-12dBm, 30=-11dBm, 31=-10dBm, 32=-9dBm, 
							33=-8dBm, 34=-7dBm, 35=-6dBm, 36=-5dBm, 37=-4dBm, 38=-3dBm, 39=-2dBm,
			 				40=-1dBm, 41=0dBm, 42=1dBm, 43=2dBm, 44=3dBm, 45=4dBm,46=5dBm, 47=6dBm, 
							48=7dBm, 49=8dBm, 50=9dBm, 51=10dBm, 52=11dBm, 53=12dBm, 54=13dBm, 55=14dBm, 
							56=15dBm, 
byte 10,11: 	Gain compensation dB X 10, range 0-65, 
				Power compensation dB X10, range 0-65
				Output power detection table
				if this is written as 0xfff by master,
				slave will read the compensation table and put into I2C_Array. 
**********************************************************************************************************************/
#define nI2C_WRITE 0x01
#define nI2C_READ 0xfe


#define nAGC_OFF		0xFD//0x02 //u8CtrlStatus|nAGC_OFF set off //swapped back
#define nAGC_ON			0x02//0xFD //u8CtrlStatus&nAGC_ON set on //swapped back
#define nLOCK			0x04
#define nUNLOCK 		0xFB
#define nRX_ENABLE 		0x08 //= mute on/off
#define nRX_DISABLE 	0xF7

typedef struct
{
	//bit0: write flag 1 by master, cleared by software		0x01 ~0xFE
	//bit1: AGC_OFFON off=1, on=0, write by master		0x02 ~0xFD
	//bit2: lock status									0x04  ~0xFB
	//bit3: Rx_enbl, enable=1, disable=0; 		 		0x08 ~0xF7

	BYTE u8CtrlStatus;   //BYTE 0 
	tunFreq       unRfFreq;     //BYTE 1-4 4 bytes for RF frequency in kHzs
    tunAtten      unAtten;      //BYTE 5-6 consider union here
    WORD		  u16RdPower;    //BYTE 7-8 output power detect, output_power dBm X10+500
    BYTE		u8GainCompenAddr; //byte 9 Gain compensation table address
	WORD		u16GainCompenVale; //byte 10-11 Gain compensation dB X 10, range 0-65, 

}tstI2CMsg;



#define nBUC_REF_ON	0x02
#define nBUC_REF_OFF 0xFD

#define nLNB_REF_ON	0x04
#define nLNB_REF_OFF 0xFB

#define nBUC_DC_ON	0x08
#define nBUC_DC_OFF 0xF7

#define nLNB_DC_ON	0x10
#define nLNB_DC_OFF 0xEF

#define nBUC_OVER_CURRENT	0x20

#define nLNB_OVER_CURRENT	0x40
#define nEXT_REF	0x80



/********************************************************************************************************************
byte 0: 
			bit0: write flag 1 by master, cleared by software
			bit1: BUC_REF_ONOFF 1=on, 0=off
  			bit2: LNB_REF_ONOFF 1=on, 0=off
			bit3: BUC_DC_ONOFF 1=on, 0=off
			bit4: LNB_DC_ONOFF 1=on, 0=off
			bit5: BUC DC over current 1=over current, 0=normal. 
					When BUC over current, BUC_DC_ONOFF will be set to 0. 
					This bit must be cleared before turn on the BUC DC again.
			bit6: LNB DC over current 1=over current, 0=normal. 
					When LNB over current, LNB_DC_ONOFF will be set to 0. 
					This bit must be cleared before turn on the LNB DC again.
			bit7: external reference detect 1=external reference 0=internal reference
byte 1:
			bit0: LED1
			bit1: LED2
			bit2: LED3
			bit3: LED4
byte 2,3: BUC current in mA, unsigned int
byte 4,5: LNB current in mA, unsigned int
byte 6,7: BUC current limit in mA, unsigned int
byte 8,9: LNB current limit in mA, unsinged int
**********************************************************************************************************************/

typedef struct
{
    BYTE u8Status;	//byte 0
	BYTE u8LEDSetting; //byte 1 bit0-3,= LED1-4
	WORD u16BUCCurrent;   //byte 2-3BUC current in mA, unsigned int
	WORD u16LNBCurrent;   //byte 4-5 LNB current in mA, unsigned int
	WORD u16BUCCurrentLimit;   //BYTE 6-7 BUC current limit in mA, unsigned int
	WORD u16LNBCurrentLimit;   //BYTE 8-9 LNB  current limit in mA, unsigned int	

}tstI2CMsgVOL;



typedef struct
{
    unsigned char u8Updateflag;
	unsigned char u818VDC;   //BYTE 0
	unsigned char u824VDC;   //BYTE 1

}tstI2CMessageVOL;


extern tstI2CMessage  stI2CUpCvtMsg;
extern tstI2CMessage  stI2CDownCvtMsg;
extern tstI2CMessageVOL  stI2CVOLMsg;

extern tstI2CMsg  stI2CUCMsg;
extern tstI2CMsg  stI2CDCMsg;
extern tstI2CMsgVOL  stI2CREFMsg;



extern char IPString[];
extern unsigned char LastMenu;
extern unsigned char CurrentMenu;
extern APP_CONFIG AppConfig;
extern unsigned char KeyPressedFlag, PressedKey, RefreshFlag;
extern BYTE EditUpdateFlag;//Edit position char update only
extern BYTE ValueUpdateFlag; //Content Value Update only

extern const rom tstMenuDisplay stMenuDisplay[30];



void OLED_vSetKepad(void);

void OLED_vSetIO(void);

void OLED_vEnableInterrupt(void);


void CheckBusy();

void WriteIns(char instruction);

void WriteData(char data1);

unsigned char ReadData(void);

void WriteString(unsigned char count,unsigned char * MSG);

void LCDWriteString(unsigned char * MSG);

void PrintCGRAM(unsigned char number1);

void LCDClear(void);

void LCDReturnhome(void);

void LCDEntryMode(void);


void LCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag);

void Initial_LCD(void);

void PrintLCD(void);

void PrintBlinkLCD(void);



void DisplayCompanyMenu(void);


void InitalizeMenu(void);



BYTE LCD__u8GetOnOff(void);

BYTE LCD__u8UpdateToModule(BYTE* pu8Parameter,BYTE* pu8Data ,BYTE u8CompareFlag,BYTE* pu8SendData);



BYTE LCD_u8UpdateDisplayContent(void);
void KeyProcessing(void);
	void UpdateStatus(void);


void Main_vLEDLO(BYTE Setting);

void Main_vLEDTX(BYTE Setting);
void Main_vLEDLO(BYTE Setting);


//Set LED IO direction to output
void Main_vLEDInit(void);


#endif /* OLED_C1_H */


<file_sep>/Drivers/External-Peripherals/oled_c1.c
#include "define.h"
#include "i2c_c1.h"


#include "oled_c1.h"





rom unsigned char CGRAM[2][8] = {{0x04,0x0E,0x15,0x04,0x04,0x04,0x04,0x04}, {0x04,0x15,0x0E,0x04,0x04,0x04,0x04,0x04}};

void OLED_vSetKepad(void)
{
	UPKEY_TRIS = 1;
	DOWNKEY_TRIS = 1;
	LEFTKEY_TRIS = 1;
	RIGHTKEY_TRIS = 1;
	ENTERKEY_TRIS = 1;
	CANCELKEY_TRIS = 1;

	CONVERTERA_STATUS_TRIS = 1;
	CONVERTERB_STATUS_TRIS = 1;

}

void OLED_vSetIO(void)
{
	OLED_RS_TRIS = 0;
	OLED_RW_TRIS = 0;
	OLED_EN_TRIS = 0;
	OLED_DB7_TRIS = 0;
	OLED_DB6_TRIS = 0;
	OLED_DB5_TRIS = 0;
	OLED_DB4_TRIS = 0;
	OLED_DB3_TRIS = 0;
	OLED_DB2_TRIS = 0;
	OLED_DB1_TRIS = 0;
	OLED_DB0_TRIS = 0;

}

void OLED_vEnableInterrupt(void)
{
	PIE1bits.TMR2IE = 0;  // disable Timer2 interrupt
	PIR1bits.TMR2IF = 0;
	IPR1bits.TMR2IP = 0; // set Timer2 interrupt to low interrupt




	INTCON3bits.INT1IE = 1; // enable key interrupt
	INTCON3bits.INT1IP = 0; // set key interrupt to low interrupt
	INTCON2bits.INTEDG1 = 1; // 1 = Interrupt on rising edge
	//	INTCON2bits.INTEDG1 = 0; // 0 = Interrupt on falling edge
	TRISBbits.TRISB1 = 1; // RB1 input, INT1
	ADCON0bits.ADON = 0;
	ADCON1 |= 0x0F; // set all IO pin to digital

}

void CheckBusy()
{
	unsigned char temp;

	OLED_BUS_W = 0xff;
	OLED_RS = 0;
	OLED_RW = 1;
	OLED_BUS_TRIS = 0xFF;
	do
	{
		OLED_EN = 1;
		Nop(); /* zj */
		temp = OLED_DB7_R;
		OLED_EN = 0;
	}while(temp);
	OLED_BUS_TRIS = 0;
}

void WriteIns(char instruction)
{
	CheckBusy();
	OLED_RS = 0;
	OLED_RW = 0;
	OLED_EN = 1;
	Nop(); /* zj */
	OLED_BUS_W = instruction;
	OLED_EN = 0;
}
void WriteData(char data1)
{
	CheckBusy();
	OLED_RS = 1;
	OLED_RW = 0;
	Nop(); /* zj */
	OLED_EN = 1;
	Nop(); /* zj */
	OLED_BUS_W = data1;
	OLED_EN = 0;
	
	Nop(); /* zj */
}
unsigned char ReadData(void)
{
	unsigned char temp;

	OLED_BUS_TRIS = 0xFF;
	OLED_RS = 1;
	OLED_RW = 1;
	OLED_EN = 1;
	Nop(); /* zj */
	temp = OLED_BUS_R;
	OLED_EN = 0;
	OLED_BUS_TRIS = 0;
	CheckBusy();

	return temp;
}

void WriteString(unsigned char count,unsigned char * MSG)
{
	unsigned char i;
	for(i = 0; i < count; i++)
	{
		if ((MSG[i] >= ' ') && (MSG[i] <= 'z'))
		{
			WriteData(MSG[i]);
		}
	}
}
void LCDWriteString(unsigned char * MSG)
{
	unsigned char temp;
	
	temp = *MSG;
	while (temp)
	{
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}
		MSG++;
		temp = *MSG;	
	}
}

void DRI_vPrintCGRAM(unsigned char number1)
{
	unsigned char i;
	WriteIns(0x40); //SET CG_RAM ADDRESS 000000
	for(i = 0;i<=24;i++)
	{
		WriteData(CGRAM[number1][i]);
	}
}

void DRI_vLCDClear(void)
{
	WriteIns(0x01); //Clear Display
}

void DRI_vLCDReturnhome(void)
{
	WriteIns(0x02); //Return Home
}
void DRI_vLCDEntryMode(void)
{
	WriteIns(0x06); //Entry Mode Set, address increment & Shift off
}

void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
{
	unsigned char temp = 0x08;
	
	if (displayflag == TRUE)
	{
		temp |= 0x04; // display ON
	}
	if (cusorflag == TRUE)
	{
		temp |= 0x02; // cusor ON
	}
	if (blinkflag == TRUE)
	{
		temp |= 0x01; // Blink ON
	}
	WriteIns(temp); //Display ON/OFF control,Display ON,Cursor&Blink OFF
}

void Initial_LCD(void)
{
	CurrentMenu = MENU0;
	LastMenu = CurrentMenu;


	WriteIns(0x38); //function set,8-bit transfer,2-lines display & 5*8 dot characteristic, font 00
	WriteIns(0x38); //function set,8-bit transfer,2-lines display & 5*8 dot characteristic, font 00
	WriteIns(0x38); //function set,8-bit transfer,2-lines display & 5*8 dot characteristic, font 00
	WriteIns(0x38); //function set,8-bit transfer,2-lines display & 5*8 dot characteristic, font 00
	WriteIns(0x0C); //Display ON/OFF control,Display ON,Cursor&Blink OFF
	WriteIns(0x06); //Entry Mode Set, address increment & Shift off
	WriteIns(0x02); //Return Home
	WriteIns(0x01); //Clear Display
}

void PrintCompanyLCD(void)
{
	unsigned char i, temp, temp1;

	for (i = 0; i < 40; i++)
	{
		temp = 0x80 + i;
		WriteIns(temp);
		temp = LCDPrintBuffer[0][i];
		
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}
	}
	for (i = 0; i < 40; i++)
	{
		temp = 0xC0 + i;
		WriteIns(temp);
		temp = LCDPrintBuffer[1][i];
		
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}
	}
	if (EditMode == TRUE)
	{
		// void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
		WriteIns(0xC0 + EditPosition);
		DRI_vLCDDisplay(TRUE, TRUE, FALSE);
	}
}


void PrintLCDWholePage(void)
{
	unsigned char i, temp, temp1;

	/* First 3 pages have same menu, no need to re-print, but menu 0 is initial , need to print */
	
	//if((LastMenu > 3) || (CurrentMenu > 3) || ((CurrentMenu==0) && (LastMenu==0)))
	{
			WriteIns(0x80);
			Nop();

			for (i = 0; i < 40; i++)
			{
				temp = acLCDTitle[CurrentMenu][i];
				if ((temp >= ' ') && (temp <= 'z'))
				{
					WriteData(temp);
				}
			}
	}

	WriteIns(0xC0);
	Nop();
	for (i = 0; i < 40; i++)
	{
		//temp = 0xC0 + i;
		//WriteIns(temp);
		temp = LCDPrintBuffer[1][i];
		
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}
	}
	if (EditMode == TRUE)
	{
		// void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
		WriteIns(0xC0 + EditPosition);
		DRI_vLCDDisplay(TRUE, TRUE, FALSE);
	}
}


/* JuZh, Consider only print the blink text part if there is no Menu Index change, provieded the blink location and length */

void PrintBlinkMenuText(void)
{
	unsigned char i, temp;
    static BYTE boBlink;
    BYTE u8BlinkPos;
    BYTE u8BlinkLen;

    u8BlinkPos = stMenuDisplay[CurrentMenu].u8BlinkPos;
    u8BlinkLen = stMenuDisplay[CurrentMenu].u8BlinkLen;
    
    boBlink = boBlink?0:1;
    
	WriteIns(0x80 + u8BlinkPos);
	for (i = 0; i < u8BlinkLen; i++)
	{
        temp = boBlink?' ':acLCDTitle[CurrentMenu][i + u8BlinkPos]; /* toggole between space and text */
        
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}

		
		
	}
    	//DelayMS(1);
    	
	if (EditMode == TRUE)
	{
		// void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
		WriteIns(0xC0 + EditPosition);
		DRI_vLCDDisplay(TRUE, TRUE, FALSE);
	}
	DelayMS(1);

}


/* consider only update the value text instead of whole line */
void PrintNewValue(void)
{
	unsigned char i, temp, temp1;


	WriteIns(0xC0);
	for (i = 0; i < 40; i++)
	{

		temp = LCDPrintBuffer[1][i];
		
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}
	}

	if (EditMode == TRUE)
	{
		// void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
		WriteIns(0xC0 + EditPosition);
		DRI_vLCDDisplay(TRUE, TRUE, FALSE);
	}    
    
}


void PrintEditPosition(void)
{
    
    unsigned char i, temp;    
    
    WriteIns(0xC0+EditPosition);

    
    if((LCDPrintBuffer[1][EditPosition] == 'E') || (LCDPrintBuffer[1][EditPosition] == 'D')) //Enabled Disabled
    {
            for (i = 0; i < 9; i++) /* Only re-print the cursor value or print "ON", "OFF", 2 bytes are enough */
            {
                temp = LCDPrintBuffer[1][EditPosition + i];

                if ((temp >= ' ') && (temp <= 'z'))
                {
                    WriteData(temp);
                }
            }     
        
        
    }
    
    

	else if(LCDPrintBuffer[1][EditPosition] != 'O') /* Not editing On/Off , set only 1 number*/
	{
		temp = LCDPrintBuffer[1][EditPosition];

        if ((temp >= ' ') && (temp <= 'z'))
        {
            WriteData(temp);
        }
	}
	
	else /* On/Off, set 2 bytes */
	{
	    for (i = 0; i < 3; i++) /* Only re-print the cursor value or print "ON", "OFF", 2 bytes are enough */
	    {
	        temp = LCDPrintBuffer[1][EditPosition + i];

	        if ((temp >= ' ') && (temp <= 'z'))
	        {
	            WriteData(temp);
	        }
	    }  
	}

	if (EditMode == TRUE)
	{
		// void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
		WriteIns(0xC0 + EditPosition);
		DRI_vLCDDisplay(TRUE, TRUE, FALSE);
	}    
           
}



/* Not used any more */
void PrintBlinkLCD(void)
{
	unsigned char i, temp;

	WriteIns(0x80);
	for (i = 0; i < 40; i++)
	{

		temp = acLCDBlinkTitle[CurrentMenu][i];;
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}
	}
	WriteIns(0xC0);
	for (i = 0; i < 40; i++)
	{
		temp = LCDPrintBuffer[1][i];
		
		if ((temp >= ' ') && (temp <= 'z'))
		{
			WriteData(temp);
		}
	}
	if (EditMode == TRUE)
	{
		// void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
		WriteIns(0xC0 + EditPosition);
		DRI_vLCDDisplay(TRUE, TRUE, FALSE);
	}
}




<file_sep>/Devices/UpConverter_c1.c
#include "define.h"
#include "Global.h"

#include "oled_c1.h"
#include "string.h"
#include "Util.h"


void UpCvt_vPrintFreq(void)
{
	strncpy(LCDPrintBuffer[1], RFFreqString1,sizeof(RFFreqString1));	
}


void UpCvt_vPrintPower(void)
{

	strncpy(LCDPrintBuffer[1], sUCPower,sizeof(sUCPower));

}

void UpCvt_vPrintStPwr(void)
{
	strncpy(LCDPrintBuffer[1], sUPC_StPwr,sizeof(sUPC_StPwr));

}


void UpCvt_vPrintLo(void)
{

	if (stConverter.stUp.u8AlarmStatus == 1)
	{
		strncpy(LCDPrintBuffer[1], LOStatusString1,sizeof(LOStatusString1));
	}
	else
	{
		strncpy(LCDPrintBuffer[1], LOStatusString2,sizeof(LOStatusString2));
	}

}

void UpCvt_vPrintAttn(void)
{

	strncpy(LCDPrintBuffer[1], AttenString1,sizeof(AttenString1));

}

void UpCvt_vPrintALC(void)
{

	strncpy(LCDPrintBuffer[1], sALCa,sizeof(sALCa));
}



void UpCvt_vPrintMute(void)
{	

	if(stConverter.stUp.u8Mute==1)
		strncpypgm2ram(sMutea,"ENABLED ",8);
	else
		strncpypgm2ram(sMutea,"DISABLED",8);
 
	strncpy(LCDPrintBuffer[1], sMutea,sizeof(sMutea));	  
}

void UpCvt_vSendI2C()
{
	stI2CUCMsg.u16GainCompenVale = 0xFFFF; /* Don't write tables */
	SetI2C(MODULE0);

}


void UpCvt_vSetFreq(void)
{

    DWORD tempDWORD = 0;
    float fFreq = 0;
                                                                                             
    // update LCD value to Freq/Atten/IP, TBD, update wrt power, ALC, Mute
    if (CurrentMenu == nFREQ_MENU_A) // UC1 Freq
    {        
        fFreq =  Util_u8String2NFloat((char*)&LCDPrintBuffer[1][0]);
        tempDWORD = (DWORD)(fFreq * 1000.0f);// should deduct 950 000? 950M ~ 2150M

		tempDWORD = nLO_FREQ - tempDWORD;
        
        if ((tempDWORD != stConverter.stUp.u32OutputFreq) && (tempDWORD <= MAXRFFREQ) && (tempDWORD >= MINRFFREQ))
        {
            //stConverter.stUp.u32OutputFreq = tempDWORD;
            stI2CUCMsg.unRfFreq.u32Freq = Util_u16DWORDSwap(tempDWORD);
            strncpy(RFFreqString1 ,LCDPrintBuffer[1], sizeof(RFFreqString1) );
            UpCvt_vSendI2C();
        }
    }

}


void UpCvt_vSetAttn(void)
{

	WORD tempWORD = 0; 

  	 if ((stConverter.stUp.u8ALC != STATUS_OFF))
	 return;

    tempWORD = LCDPrintBuffer[1][0] - '0';
    tempWORD = ((WORD)(tempWORD * 10)) + ((WORD)LCDPrintBuffer[1][1] - '0');
    tempWORD = ((WORD)(tempWORD * 10)) + ((DWORD)LCDPrintBuffer[1][3] - '0');
    
    /* tempWORD is atten * 10 */
    if ((tempWORD != stConverter.stUp.u16Atten) && (tempWORD <= MAXATTEN)&&(tempWORD >= MINATTEN))
    {
        //stConverter.stUp.u16Atten = tempWORD;
        stI2CUCMsg.unAtten.u16Atten= Util_u16ByteSwap(tempWORD);
        memcpy(AttenString1 ,LCDPrintBuffer[1], sizeof(AttenString1) );

        UpCvt_vSendI2C();
    }
}

void UpCvt_vSetPower(void)
{
/*Disabled and moved to 	UpCvt_vSetStPwr */		
    
}


void UpCvt_vSetStPwr(void)
{
	BYTE u8TempByte =0;
	float fTemp;
	SHORT i16Temp;
	WORD u16Temp;
	
	
	
	if ((stConverter.stUp.u8ALC != STATUS_ON))
		return;
	
	
	fTemp = Util_u8String2Float(LCDPrintBuffer[1]);
	
	u16Temp = (WORD)(fTemp *10 + 500);
	
	
	if ((u16Temp!= stConverter.stUp.u16SetPwr) && (u16Temp <= MAXPOWER) && (u16Temp >= MINPOWER))
	{
		stConverter.stUp.u16SetPwr = u16Temp;
		stConverter.stUp.fSetPwr= fTemp;
		//shared 16bits of att and wtpwr
		stI2CUCMsg.unAtten.u16Atten = Util_u16ByteSwap(u16Temp);
	
		memcpy(sUPC_StPwr,LCDPrintBuffer[1], sizeof(sUPC_StPwr) );
	
		
		UpCvt_vSendI2C();
	}  
	


}


void UpCvt_vSetALC(void)
{
    BYTE u8TempByte =0;

    u8TempByte = LCD__u8GetOnOff();

	strncpy(sALCa,LCDPrintBuffer[1], sizeof(sALCa) );

	if(u8TempByte==0)
	{
		stConverter.stUp.u8ALC = OFF;
		stI2CUCMsg.u8CtrlStatus &= nAGC_OFF;//0xFD,swapped back
	}
	else if(u8TempByte==1)
	{
		stConverter.stUp.u8ALC = ON;
		stI2CUCMsg.u8CtrlStatus |= nAGC_ON;//0x02, swapped back	
	}	

	UpCvt_vSendI2C();
	
    
}

void UpCvt_vSetMute()
{
    BYTE u8TempByte =0;


	if(!memcmppgm2ram(LCDPrintBuffer[1],"ENABLED",7))
		u8TempByte = 1;
	else if (!memcmppgm2ram(LCDPrintBuffer[1],"DISABLED",8))
		u8TempByte = 0;

    if(u8TempByte==0) 
    {
    	stConverter.stUp.u8Mute = 0;
        strncpy(sMutea,LCDPrintBuffer[1], sizeof(sMutea) );	
		stI2CUCMsg.u8CtrlStatus &= nRX_DISABLE  ;

        UpCvt_vSendI2C();

		Main_vLEDTX(u8TempByte);
    }
	 else if(u8TempByte==1) 
    {
    	stConverter.stUp.u8Mute = 1;
        strncpy(sMutea,LCDPrintBuffer[1], sizeof(sMutea) );	
		stI2CUCMsg.u8CtrlStatus |= nRX_ENABLE;

        UpCvt_vSendI2C();

		Main_vLEDTX(u8TempByte);
    }
	
    
}





<file_sep>/Drivers/External-Peripherals/oled_c2.h
#ifndef OLED_C2_H
#define OLED_C2_H


void LCDSub_vLeftMoveCursorFreq(void);
void LCDSub_vLeftMoveCursorPower(void);
void LCDSub_vLeftMoveCursorAttn(void);
void LCDSub_vLeftMoveCursorIP(void);


void LCDSub_vRightMoveCursorFreq(void);
void LCDSub_vRightMoveCursorPower(void);
void LCDSub_vRightMoveCursorAttn(void);
void LCDSub_vRightMoveCursorIP(void);



#endif /* OLED_C2_H */
<file_sep>/Drivers/External-Peripherals/oled_c2.c
#include "define.h"
#include "Global.h"
#include <string.h>

#include "DeviceApi.h"
#include "i2c_c1.h"
#include "Util.h"


#include "oled_c1.h"
#include "oled_c2.h"


unsigned char LCDBufferPosition[2];
char LOStatusString1[9] = {"LOCKED  "}; 
char LOStatusString2[9] = {"UNLOCKED"}; 

/*JuZh 950.000MHz to 2150.000MHz */
char RFFreqString1[12] = {"3625.000MHz"}; /* must update to 3 digits decimal */
char RFFreqString2[12] = {"3625.000MHz"}; /* must update to 3 digits decimal */
char AttenString1[8] = {"00.0 dB"}; 
char AttenString2[8] = {"00.0 dB"}; 

char IPString[16] = {"192.168.001.001"}; 
char MNString[18] = {"SCMDC62001SN10-RM"}; 
char SNString[9] = {"12345678"}; 

char s18VDC[4] = {"OFF"};
char s24VDC[4] = {"OFF"};
char sLNBREF[4] = {"OFF"};
char sBUCREF[4] = {"OFF"};


char sUCPower[10] = {"-30.0 dBm"};
char sDCPower[10] = {"-30.0 dBm"};
char sUPC_StPwr[10] = {"-00.0 dBm"};
char sDNC_StPwr[10] = {"-00.0 dBm"};

char sALCa[4]= {"OFF"};
char sALCb[4] = {"OFF"};
char sMutea[9] = {"DISABLED"};
char sMuteb[9] = {"DISABLED"};


unsigned char LastMenu = MENU0;
unsigned char CurrentMenu = MENU0;

/* LCD_u8UpdateDisplayContent is called if RefreshFlag is set to TRUE, it is set to TRUE if there is sys value update 
   or if there is Menu page change.
   RefreshFlag is set to false after LCD_u8UpdateDisplayContent and PrintLCD
*/
unsigned char KeyPressedFlag = FALSE, PressedKey = NULL;

BYTE RefreshFlag = FALSE; //Page change, update whole Menu
BYTE EditUpdateFlag = FALSE;//Edit position char update only
BYTE ValueUpdateFlag = FALSE; //Content Value Update only

unsigned char EditMode = FALSE, EditPosition = 0, FirstPressKey = TRUE;

tstI2CMessage  stI2CUpCvtMsg;
tstI2CMessage  stI2CDownCvtMsg;
tstI2CMessageVOL  stI2CVOLMsg;

tstI2CMsg  stI2CUCMsg;
tstI2CMsg  stI2CDCMsg;
tstI2CMsgVOL  stI2CREFMsg;
tstI2CMsg stI2CLOMsg;

BYTE u8ExtLO = 0;


char readflag = 0x00;


#pragma udata gpr8
char LCDPrintBuffer[2][nLCDLEN];

#pragma udata grp10
char LCDBlinkBuffer[2][nLCDLEN];


typedef struct menustructure
{
	unsigned char editable;
} menustructure_t;
#define MaxMenuNum 		28
menustructure_t ConvertMenu[MaxMenuNum];

		

typedef struct{
	void (*pvfPrintMenu)(void);
	void (*pvfSetValue)(void);
	void (*pvfMoveCursorLeft)(void);
	void (*pvfMoveCursorRight)(void);

	BYTE u8RightMenu;
	BYTE u8LeftMenu;
	BYTE u8UpMenu;
	BYTE u8DownMenu; 
    
    BYTE u8BlinkPos; /* Start position for blinking text */
    BYTE u8BlinkLen; /* Length for blinking text */
}tstMenuDisplay;


const rom tstMenuDisplay stMenuDisplay[30]=
{
	{&Main_vPrintUpConverter,NULL,NULL,NULL,				MENU2,MENU3,	MENU0,MENU4,0,16}, //zj changed 0 -3, blink len 7 to 14
		
	{&Main_vPrintDownConverter,NULL,NULL,NULL,				MENU2,MENU0,	MENU1,MENU11,8,7},//skip
	
	{&Main_vPrintVolCtrl,NULL,NULL,NULL,					MENU3,MENU0,	MENU2,MENU2,16,10},//zj changed	to be fixed page
	
	{&Main_vPrintDeviceInfo,NULL,NULL,NULL,					MENU0,MENU2,	MENU3,MENU27,26,13},//zj changed 0 -3
	

	{&UpCvt_vPrintFreq, &UpCvt_vSetFreq,&LCDSub_vLeftMoveCursorFreq,&LCDSub_vRightMoveCursorFreq,		MENU5,MENU10,	MENU0,MENU4,3,9},
	
	{&UpCvt_vPrintPower, &UpCvt_vSetPower,&LCDSub_vLeftMoveCursorPower,&LCDSub_vRightMoveCursorPower,	MENU6,MENU4,	MENU0,MENU5,3,7},

	{&UpCvt_vPrintStPwr, &UpCvt_vSetStPwr,&LCDSub_vLeftMoveCursorPower,&LCDSub_vRightMoveCursorPower,	MENU7,MENU5,	MENU0,MENU6,3,9},	

	{&UpCvt_vPrintLo, NULL,NULL,NULL,																	MENU8,MENU6,	MENU0,MENU7,3,11},
	
	{&UpCvt_vPrintAttn, &UpCvt_vSetAttn,&LCDSub_vLeftMoveCursorAttn,&LCDSub_vRightMoveCursorAttn,		MENU9,MENU7,	MENU0,MENU8,3,6 },
	
	{&UpCvt_vPrintALC, &UpCvt_vSetALC,NULL,NULL,														MENU10,MENU8,	MENU0,MENU9,3,5},
	
	{&UpCvt_vPrintMute, &UpCvt_vSetMute,NULL,NULL,														MENU4,MENU9,	MENU0,MENU10,3,9},
	
	
	
	{&DownCvt_vPrintFreq, &DownCvt_vSetFreq,&LCDSub_vLeftMoveCursorFreq,&LCDSub_vRightMoveCursorFreq,	MENU12,MENU17,	MENU1,MENU11,3,9},
	
	{&DownCvt_vPrintPower, &DownCvt_vSetPower,&LCDSub_vLeftMoveCursorPower,&LCDSub_vRightMoveCursorPower,MENU13,MENU11,	MENU1,MENU12,3,7},
	
	{&DownCvt_vPrintStPwr, &DownCvt_vSetStPwr,&LCDSub_vLeftMoveCursorPower,&LCDSub_vRightMoveCursorPower,MENU14,MENU12,	MENU0,MENU13,3,9},	

	{&DownCvt_vPrintLo, NULL,NULL,NULL,																	MENU15,MENU13,	MENU1,MENU14,3,11},
	
	{&DownCvt_vPrintAttn, &DownCvt_vSetAttn,&LCDSub_vLeftMoveCursorAttn,&LCDSub_vRightMoveCursorAttn,	MENU16,MENU14,	MENU1,MENU15,3,6},
	
	{&DownCvt_vPrintALC, &DownCvt_vSetALC,NULL,NULL,													MENU17,MENU15,	MENU1,MENU16,3,5},
	
	{&DownCvt_vPrintMute, &DownCvt_vSetMute,NULL,NULL,													MENU11,MENU16,	MENU1,MENU17,3,9},
	
	
	
	{&VOLCTRL_vPrint18V, &VOLCTRL_vSet18V,NULL,NULL,			MENU19,MENU26,	MENU2,MENU18,5,8},
	
	{&VOLCTRL_vPrint24V, &VOLCTRL_vSet24V,NULL,NULL,			MENU20,MENU18,	MENU2,MENU19,5,8},
		
	{&VOLCTRL_vPrintLNBREF, &VOLCTRL_vSetLNBREF,NULL,NULL,		MENU21,MENU19,	MENU2,MENU20,5,9},
	
	{&VOLCTRL_vPrintBUCREF, &VOLCTRL_vSetBUCREF,NULL,NULL,		MENU22,MENU20,	MENU2,MENU21,5,9},


	{&VOLCTRL_vPrintEXTREF, NULL,NULL,NULL,						MENU23,MENU21,	MENU2,MENU22,5,9},

	{&VOLCTRL_vPrintBUCI, NULL,NULL,NULL,						MENU24,MENU22,	MENU2,MENU23,5,13},

	{&VOLCTRL_vPrintLNBI, NULL,NULL,NULL,						MENU25,MENU23,	MENU2,MENU24,5,13},
	
	{&VOLCTRL_vPrintBUCAlm, &VOLCTRL_vClearBUCAlm,NULL,NULL, 	MENU26,MENU24,	MENU2,MENU25,5,17},

	{&VOLCTRL_vPrintLNBAlm, &VOLCTRL_vClearLNBAlm,NULL,NULL, 	MENU18,MENU25,	MENU2,MENU26,5,17},
	
	
	{&Main_vPrintIP,&Main_vSetIP,&LCDSub_vLeftMoveCursorIP,&LCDSub_vRightMoveCursorIP,	MENU28,MENU29,	MENU3,MENU27,5,4},
	
	{&Main_vPrintModelNo,NULL,NULL,NULL,												MENU29,MENU27,	MENU3,MENU28,10,15},
	
	{&Main_vPrintSerialNo,NULL,NULL,NULL,												MENU27,MENU28,	MENU3,MENU29,26,14}

};



const rom char acLCDTitle[30][nLCDLEN]={
   //1234567890123456789012345678901234567890//
   //0123456789012345678901234567890123456789//     
    "[DOWN CONVERTER][MAINCTRL][DEVICE INFO] ",
    "[DOWN CONVERTER][MAINCTRL][DEVICE INFO] ",
    "[DOWN CONVERTER][MAINCTRL][DEVICE INFO] ",
    "[DOWN CONVERTER][MAINCTRL][DEVICE INFO] ",
    
    "DC [RF FREQ] [POWER] [SET PWR] [LO STATU",
    "DC [POWER] [SET PWR] [LO STATUS] [ATTN] ",
    "DC [SET PWR] [LO STATUS] [ATTN] [ALC] [R",
    "DC [LO STATUS] [ATTN] [ALC] [RF ENBL] [R",	  
    "DC [ATTN] [ALC] [RF ENBL] [RF FREQ] [POW",
    "DC [ALC] [RF ENBL] [RF FREQ] [POWER] [SE",
    "DC [RF ENBL] [RF FREQ] [POWER] [SET PWR]",

    
    "DC [RF FREQ] [POWER] [SET PWR] [LO STATU",
    "DC [POWER] [SET PWR] [LO STATUS] [ATTN] ",
    "DC [SET PWR] [LO STATUS] [ATTN] [ALC] [R",
    "DC [LO STATUS] [ATTN] [ALC] [RF ENBL] [R",    
    "DC [ATTN] [ALC] [RF ENBL] [RF FREQ] [POW",
    "DC [ALC] [RF ENBL] [RF FREQ] [POWER] [SE",
    "DC [RF ENBL] [RF FREQ] [POWER] [SET PWR]",
    
    "CTRL [LNB DC] [BUC DC] [LNB REF] [BUC RE",
    "CTRL [BUC DC] [LNB REF] [BUC REF]   REF ",
    "CTRL [LNB REF] [BUC REF] [  REF  ] [BUC ",
    "CTRL [BUC REF] [  REF  ] [BUC CURRENT] [",
    
    "CTRL [  REF  ] [BUC CURRENT] [LNB CURREN", 
    "CTRL [BUC CURRENT] [LNB CURRENT] [BUC OV", 
    "CTRL [LNB CURRENT] [BUC OVERCURRENT] [LN", 
    "CTRL [BUC OVERCURRENT] [LNB OVERCURRENT]", 
    "CTRL [LNB OVERCURRENT] [LNB DC] [BUC DC]",    
    
    "INFO [IP] [MODULE NUMBER] [SERIAL NUMBER",
    "INFO [IP] [MODULE NUMBER] [SERIAL NUMBER",
    "INFO [IP] [MODULE NUMBER] [SERIAL NUMBER"
}; /* Page 0-3 same title */

const rom char acLCDBlinkTitle[23][nLCDLEN]={
"        [DNCVT] [DC VOL] [DEVICE INFO]  ",
"[UPCVT]         [DC VOL] [DEVICE INFO]  ",
"[UPCVT] [DNCVT]          [DEVICE INFO]  ",
"[UPCVT] [DNCVT] [DC VOL]                ",

"UC            [POWER] [LO STATUS] [ATTN]",
"UC          [LO STATUS] [ATTN] [ALC] [MU",
"UC              [ATTN] [ALC] [MUTE] [RF ",
"UC         [ALC] [MUTE] [RF FREQ] [POWER",
"UC        [MUTE] [RF FREQ] [POWER] [LO S",
"UC         [RF FREQ] [POWER] [LO STATUS]",

"DC           [POWER] [LO STATUS] [ATTN]",
"DC         [LO STATUS] [ATTN] [ALC] [MU",
"DC             [ATTN] [ALC] [MUTE] [RF ",
"DC        [ALC] [MUTE] [RF FREQ] [POWER",
"DC       [MUTE] [RF FREQ] [POWER] [LO S",
"DC        [RF FREQ] [POWER] [LO STATUS]",

"VOL          [BUC DC][LNB REF] [BUC REF]",
"VOL [LNB DC]         [LNB REF] [BUC REF]",
"VOL [LNB DC] [BUC DC]          [BUC REF]",
"VOL [LNB DC] [BUC DC][LNB REF]          ",

"INFO      [MODULE NUMBER] [SERIAL NUMBER",
"INFO [IP]                 [SERIAL NUMBER",
"INFO [IP] [MODULE NUMBER]               "
}; /* Page 0-3 same title */


static BYTE SYS__u8UpdateUpConverter();
static BYTE SYS__u8UpdateDownConverter();
static BYTE SYS__u8UpdateVOL();



void DisplayCompanyMenu(void)
{

	strcpypgm2ram (LCDPrintBuffer[0], "                  SINGCOM               ");
	strcpypgm2ram (LCDPrintBuffer[1], "             FREQUENCY CONVERTER        ");

	PrintCompanyLCD();
}



void InitalizeMenu(void)
{
	unsigned char i;

	for (i = 0; i < 40; i++)
	{
		//LCDPrintBuffer[0][i] = ' ';
		LCDPrintBuffer[1][i] = ' ';

	}	
	CurrentMenu = MENU0;
	IPString[0] = ((AppConfig.MyIPAddr.v[0] / 100) % 10) + '0';
	IPString[1] = ((AppConfig.MyIPAddr.v[0] / 10) % 10) + '0';
	IPString[2] = ((AppConfig.MyIPAddr.v[0] / 1) % 10) + '0';
	IPString[4] = ((AppConfig.MyIPAddr.v[1] / 100) % 10) + '0';
	IPString[5] = ((AppConfig.MyIPAddr.v[1] / 10) % 10) + '0';
	IPString[6] = ((AppConfig.MyIPAddr.v[1] / 1) % 10) + '0';
	IPString[8] = ((AppConfig.MyIPAddr.v[2] / 100) % 10) + '0';
	IPString[9] = ((AppConfig.MyIPAddr.v[2] / 10) % 10) + '0';
	IPString[10] = ((AppConfig.MyIPAddr.v[2] / 1) % 10) + '0';
	IPString[12] = ((AppConfig.MyIPAddr.v[3] / 100) % 10) + '0';
	IPString[13] = ((AppConfig.MyIPAddr.v[3] / 10) % 10) + '0';
	IPString[14] = ((AppConfig.MyIPAddr.v[3] / 1) % 10) + '0';


	// Level 0 Menu 0/1/2/3: UC1/DC2/DCVOL/INFO
	// UC1	   Menu 4/5/6/7/8/9: Output Freq(Editable)/Output Power(E)/LO Status/Atten(E)/ALC(E)/Mute(E)
	// DC2	   Menu 10/11/12/13/14/15: Input Freq(Editable)/Output Power(E)/LO Status/Atten(E)/ALC(E)/Mute(E)
	// DCVOL   Menu 16/17: 18V DC(E) / 24V DC(E)
	// INFO Menu 18/19/20: IP(Editable)/MN/SN

	// Level 0 

	ConvertMenu[0].editable = FALSE;
	ConvertMenu[1].editable = FALSE;
	ConvertMenu[2].editable = FALSE;
	ConvertMenu[3].editable = FALSE;

	// UC1
	ConvertMenu[4].editable = TRUE;
	ConvertMenu[5].editable = FALSE;
	ConvertMenu[6].editable = TRUE;	
	ConvertMenu[7].editable = FALSE;
	ConvertMenu[8].editable = TRUE;
	ConvertMenu[9].editable = TRUE;
	ConvertMenu[10].editable = TRUE;

	// DC2	
	ConvertMenu[11].editable = TRUE;
	ConvertMenu[12].editable = FALSE;
	ConvertMenu[13].editable = TRUE;	
	ConvertMenu[14].editable = FALSE;
	ConvertMenu[15].editable = TRUE;
	ConvertMenu[16].editable = TRUE;
	ConvertMenu[17].editable = TRUE;

	// DCVOL

	ConvertMenu[18].editable = TRUE;
	ConvertMenu[19].editable = TRUE;
	ConvertMenu[20].editable = TRUE;
	ConvertMenu[21].editable = TRUE;
	ConvertMenu[22].editable = FALSE;
	ConvertMenu[23].editable = FALSE;
	ConvertMenu[24].editable = FALSE;
	ConvertMenu[25].editable = TRUE;
	ConvertMenu[26].editable = TRUE;	

	// INFO 
	ConvertMenu[27].editable = TRUE;
	ConvertMenu[28].editable = FALSE;
	ConvertMenu[29].editable = FALSE;

	
}



BYTE LCD__u8GetOnOff(void)
{
	if('N' == LCDPrintBuffer[1][1])
		return 1;
	else if('F' == LCDPrintBuffer[1][1])
	 	return 0;

}


BYTE LCD__u8UpdateToModule(BYTE* pu8Parameter,BYTE* pu8Data ,BYTE u8CompareFlag,BYTE* pu8SendData)
{
	if ((*pu8Data!= *pu8Parameter))
	{
		*pu8Parameter= *pu8Data;

		if(nBYTE_COMPARE == u8CompareFlag)
		{
			*pu8SendData = *pu8Data;

		}
		
		else
		{
			if(STATUS_ON == *pu8Data)
				*pu8SendData |= u8CompareFlag;
			else
				*pu8SendData &= (~u8CompareFlag);
		}
		
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}




/**************New Menu ******************/
// Level 0 Menu 0/1/2/3: UC1/DC2/DCVOL/INFO
// UC1	   Menu 4/5/6/7/8/9: Output Freq(Editable)/Output Power(E)/LO Status/Atten(E)/ALC(E)/Mute(E)
// DC2	   Menu 10/11/12/13/14/15: Input Freq(Editable)/Output Power(E)/LO Status/Atten(E)/ALC(E)/Mute(E)
// DCVOL   Menu 16/17: 18V DC(E) / 24V DC(E)
// INFO Menu 18/19/20: IP(Editable)/MN/SN

BYTE LCD_u8UpdateDisplayContent(void)
{
	unsigned char i;

	memset(LCDPrintBuffer[1],' ', nLCDLEN);
	
	stMenuDisplay[CurrentMenu].pvfPrintMenu();
}


static BYTE LCD__u8ToParentMenu()
{
   CurrentMenu = stMenuDisplay[CurrentMenu].u8UpMenu;       
}

 /* In main menus, Enter 1st child menus */  
static BYTE LCD__u8ToChildMenu()
{
    CurrentMenu = stMenuDisplay[CurrentMenu].u8DownMenu; 
  
}


 BYTE LCD__u8UpkeyEdit()
{
    // update LCD display zj: must consider on off settings too
    if ((LCDPrintBuffer[1][EditPosition] >= '0') && (LCDPrintBuffer[1][EditPosition] < '9'))
    {
        LCDPrintBuffer[1][EditPosition]++;
    }
    else if (LCDPrintBuffer[1][EditPosition] == '9')
    {
        LCDPrintBuffer[1][EditPosition] = '0';
    }
#if 1 

    else if (LCDPrintBuffer[1][EditPosition] == '-')
    {
        LCDPrintBuffer[1][EditPosition] = '+';

    }

    else if (LCDPrintBuffer[1][EditPosition] == '+')
    {
        LCDPrintBuffer[1][EditPosition] = '-';

    }				
#endif
    if(!memcmppgm2ram(LCDPrintBuffer[1],"ON ",3))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"OFF",3);

    }
    else if(!memcmppgm2ram(LCDPrintBuffer[1],"OFF",3)) 
    {
        memcpypgm2ram(LCDPrintBuffer[1],"ON ",3);

    }

    if(!memcmppgm2ram(LCDPrintBuffer[1],"Alarm",5))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"OK   ",5);

    }

    if(!memcmppgm2ram(LCDPrintBuffer[1],"ENABLED ",8))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"DISABLED",8);

    }  
    else if(!memcmppgm2ram(LCDPrintBuffer[1],"DISABLED",8))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"ENABLED ",8);

    }      
 
    EditUpdateFlag = TRUE;
    /* Blink is required here to display the updated value */
    /* No additional Menu Content updated is required  */
    /* RefreshFlag is already set to true, when entering Edit mode */
}

static BYTE LCD__u8UpkeyToParentMenu()
{
    LCD__u8ToParentMenu();
     /* Refresh flag not required to be set here, since there is menu change, SYS__vLcdKeyPadTask will refresh  */   
    
}

static BYTE LCD__u8UpKeyProcess()
{
    if (EditMode == TRUE)
    {
        LCD__u8UpkeyEdit();
    }
    else /* Go into parent menu */
    {
        LCD__u8UpkeyToParentMenu();
    }    
    
    
}

static BYTE LCD__u8DownKeyEdit()
{
    // update LCD display  zj: must consider on off settings too
    if ((LCDPrintBuffer[1][EditPosition] > '0') && (LCDPrintBuffer[1][EditPosition] <= '9'))
    {
        LCDPrintBuffer[1][EditPosition]--;
    }
    else if (LCDPrintBuffer[1][EditPosition] == '0')
    {
        LCDPrintBuffer[1][EditPosition] = '9';
    }
#if 1	
    else if (LCDPrintBuffer[1][EditPosition] == '-')
    {
        LCDPrintBuffer[1][EditPosition] = '+';
    }

    else if (LCDPrintBuffer[1][EditPosition] == '+')
    {
        LCDPrintBuffer[1][EditPosition] = '-';

    }	
#endif
    if(!memcmppgm2ram(LCDPrintBuffer[1],"ON ",3))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"OFF",3);

    }
    else if(!memcmppgm2ram(LCDPrintBuffer[1],"OFF",3)) 
    {
        memcpypgm2ram(LCDPrintBuffer[1],"ON ",3);
    }

    if(!memcmppgm2ram(LCDPrintBuffer[1],"Alarm",5))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"OK   ",5);

    }

    if(!memcmppgm2ram(LCDPrintBuffer[1],"ENABLED ",8))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"DISABLED",8);

    }  
    else if(!memcmppgm2ram(LCDPrintBuffer[1],"DISABLED",8))
    {
        memcpypgm2ram(LCDPrintBuffer[1],"ENABLED ",8);

    }   	
    
    EditUpdateFlag = TRUE;
    
    /* Blink is required here to display the updated value */
    /* No additional Menu Content updated is required  */
    /* RefreshFlag is already set to true, when entering Edit mode */
}

static BYTE LCD__u8DownkeyToChildMenu()
{
    LCD__u8ToChildMenu();    
    /* Refresh flag not required to be set here, since there is menu change, SYS__vLcdKeyPadTask will refresh  */    
}

static BYTE LCD__u8DownKeyProcess()
{
    if (EditMode == TRUE)
    {

        LCD__u8DownKeyEdit();
        
    }
    else /* Go into child menu */
    {
        LCD__u8DownkeyToChildMenu();
    }       
}

void LCDSub_vLeftMoveCursorFreq()
{
	if (EditPosition == 0)
	{
		EditPosition = 7;
	}
	else if (EditPosition == 5) //skip '.'
	{
		EditPosition = 3;
	}
	else
	{
		EditPosition--;
	}


}

void LCDSub_vLeftMoveCursorAttn()
{
	if (EditPosition == 0)
	{
		EditPosition = 3;
	}
	else if (EditPosition == 3)//skip '.'
	{
		EditPosition = 1;
	}					
	else
	{
		EditPosition--;
	}


}


void LCDSub_vLeftMoveCursorPower(void)
{
	if (EditPosition == 0)
	{
		EditPosition = 4;
	}
	else if (EditPosition == 4)
	{
		EditPosition =2;
	}		
	else
	{
		EditPosition--;
	}


}

void LCDSub_vLeftMoveCursorIP(void)
{
	if (EditPosition == 0)
	{
		EditPosition = 14;
	}

	if('.' == LCDPrintBuffer[1][EditPosition-1]) /* Skip '.' */
	{
		EditPosition -= 2;	  
	}
	else
	{
		EditPosition--;
	}


}

static BYTE LCD__u8LeftKeyMoveCursor(void)
{
    // update LCD display
    stMenuDisplay[CurrentMenu].pvfMoveCursorLeft();

	// void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
    WriteIns(0xC0 + EditPosition);
    DRI_vLCDDisplay(TRUE, TRUE, FALSE);      
}

static BYTE LCD__u8LeftKeyToLeftMenu(void)
{

        CurrentMenu = stMenuDisplay[CurrentMenu].u8LeftMenu;
   
   
 /* Refresh flag not required to be set here, since there is menu change, SYS__vLcdKeyPadTask will refresh  */       
}


static BYTE LCD__u8LeftKeyProcess(void)
{
    if (EditMode == TRUE) /* Move cursor to left */
    {
        LCD__u8LeftKeyMoveCursor();
    }
    else /* Go to previous menu */
    {
        LCD__u8LeftKeyToLeftMenu();
    }    
     
}

void LCDSub_vRightMoveCursorFreq(void)
{
	if (EditPosition == 7)
	{
		EditPosition = 0;
	}
	else if (EditPosition == 3) //'.'
	{
		EditPosition = 5;
	}
	else
	{
		EditPosition++;
	}


}

void LCDSub_vRightMoveCursorAttn(void)
{
	if (EditPosition == 3)
	{
		EditPosition = 0;
	}
	else if (EditPosition == 1)//skip '.'
	{
		EditPosition = 3;
	}					
	else
	{
		EditPosition = 1;
	}


}


void LCDSub_vRightMoveCursorPower(void)
{
	if (EditPosition == 2)
	{
		EditPosition = 4;
	}	
	else if (EditPosition == 4)
	{
		EditPosition = 0;
	}		
	else
	{
		EditPosition++;
	}


}

void LCDSub_vRightMoveCursorIP(void)
{
	if (EditPosition == 14)
	 {
		 EditPosition = 0;
	 }

	 if('.' == LCDPrintBuffer[1][EditPosition+1]) /* Skip '.' */
	 {
		 EditPosition += 2;    
	 }
	 else
	 {
		 EditPosition++;
	 }


}


static BYTE LCD__u8RightKeyMoveCursor()
{
    // update LCD display
   
	stMenuDisplay[CurrentMenu].pvfMoveCursorRight();

    // void DRI_vLCDDisplay(unsigned char displayflag, unsigned char cusorflag, unsigned char blinkflag)
    WriteIns(0xC0 + EditPosition);
    DRI_vLCDDisplay(TRUE, TRUE, FALSE);    
    
}

static BYTE LCD__u8RightKeyToRightMenu()
{
	CurrentMenu = stMenuDisplay[CurrentMenu].u8RightMenu;   
 /* Refresh flag not required to be set here, since there is menu change, SYS__vLcdKeyPadTask will refresh  */       
}

static BYTE LCD__u8RightKeyProcess()
{
    if (EditMode == TRUE)   /* Move cursor to right */
    {
        LCD__u8RightKeyMoveCursor();
    }
    else    /* Go to next menu */
    {
        LCD__u8RightKeyToRightMenu();
    }      
}

static BYTE LCD__u8EnterKeySetValue()
{ 
    
    // update LCD value to Freq/Atten/IP, TBD, update wrt power, ALC, Mute
	stMenuDisplay[CurrentMenu].pvfSetValue();

    DRI_vLCDDisplay(TRUE, FALSE, FALSE);
    EditMode = FALSE;
    EditPosition = 0;
}


 /* In main menus, Enter 1st child menus */  
static BYTE LCD__u8EnterKeyToChildMenu()
{
    LCD__u8ToChildMenu();
    /* Refresh flag not required to be set here, since there is menu change, SYS__vLcdKeyPadTask will refresh  */   
}

static BYTE LCD__u8EnterKeyToEditMode()
{
    EditMode = TRUE;
    EditPosition = 0;

    WriteIns(0xC0 + EditPosition);
    DRI_vLCDDisplay(TRUE, TRUE, FALSE);    
   //RefreshFlag = TRUE;

}

static BYTE LCD__u8EnterKeyProcess()
{   
    /* 1. In Edit mode Set new values */
    if (EditMode == TRUE) 
    {
        LCD__u8EnterKeySetValue();

		return 0;
    }


    /* 2. Not in Edit Mode */	
#if 0 	
    /* 2.1 In main menus, Enter 1st child menus */    
	/*JuZh: Not required feature */
    if(CurrentMenu <= MENU3)
    {
        LCD__u8EnterKeyToChildMenu();
		return 0;
    }
#endif	
    /* 2.2 In Child menus, go to edit mode */  
    if (ConvertMenu[CurrentMenu].editable == TRUE)
    {
        LCD__u8EnterKeyToEditMode();
		return 0;
    }
   // RefreshFlag = TRUE;       
}

static BYTE LCD__u8CancelKeyExitEditMode()
{				
    EditMode = FALSE;
    EditPosition = 0;
    DRI_vLCDDisplay(TRUE, FALSE, FALSE);        
}

/* Consider merget this function with UpkeyToParentMenu */
static BYTE LCD__u8CancelKeyToParentMenu()
{
    LCD__u8ToParentMenu();
    /* Refresh flag not required to be set here, since there is menu change, SYS__vLcdKeyPadTask will refresh  */   
}

static BYTE LCD__u8CancelKeyProcess()
{

    /* 1. In Edit mode, Cancel Edit Mode */
    if(TRUE == EditMode) 
    {
        LCD__u8CancelKeyExitEditMode();
    }
#if 0 	
    /* 2. Not in editor mode, in child menu,  Exit from child menu, Enter parent menus */
	/* JuZh: Diabled not required feature to save process time */
    else
    {
        LCD__u8CancelKeyToParentMenu();
    }
#endif 	
    
    RefreshFlag = TRUE;       
}


// Level 0 Menu 0/1/2/3: UC1/DC2/DCVOL/INFO
// UC1	   Menu 4/5/6/7/8/9: Output Freq(Editable)/Output Power(E)/LO Status/Atten(E)/ALC(E)/Mute(E)
// DC2	   Menu 10/11/12/13/14/15: Input Freq(Editable)/Output Power(E)/LO Status/Atten(E)/ALC(E)/Mute(E)
// DCVOL   Menu 16/17: 18V DC(E) / 24V DC(E)
// INFO Menu 18/19/20: IP(Editable)/MN/SN



void KeyProcessing(void)
{

	switch (PressedKey)
	{
		case UPKEY:
		{
            LCD__u8UpKeyProcess();
		}
		break;
		case DOWNKEY:
		{
            LCD__u8DownKeyProcess();
		}
		break;
		case LEFTKEY:
		{
            LCD__u8LeftKeyProcess();
		}
		break;
		case RIGHTKEY:
		{
            LCD__u8RightKeyProcess();
		}
		break;
		case ENTERKEY: //zj tbd must update freq 0000.000 attn 00.0, ALC mute, Power
		{
            LCD__u8EnterKeyProcess();
		}
		break;
		case CANCELKEY:
		{
            LCD__u8CancelKeyProcess();
		}
		break;
		default :
		break;
	}

}


//tstI2CMsg  stI2CUCMsg;
//tstI2CMsg  stI2CDCMsg;
//tstI2CMsgVOL  stI2CREFMsg;


static BYTE SYS__u8UpdateUpConverter()
{
 	DWORD tempDWORD = 0;
	WORD tempWORD = 0;
	BYTE u8TempByte = 0; 
	BOOL boReturn = FALSE;
	float fAtten;
	float fSetPwr;
	int i16frac;
    
	stI2CUCMsg.u8CtrlStatus &= nI2C_READ;
	
	I2C_Send(I2C_UPCVT_ADDR, (char*)&readflag, 1); //Notify Want to read I2C message by sending null
	boReturn = I2C_Read(I2C_UPCVT_ADDR, (char*)&stI2CUCMsg, sizeof(tstI2CMsg)); //Read I2C message
	if(!boReturn)
		return FALSE;
    
	if (((stI2CUCMsg.u8CtrlStatus & nLOCK )== 0) && (stConverter.stUp.u8AlarmStatus != 0))
	{

		stConverter.stUp.u8AlarmStatus = 0; //0 = alm
		stConverter.stUp.u8Lock = 0;
        /* If we are not at this menu now, we don't have to refresh now */
		if(((CurrentMenu == nLOSTATUS_MENU_A) || (CurrentMenu == MENU0)) && (TRUE != EditMode) ) /* if edit is on going, don't refresh the new read values */
        {
            ValueUpdateFlag = TRUE;
            stMenuDisplay[CurrentMenu].pvfPrintMenu();
        }


	

		Main_vLEDLO(OFF);
        Main_vLEDRX(OFF); 
           
	}
	else if (((stI2CUCMsg.u8CtrlStatus & nLOCK)!= 0) && (stConverter.stUp.u8AlarmStatus == 0))
	{

	    stConverter.stUp.u8AlarmStatus = 1;
		stConverter.stUp.u8Lock = 1;
        /* If we are not at this menu, we don't have to refresh now */
		if(((CurrentMenu == nLOSTATUS_MENU_A)|| (CurrentMenu == MENU0)) && (TRUE != EditMode) )
        {
			ValueUpdateFlag = TRUE;
            stMenuDisplay[CurrentMenu].pvfPrintMenu();
        }


		/* ZJ don't check down cvt anymore */
	//	if(stConverter.stDown.u8Lock == 1) //check the other cvt
		if(u8ExtLO == 1)
			Main_vLEDLO(ON);
            
            Main_vLEDRX(ON); 
        
	}


	u8TempByte = (stI2CUCMsg.u8CtrlStatus & nAGC_ON)?1:0;
	//if(u8TempByte != stConverter.stUp.u8ALC)
	{
		stConverter.stUp.u8ALC= u8TempByte;



		if(STATUS_OFF == stConverter.stUp.u8ALC)
		{
			strcpypgm2ram(sALCa,"OFF");
		}
		else
		{
			strcpypgm2ram(sALCa,"ON ");
		}

		/* If we are not at this menu now, we don't have to refresh now */
		if((CurrentMenu == nALC_MENU_A)&& (TRUE != EditMode))
		{
            ValueUpdateFlag = TRUE;
            stMenuDisplay[nALC_MENU_A].pvfPrintMenu();
        }
        
	}

	u8TempByte = (stI2CUCMsg.u8CtrlStatus & nRX_ENABLE)?1:0; //0 = disabled, 1 = enabled
	//if(u8TempByte != stConverter.stUp.u8Mute)
	{
		stConverter.stUp.u8Mute= u8TempByte;
		


		if(STATUS_OFF == stConverter.stUp.u8Mute)
		{
			strcpypgm2ram(sMutea,"DISABLED ");
			Main_vLEDTX(OFF);
		}
		else
		{
			strcpypgm2ram(sMutea,"ENABLED ");
			Main_vLEDTX(ON);
		}
       
	   if((CurrentMenu == nMUTE_MENU_A)&& (TRUE != EditMode))
	   {
		   ValueUpdateFlag = TRUE;
		   stMenuDisplay[nMUTE_MENU_A].pvfPrintMenu();
	   }

	}	


    tempDWORD = Util_u16DWORDSwap(stI2CUCMsg.unRfFreq.u32Freq);

    if((tempDWORD != stConverter.stUp.u32OutputFreq)&&(tempDWORD <=MAXRFFREQ)&&(tempDWORD>=MINRFFREQ))
    {
        stConverter.stUp.u32OutputFreq = tempDWORD;
		
		sprintf(RFFreqString1,"%04ld.%03ldMHz",(nLO_FREQ - stConverter.stUp.u32OutputFreq)/1000,(nLO_FREQ - stConverter.stUp.u32OutputFreq)%1000 );	
    
        if(((CurrentMenu == nFREQ_MENU_A) || (CurrentMenu == MENU0)) && (TRUE != EditMode))
        {
            ValueUpdateFlag = TRUE;
            stMenuDisplay[CurrentMenu].pvfPrintMenu();
        }
    }

    
    tempWORD = Util_u16ByteSwap(stI2CUCMsg.unAtten.u16Atten);

	if (stConverter.stUp.u8ALC == STATUS_OFF) /* ALC OFF, read out Attn*/
	{
	    if ((tempWORD != stConverter.stUp.u16Atten)&&(tempWORD<= MAXATTEN)&&(tempWORD>= MINATTEN))
	    {

	        stConverter.stUp.u16Atten = tempWORD;
	    
			fAtten = (float)stConverter.stUp.u16Atten /10.0f;
			
			sprintf(AttenString1,"%02d.%1d dB",(int)fAtten,(int)(fAtten*10) %10 );	

			if((CurrentMenu == nATTN_MENU_A)&& (TRUE != EditMode))
	        {
	            ValueUpdateFlag = TRUE;
	            stMenuDisplay[nATTN_MENU_A].pvfPrintMenu();
	        }

	    }
	}
	else /* ALC ON, read out Set Power*/
	{
	    if ((tempWORD != stConverter.stUp.u16SetPwr)&&(tempWORD<= MAXPOWER)&&(tempWORD>= MINPOWER))
	    {

	        stConverter.stUp.u16SetPwr = tempWORD;

			fSetPwr = ((float)stConverter.stUp.u16SetPwr -500)/10.0f;

			stConverter.stUp.fSetPwr = fSetPwr;

			i16frac = (int)(fSetPwr*10) %10;
			if(i16frac < 0)
				i16frac *= -1;
			if ((fSetPwr<0)&& (fSetPwr>-1))
			{
				sprintf(sUPC_StPwr,"-%02d.%1d dBm",(int)fSetPwr,i16frac);	
			}
			else
			{
				sprintf(sUPC_StPwr,"%+03d.%1d dBm",(int)fSetPwr,i16frac);
			}	
			if((CurrentMenu == nSTPWR_MENU_A)&& (TRUE != EditMode))
	        {
	            ValueUpdateFlag = TRUE;
	            stMenuDisplay[nSTPWR_MENU_A].pvfPrintMenu();
	        }

	    }


	}

	



	tempWORD = Util_u16ByteSwap(stI2CUCMsg.u16RdPower);
	if(((tempWORD) != stConverter.stUp.u16OutputPower)&&(tempWORD<=MAXPOWER)&&(tempWORD>=MINPOWER))
	{
		stConverter.stUp.u16OutputPower = tempWORD; ;
		stConverter.stUp.fOutputPower= ((float)tempWORD-500) ;
		stConverter.stUp.fOutputPower= stConverter.stUp.fOutputPower / 10.0f;
		fSetPwr = stConverter.stUp.fOutputPower;

		i16frac = (int)(fSetPwr*10) %10;
		if(i16frac < 0)
			i16frac *= -1;

		if ((fSetPwr<0) && (fSetPwr>-1))
		{
			sprintf(sUCPower,"-%02d.%1d dBm",(int)fSetPwr,i16frac);
		}
		else
		{
			sprintf(sUCPower,"%+03d.%1d dBm",(int)fSetPwr,i16frac);
		}	

		if(((CurrentMenu == nPOWER_MENU_A)  || (CurrentMenu == MENU0))&& (TRUE != EditMode))
        {
			ValueUpdateFlag = TRUE;
            stMenuDisplay[CurrentMenu].pvfPrintMenu();
        }
	}

	return TRUE;
          
}

static BYTE SYS__u8UpdateDownConverter()
{
 	DWORD tempDWORD = 0;
	WORD tempWORD = 0;
	BYTE u8TempByte = 0;    
	BOOL boReturn = FALSE;	
	float fAtten;
	float fSetPwr;
	int i16frac;
    
	stI2CDCMsg.u8CtrlStatus &= nI2C_READ;
	
	I2C_Send(I2C_DownCVT_ADDR, (char*)&readflag, 1);
	
	boReturn = I2C_Read(I2C_DownCVT_ADDR, (char*)&stI2CDCMsg, sizeof(tstI2CMsg));

	if(!boReturn)
		return FALSE;
    
    
	if (((stI2CDCMsg.u8CtrlStatus & nLOCK)== 0)  && (stConverter.stDown.u8AlarmStatus != 0))
	{

		stConverter.stDown.u8AlarmStatus = 0;
		stConverter.stDown.u8Lock = 0;

		if(((CurrentMenu == nLOSTATUS_MENU_B)  || (CurrentMenu == MENU1)) && (TRUE != EditMode))
		{
            ValueUpdateFlag = TRUE;
            stMenuDisplay[CurrentMenu].pvfPrintMenu();
        }

		Main_vLEDLO(OFF);
        
	}
	else if (((stI2CDCMsg.u8CtrlStatus & nLOCK)!= 0) && (stConverter.stDown.u8AlarmStatus == 0))
	{

        
		stConverter.stDown.u8AlarmStatus = 1;
		stConverter.stDown.u8Lock = 1;

		if(((CurrentMenu == nLOSTATUS_MENU_B)  || (CurrentMenu == MENU1))&& (TRUE != EditMode))
        {
			ValueUpdateFlag = TRUE;
             stMenuDisplay[CurrentMenu].pvfPrintMenu();
        }		

		if(stConverter.stUp.u8Lock == 1)
			Main_vLEDLO(ON);
       
	}


	u8TempByte = (stI2CDCMsg.u8CtrlStatus & nAGC_OFF)?0:1; 
	//if(u8TempByte != stConverter.stDown.u8ALC)
	{
		stConverter.stDown.u8ALC= u8TempByte;



		if(STATUS_OFF == stConverter.stDown.u8ALC)
		{
			strcpypgm2ram(sALCb,"OFF");
		}
		else
		{
			strcpypgm2ram(sALCb,"ON ");
		}

		if((CurrentMenu == nALC_MENU_B) && (TRUE != EditMode))
        {
			ValueUpdateFlag = TRUE;
            stMenuDisplay[nALC_MENU_B].pvfPrintMenu();
        }
        
	}
	u8TempByte = (stI2CDCMsg.u8CtrlStatus & nRX_ENABLE)?1:0;
	//if(u8TempByte != stConverter.stDown.u8Mute)
	{
		stConverter.stDown.u8Mute= u8TempByte;
		


		if(STATUS_OFF == stConverter.stDown.u8Mute)
		{
			strcpypgm2ram(sMuteb,"DISABLED");
			//Main_vLEDRX(OFF); //zj disable for Lband
		}
		else
		{
			strcpypgm2ram(sMuteb,"ENABLED ");
			//Main_vLEDRX(ON);  //zj disable for Lband
		}

		if ((CurrentMenu == nMUTE_MENU_B) && (TRUE != EditMode))
		{
            ValueUpdateFlag = TRUE;
            stMenuDisplay[nMUTE_MENU_B].pvfPrintMenu();   
        }

	}	

    
    tempDWORD = Util_u16DWORDSwap(stI2CDCMsg.unRfFreq.u32Freq);


    if ((tempDWORD != stConverter.stDown.u32InputFreq)&&(tempDWORD <=MAXRFFREQ)&&(tempDWORD>=MINRFFREQ))
    {

        
        stConverter.stDown.u32InputFreq = tempDWORD;


		sprintf(RFFreqString2,"%04ld.%03ldMHz",stConverter.stDown.u32InputFreq/1000,stConverter.stDown.u32InputFreq%1000 );	
		
        if(((CurrentMenu == nFREQ_MENU_B)  || (CurrentMenu == MENU1)) && (TRUE != EditMode))
        {
            ValueUpdateFlag = TRUE;
             stMenuDisplay[CurrentMenu].pvfPrintMenu();   
        }

          
    }

    
    
    tempWORD = Util_u16ByteSwap(stI2CDCMsg.unAtten.u16Atten);
	if (stConverter.stDown.u8ALC == STATUS_OFF) /* ALC OFF, read out Attn*/
	{

		    if ((tempWORD != stConverter.stDown.u16Atten)&&(tempWORD<= MAXATTEN)&&(tempWORD>= MINATTEN))
		    {

		        stConverter.stDown.u16Atten = tempWORD;

				fAtten = (float)stConverter.stDown.u16Atten /10.0f;
		    
				sprintf(AttenString2,"%02d.%1d dB",(int)fAtten,(int)(fAtten*10) %10 );	
				
		        if((CurrentMenu == nATTN_MENU_B) && (TRUE != EditMode))
		        {
		            ValueUpdateFlag = TRUE;
		            stMenuDisplay[nATTN_MENU_B].pvfPrintMenu(); 
		        }

		                
		    }
	}

	else /* ALC ON, read out Set Power*/
	{
	    if ((tempWORD != stConverter.stDown.u16SetPwr)&&(tempWORD<= MAXPOWER)&&(tempWORD>= MINPOWER))
	    {

	        stConverter.stDown.u16SetPwr = tempWORD;

			fSetPwr = ((float)stConverter.stDown.u16SetPwr -500)/10.0f;

			stConverter.stDown.fSetPwr = fSetPwr;

			i16frac = (int)(fSetPwr*10) %10;

			if(i16frac < 0)
				i16frac *= -1;
			
			if ((fSetPwr<0) && (fSetPwr>-1))
			{
				sprintf(sDNC_StPwr,"-%02d.%1d dBm",(int)fSetPwr,i16frac);	
			}
			else
			{
				sprintf(sDNC_StPwr,"%+03d.%1d dBm",(int)fSetPwr,i16frac);
			}
			if((CurrentMenu == nSTPWR_MENU_B)&& (TRUE != EditMode))
	        {
	            ValueUpdateFlag = TRUE;
	            stMenuDisplay[nSTPWR_MENU_B].pvfPrintMenu();
	        }

	    }

	}
	

	
	
	tempWORD = Util_u16ByteSwap(stI2CDCMsg.u16RdPower);

	if((tempWORD != stConverter.stDown.u16OutputPower)&&(tempWORD<=MAXPOWER)&&(tempWORD>=MINPOWER))
	{
		stConverter.stDown.i16OutputPower = tempWORD;
		stConverter.stDown.fOutputPower = ((float)tempWORD-500);
		stConverter.stDown.fOutputPower = stConverter.stDown.fOutputPower /10.0f;
		fSetPwr = stConverter.stDown.fOutputPower;

		i16frac = (int)(fSetPwr*10) %10;
		
		if(i16frac < 0)
			i16frac *= -1;
		

		if ((fSetPwr<0) && (fSetPwr>-1))
		{
        	sprintf(sDCPower,"-%02d.%1d dBm",(int)fSetPwr,i16frac);
		}
		else
		{
        	sprintf(sDCPower,"%+03d.%1d dBm",(int)fSetPwr,i16frac);
		}
		if(((CurrentMenu == nPOWER_MENU_B) || (CurrentMenu == MENU1))&& (TRUE != EditMode))
        {
			ValueUpdateFlag = TRUE;
            stMenuDisplay[CurrentMenu].pvfPrintMenu();
        }

	}    

	return TRUE;
}

static BYTE SYS__u8UpdateVOL()
{
	BYTE u8TempByte = 0;
	WORD u16TempWord = 0;
	BOOL boReturn = FALSE;	
    
	stI2CREFMsg.u8Status &= nI2C_READ;
	
	I2C_Send(I2C_REF_ADDR, (char*)&readflag, 1);
	boReturn = I2C_Read(I2C_REF_ADDR, (char*)&stI2CREFMsg, sizeof(tstI2CMsgVOL));

	if(!boReturn)
		return FALSE;
    
    
    u8TempByte = stI2CREFMsg.u8Status & nLNB_DC_ON ? 1:0;
    if(stConverter.stDC.u8LNB_DC_ONOFF!= u8TempByte )
    {
        stConverter.stDC.u8LNB_DC_ONOFF= u8TempByte;
		if (STATUS_ON != stConverter.stDC.u8LNB_DC_ONOFF)
		{
			s18VDC[1] ='F';
			s18VDC[2] ='F';
		}
		else
		{
			s18VDC[1] ='N';
			s18VDC[2] =' ';

		}
	    
		if(((CurrentMenu == nDC18V_MENU) || (CurrentMenu == MENU2)) && (TRUE != EditMode))
	    {	ValueUpdateFlag = TRUE;
	        stMenuDisplay[CurrentMenu].pvfPrintMenu(); 
	    }
	         
            
    }

   
    
    u8TempByte = stI2CREFMsg.u8Status & nBUC_DC_ON ? 1:0;
	
    if(stConverter.stDC.u8BUC_DC_ONOFF!= u8TempByte )
    {
        stConverter.stDC.u8BUC_DC_ONOFF= u8TempByte;

		
		if (STATUS_ON != stConverter.stDC.u8BUC_DC_ONOFF)
		{
			s24VDC[1] ='F';
			s24VDC[2] ='F';
		}
		else
		{
			s24VDC[1] ='N';
			s24VDC[2] =' ';
		
		}
		
		 if (((CurrentMenu == nDC24V_MENU) || (CurrentMenu == MENU2)) && (TRUE != EditMode))
		 {
			 ValueUpdateFlag = TRUE;
		 
			 stMenuDisplay[CurrentMenu].pvfPrintMenu();  
		 }
		
    } 



	 u8TempByte = stI2CREFMsg.u8Status & nBUC_REF_ON ? 1:0;
	 
	 if(stConverter.stDC.u8BUC_REF_ONOFF!= u8TempByte )
	 {
		 stConverter.stDC.u8BUC_REF_ONOFF= u8TempByte;
		if (STATUS_ON != stConverter.stDC.u8BUC_REF_ONOFF)
		{
			sBUCREF[1] ='F';
			sBUCREF[2] ='F';
		}
		else
		{
			sBUCREF[1] ='N';
			sBUCREF[2] =' ';
		}


		if ((CurrentMenu == nBUCREF_MENU) && (TRUE != EditMode))
		{
			ValueUpdateFlag = TRUE;

			stMenuDisplay[nBUCREF_MENU].pvfPrintMenu();	
		}

	 } 


	 u8TempByte = stI2CREFMsg.u8Status & nLNB_REF_ON ? 1:0;
	 
	 if(stConverter.stDC.u8LNB_REF_ONOFF!= u8TempByte )
	 {
		 stConverter.stDC.u8LNB_REF_ONOFF= u8TempByte;

		 if (STATUS_ON != stConverter.stDC.u8LNB_REF_ONOFF)
		 {
			 sLNBREF[1] ='F';
			 sLNBREF[2] ='F';
		 }
		 else
		 {
			 sLNBREF[1] ='N';
			 sLNBREF[2] =' ';
		 }	

		 if ((CurrentMenu == nLNBREF_MENU) && (TRUE != EditMode))
		 {
			 ValueUpdateFlag = TRUE;
		 
			 stMenuDisplay[nLNBREF_MENU].pvfPrintMenu();	
		 }	 
	 		 
		 

	 } 


	 u8TempByte = stI2CREFMsg.u8Status & nBUC_OVER_CURRENT ? 1:0;

	 if(stConverter.stDC.u8BUC_DC_OVERCurrent != u8TempByte)
	 {
	 	stConverter.stDC.u8BUC_DC_OVERCurrent = u8TempByte;

		if ((CurrentMenu == nBUCOVERI_MENU) && (TRUE != EditMode))
		{
		ValueUpdateFlag = TRUE;

		stMenuDisplay[CurrentMenu].pvfPrintMenu();  
		}

	 }

	 u8TempByte = stI2CREFMsg.u8Status & nLNB_OVER_CURRENT ? 1:0;
	 
	if(stConverter.stDC.u8LNB_DC_OVERCurrent != u8TempByte)
	{
		stConverter.stDC.u8LNB_DC_OVERCurrent = u8TempByte;
		
		if ((CurrentMenu == nLNBOVERI_MENU) && (TRUE != EditMode))
		{
			ValueUpdateFlag = TRUE;

			stMenuDisplay[CurrentMenu].pvfPrintMenu();  
		}

	}

	u8TempByte = stI2CREFMsg.u8Status & nEXT_REF ? 1:0;
	  
	if(stConverter.stDC.u8EXTREF != u8TempByte )
	{
		stConverter.stDC.u8EXTREF = u8TempByte;


		if (((CurrentMenu == nEXTREF_MENU) || (CurrentMenu == MENU2)) && (TRUE != EditMode))
		{
		ValueUpdateFlag = TRUE;

		stMenuDisplay[CurrentMenu].pvfPrintMenu();  
		}

	}  

	u16TempWord =  Util_u16ByteSwap(stI2CREFMsg.u16BUCCurrent);
	if(u16TempWord != stConverter.stDC.u16BUCCurrent )
	{
		stConverter.stDC.u16BUCCurrent = u16TempWord;

		if ((CurrentMenu == nBUCI_MENU) && (TRUE != EditMode))
		{
			ValueUpdateFlag = TRUE;

			stMenuDisplay[CurrentMenu].pvfPrintMenu();  
		}
	}


	u16TempWord = Util_u16ByteSwap(stI2CREFMsg.u16LNBCurrent);
	if(u16TempWord!= stConverter.stDC.u16LNBCurrent)
	{
		stConverter.stDC.u16LNBCurrent = u16TempWord;
		if ((CurrentMenu == nLNBI_MENU) && (TRUE != EditMode))
		{
			ValueUpdateFlag = TRUE;

			stMenuDisplay[CurrentMenu].pvfPrintMenu();  
		}		

	}

	u16TempWord =  Util_u16ByteSwap(stI2CREFMsg.u16BUCCurrentLimit);
	if(u16TempWord!= stConverter.stDC.u16BUCCurrentLimit)
	{
		stConverter.stDC.u16BUCCurrentLimit=u16TempWord;

	}


	u16TempWord =  Util_u16ByteSwap(stI2CREFMsg.u16LNBCurrentLimit);
	if(u16TempWord!= stConverter.stDC.u16LNBCurrentLimit)
	{
		stConverter.stDC.u16LNBCurrentLimit = u16TempWord;

	}


    return TRUE;
    
}

static BYTE SYS__u8UpdateLO()
{
	BYTE u8TempByte = 0;
	WORD u16TempWord = 0;
	BOOL boReturn = FALSE;	
    
	stI2CLOMsg.u8CtrlStatus&= nI2C_READ;
	
	I2C_Send(I2C_LO_ADDR, (char*)&readflag, 1);
	boReturn = I2C_Read(I2C_LO_ADDR, (char*)&stI2CLOMsg, sizeof(tstI2CMsg));

	if(!boReturn)
		return FALSE;

	if(stI2CLOMsg.u8CtrlStatus & 0x04) //bit2 is lo status
		u8ExtLO = 1;
	else
		u8ExtLO = 0;
		

}




/* ValueUpdateFlag will force refresh LCD. it must be rechecked whether current page is relavant or else, refresh is not required */
/* Data strings should be re-organized */
/* JuZh: should consider only send to one module per cycle, or else waiting time is too long */
void UpdateStatus(void)
{
	static BYTE u8Index = 0;
	

	if(0 == u8Index)
        /* Module Up Converter*/
    	SYS__u8UpdateUpConverter();
	
	else if(1 == u8Index)	
	{
		/* Module Down Converter*/
		/* Do nothing since L-Band Up converter only */
	//	SYS__u8UpdateDownConverter();

		SYS__u8UpdateLO();
	}
	else if(2 == u8Index)
	    /* Module Dc Control*/
	    SYS__u8UpdateVOL();

	u8Index++;
	
	if(u8Index>=3)
		u8Index = 0;

}

<file_sep>/Devices/DownConverter_c1.c
#include "define.h"
#include "Global.h"

#include "oled_c1.h"
#include "string.h"
#include "Util.h"


void DownCvt_vPrintFreq(void)
{
	strncpy(LCDPrintBuffer[1], RFFreqString2,sizeof(RFFreqString2));	
}


void DownCvt_vPrintPower(void)
{

	strncpy(LCDPrintBuffer[1], sDCPower,sizeof(sDCPower));

}


void DownCvt_vPrintStPwr(void)
{
	strncpy(LCDPrintBuffer[1], sDNC_StPwr,sizeof(sDNC_StPwr));
}

void DownCvt_vPrintLo(void)
{

	if (stConverter.stDown.u8AlarmStatus == 1)
	{
		strncpy(LCDPrintBuffer[1], LOStatusString1,sizeof(LOStatusString1));
	}
	else
	{
		strncpy(LCDPrintBuffer[1], LOStatusString2,sizeof(LOStatusString2));
	}

}

void DownCvt_vPrintAttn(void)
{

	strncpy(LCDPrintBuffer[1], AttenString2,sizeof(AttenString2));

}

void DownCvt_vPrintALC(void)
{
	strncpy(LCDPrintBuffer[1], sALCb,sizeof(sALCb));

}



void DownCvt_vPrintMute(void)
{	
	if(stConverter.stDown.u8Mute==1)
		strncpypgm2ram(sMuteb,"ENABLED ",8);
	else
		strncpypgm2ram(sMuteb,"DISABLED",8);

	strncpy(LCDPrintBuffer[1], sMuteb,sizeof(sMuteb));	  
}



void DownCvt_vSendI2C()
{
	stI2CDCMsg.u16GainCompenVale = 0xFFFF; /* Don't write tables */
	SetI2C(MODULE1);

}

   
 

void DownCvt_vSetFreq(void)
 {
    DWORD tempDWORD = 0;
    float fFreq = 0;
    
    fFreq = Util_u8String2NFloat((char*)&LCDPrintBuffer[1][0]);
    tempDWORD = (DWORD)(fFreq * 1000.0f);// should deduct 950 000? 950M ~ 2150M
	 
	 if ((tempDWORD != stConverter.stDown.u32InputFreq) && (tempDWORD <= MAXRFFREQ) && (tempDWORD >= MINRFFREQ))
	 {
		// stConverter.stDown.u32InputFreq = tempDWORD;
		 stI2CDCMsg.unRfFreq.u32Freq = Util_u16DWORDSwap(tempDWORD);
		 strncpy(RFFreqString2 ,LCDPrintBuffer[1], sizeof(RFFreqString2) );
         
		 DownCvt_vSendI2C();
	 }
 }

 void DownCvt_vSetAttn(void)
 {
  WORD tempWORD = 0; 

  	 if ((stConverter.stDown.u8ALC != STATUS_OFF))
		 return;
  
	 tempWORD = LCDPrintBuffer[1][0] - '0';
	 tempWORD = ((WORD)(tempWORD * 10)) + ((WORD)LCDPrintBuffer[1][1] - '0');
	 tempWORD = ((WORD)(tempWORD * 10)) + ((DWORD)LCDPrintBuffer[1][3] - '0');
     
      /* tempWORD is atten * 10 */
	 if ((tempWORD != stConverter.stDown.u16Atten) && (tempWORD <= MAXATTEN) && (tempWORD >= MINATTEN))
	 {
		 //stConverter.stDown.u16Atten = tempWORD;
		 stI2CDCMsg.unAtten.u16Atten= Util_u16ByteSwap(tempWORD);
		 memcpy(AttenString2 ,LCDPrintBuffer[1], sizeof(AttenString2) );
	
		 DownCvt_vSendI2C();
	 }
 }

 void DownCvt_vSetPower(void)
 {
/*Disabled and moved to 	DownCvt_vSetStPwr */		
  					 
 }

 void DownCvt_vSetStPwr(void)
{
	 BYTE u8TempByte =0;
	 float fTemp;
	 SHORT i16Temp;
	 WORD u16Temp;
	 

	 
	 if ((stConverter.stDown.u8ALC != STATUS_ON))
		 return;

	 
	 fTemp = Util_u8String2Float(LCDPrintBuffer[1]);
	 
	 u16Temp = (WORD)(fTemp *10 + 500);


	 if ((u16Temp!= stConverter.stDown.u16SetPwr) && (u16Temp <= MAXPOWER) && (u16Temp >= MINPOWER))
	 {
		 stConverter.stDown.u16SetPwr = u16Temp;
		 stConverter.stDown.fSetPwr= fTemp;
		 //shared 16bits of att and wtpwr
		 stI2CDCMsg.unAtten.u16Atten = Util_u16ByteSwap(u16Temp);

		 memcpy(sDNC_StPwr,LCDPrintBuffer[1], sizeof(sDNC_StPwr) );

		 
		 DownCvt_vSendI2C();
	 }	



}

 void DownCvt_vSetALC(void)
 {
	 BYTE u8TempByte =0;
	 
	 u8TempByte = LCD__u8GetOnOff();
	 
	 strncpy(sALCb,LCDPrintBuffer[1], sizeof(sALCb) );
	 
	 if(u8TempByte==0)
	 {
		 stConverter.stDown.u8ALC = OFF;
		 stI2CDCMsg.u8CtrlStatus |= nAGC_OFF;//0x02
	 }
	 else if(u8TempByte==1)
	 {
		 stConverter.stDown.u8ALC = ON;
		 stI2CDCMsg.u8CtrlStatus &= nAGC_ON;//0xFD	 
	 }	 
	 
	 DownCvt_vSendI2C();
	 

 }

 void DownCvt_vSetMute(void)
 {

    BYTE u8TempByte =0;


	if(!memcmppgm2ram(LCDPrintBuffer[1],"ENABLED",7))
		u8TempByte = 1;
	else if (!memcmppgm2ram(LCDPrintBuffer[1],"DISABLED",8))
		u8TempByte = 0;

    if(u8TempByte==0) 
    {
    	stConverter.stDown.u8Mute = 0;
        strncpy(sMuteb,LCDPrintBuffer[1], sizeof(sMuteb) );	
		stI2CDCMsg.u8CtrlStatus  &= nRX_DISABLE;

        DownCvt_vSendI2C();

		//Main_vLEDRX(u8TempByte);  //zj disable for Lband
    }
	 else if(u8TempByte==1) 
    {
    	stConverter.stDown.u8Mute = 1;
        strncpy(sMuteb,LCDPrintBuffer[1], sizeof(sMuteb) );	
		stI2CDCMsg.u8CtrlStatus |= nRX_ENABLE;

        DownCvt_vSendI2C();

		//Main_vLEDRX(u8TempByte);  //zj disable for Lband
    }	 

}

<file_sep>/Main/Main.c
/*********************************************************************

 *********************************************************************
 * FileName:        MainDemo.c
 * Dependencies:    TCPIP.h
 * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
 * Compiler:        Microchip C32 v1.05 or higher
 *					Microchip C30 v3.12 or higher
 *					Microchip C18 v3.30 or higher
 *					HI-TECH PICC-18 PRO 9.63PL2 or higher
 * Company:         Microchip Technology, Inc.
 *
 ********************************************************************/

#define THIS_IS_STACK_APPLICATION


#if defined(__18F97J60) || defined(_18F97J60)
// Include all headers for any enabled TCPIP Stack functions
#include "TCPIP Stack/TCPIP.h"
#endif

// Include functions specific to this stack application
#include "singcom.h"   //*******************************************************************************
#include "Main.h"
#include "define.h"
#include "delays.h"
#include "Global.h"
#include "timers.h"
#include "list.h"
#include "DualUART.h"

#include "Uart.h"

#include <i2c.h>
#include "i2c_c1.h"
#include "oled_c1.h"
#include "Util.h"

#define nMINUTE 60


//*******************************************************************************************************
extern char Uart0PCCmdBuf[32];
extern BYTE Uart1ReadLCDCmd;
extern BYTE Uart0ReadPCCmd;
extern BYTE Uart0PCCmdBufCount;
//**********************************************************************************************************




#if 0
const char CompanyString1[40] = {"                  SINGCOM               "};
const char CompanyString2[40] = {"               DOWN CONVERTER           "};
const char menutextstring1[] = {"[CVT. 1] "};
const char menutextstring2[] = {"[CVT. 2] "};
const char menutextstring3[] = {"[DEVICE INFO] "};
const char menutextstring4[] = {"LO "};
const char menutextstring5[] = {"RF "};
const char menutextstring6[] = {"LO "};
const char menutextstring7[] = {"Atten. "};
const char menutextstring8[] = {"IF "};
#endif
char FirmwareVersion[4] = {"100"};




tstUpDownConverter stConverter;



extern unsigned char LastMenu;
extern unsigned char CurrentMenu;




unsigned char SecondCounter = 0, MinuteCounter = 0;




// Declare AppConfig structure and some other supporting stack variables
unsigned char HardwareWDT = 1;

//#define UART_TEST // uart testing

IP_ADDR		MyStaticIPAddr;
unsigned char ostart,oend,ctemp;

#pragma udata grp5
APP_CONFIG AppConfig;

#pragma udata
unsigned char Read2[2][4];

#pragma udata gpr8
systeminfo_t systeminfo1;

#pragma udata grp10
systeminfo_t2 systeminfo2;


static TICK webPass_tmr = 0, lcd_timer = 0, keypad_tmr = 0,tiDisplay_tmr=0;

static void DRI__vEnableGlobalInt();
static void DRI__vHitWatchDog(void) ; 
static void DRI__vEnableGlobalInt();
static void DRI__vConfigSPI();

static void SYS__vStatusTask();
static void SYS__vLcdDisplayTask(void);
static void SYS__vLcdKeyPadTask(void);
static BYTE SYS__u8InitData(void);

static void HTTP_vClearWebPassword();

void main(void);


//#pragma code // Return to default code section

//#define ClearWatchDog()  { _asm CLRWDT _endasm }


/* Called in Main function */
static void DRI__vHitWatchDog(void)  
{ 
	// clear MCU watchdog 
	//ClearWatchDog();
_asm 
	CLRWDT 
_endasm

	// clear hardware watch dog, toggle RA3
//	if (HardwareWDT == 1)
	{
//		WDI_FLAG = 0;
//		HardwareWDT = 0;
	}
//	else
	{
//		WDI_FLAG = 1;
//		HardwareWDT = 1;
	}
}

//********************* FUNCTIONS ************************



/* only used in pic18f8722 */
#if 0 
static void DRI__vINT_OFF(void)
{
//	INTCONbits.GIEL = 0;
//	INTCONbits.GIEH = 0;		
//	INTCONbits.GIE = 0;
}

static void DRI__vINT_ON(void)
{
//	INTCONbits.GIEL = 1;
//	INTCONbits.GIEH = 1;		
//	INTCONbits.GIE = 1;
}
#endif


/* Enable Global interrupts */
static void DRI__vEnableGlobalInt()
{
	INTCONbits.GIEL=1;
	INTCONbits.GIEH = 1;		
	INTCONbits.GIE = 1;		//Enable global interrupts
	INTCONbits.PEIE = 1;	//Enable peripheral interrupts

}

/* Configure SPI bits and registers */
static void DRI__vConfigSPI()
{
	SCL_TRIS = 0;
	SDA_TRIS = 0;

	SSP2CON1bits.SSPEN = 0;
	SSP2STATbits.SMP = 1;
	SSP2CON1 &= 0xF0;
	SSP2CON1 |= 0x08;


}

/* Update System status from sub modules */
/* Consider to change this task to be receive interrupt triggered with update values to reduce process time*/
static void SYS__vStatusTask()
{
    
    /* Status update from Module MCUs and blick LCD */
   
    /* SecondCounter actually update every 1/3 second = 330ms*/
    if ((TickGet() - lcd_timer) >= (TICK_SECOND / 2))
    {
        /* Update status */
        UpdateStatus(); /* A time consuming task */
        lcd_timer = TickGet();
       
    }    
       
}


/* For blinking display Only*/

static void SYS__vLcdBlinkTask(void) 
{
	if ((TickGet() - tiDisplay_tmr) >= (TICK_SECOND /3 ))
		{
			
			
			tiDisplay_tmr = TickGet();
			SecondCounter++;
	
			/* Blinking effect: dislay text alternatively for odd and even seconds*/
			/* TBD can be improved here to only update blinking part , keep others static */
			// if (((SecondCounter % 4) == 1)||((SecondCounter % 4) == 2)) /* Blink 0.33s */
			// {
				// PrintLCD();
			// }
			// else
			// {
				// PrintBlinkLCD();
			// }
                /* Refresh LCD screen */
            if (ValueUpdateFlag == TRUE) /* if there is data update from I2C, whole page content has to be updated */
            {
                ValueUpdateFlag = FALSE;

                PrintNewValue();
            }
            else /* if no data update, just toggle the blink of value/'  ', or update the edit cursor position */
            {
                PrintBlinkMenuText(); 
            }
      

            

			if(SecondCounter >= nMINUTE) /* Reset from 0 */
				SecondCounter = 0;
				
			
		}	 
		


}

#if 1

/* For key processing and Menu Update*/
static void SYS__vLcdKeyPadTask(void)
{
    
    /* 1. Key press handling */
        /* KeyPressedFlag = true & PressedKey is set in ISR.c -> LowISR */
    if ((KeyPressedFlag == TRUE) && (PressedKey != NULL))
    {

#if 0
        /* There is not the very first time of key pressing */
        if (FirstPressKey == FALSE)
        {
            /* The previous handling and t2 happened  1/10 second = 100 ms before, must re-check */
            if ((TickGet() - keypad_tmr) >= (TICK_SECOND / 10))
            {
                KeyProcessing();
            }
        }
        /* This is the first time of keypress after start up, t2 is 0 in beginning */
        else
#endif	
        {
            KeyProcessing();
        }
        
        keypad_tmr = TickGet();
		
 //       FirstPressKey = FALSE; /* This is not the 1st key press */
        KeyPressedFlag = FALSE; /* Clear KeyPressedFlag from LowISR */
    }

        
         
    /* 2. Menu page update */ 
    /* Current Menu from KeyProcessing diffs from previous menu recorded,refresh is required */
    if (CurrentMenu != LastMenu)
    {
        RefreshFlag = TRUE;
        LastMenu = CurrentMenu;
    }
    
    /* Menu changed, Refresh whole LCD screen */
    if (RefreshFlag == TRUE)
    {
        RefreshFlag = FALSE;
        LCD_u8UpdateDisplayContent();
        PrintLCDWholePage();
    }
    
     /* Got New data set from keypad, Refresh only Data */
    else if(ValueUpdateFlag == TRUE)
    {
        ValueUpdateFlag = FALSE;
        PrintNewValue();
        
    }
    
      /* Up down ky pressed, Refresh only Edit position */
    else if(EditUpdateFlag == TRUE) /*Update only the Edit cursor position */
    {
        EditUpdateFlag = FALSE;
        PrintEditPosition();
    }          

        
          
}
#endif

#if 0
static void SYS__vLcdKeyPadTask(void)
{
    
    /* 1. Key press handling */
        /* KeyPressedFlag = true & PressedKey is set in ISR.c -> LowISR */
    if ((KeyPressedFlag == TRUE) && (PressedKey != NULL))
    {
       
        KeyProcessing();
       		
        FirstPressKey = FALSE; /* This is not the 1st key press */
        KeyPressedFlag = FALSE; /* Clear KeyPressedFlag from LowISR */
    }

        
        
    /* 2. Menu page update */ 
    /* Current Menu from KeyProcessing diffs from previous menu recorded,refresh is required */
    if (CurrentMenu != LastMenu)
    {
        RefreshFlag = TRUE;
        LastMenu = CurrentMenu;
    }
    
    /* Refresh LCD screen */
    if (RefreshFlag == TRUE)
    {
        RefreshFlag = FALSE;
        LCD_u8UpdateDisplayContent();
        PrintLCDWholePage();
    }
    
    if(ValueUpdateFlag == TRUE)
    {
        ValueUpdateFlag = FALSE;
        PrintNewValue();
        
    }
    
    if(EditUpdateFlag == TRUE) /*Update only the Edit cursor position */
    {
        EditUpdateFlag = FALSE;
        PrintEditPosition();
    }        
          
}
#endif


/* Flag to be used in HTTP Password Page */
static void HTTP_vClearWebPassword()
{
    
    //schedule task to clear set password result
    if (TickGet() - webPass_tmr >= TICK_SECOND*3)
    {
        webPass_tmr = TickGet();
        webpassinfo.count++;  
        if (webpassinfo.count == 2) 
        { 	   
            webpassinfo.setpass = others; 
            webpassinfo.count = 0;
        }
    }

    
}

static BYTE SYS__u8InitData(void)
{
	stConverter.stUp.u8AlarmStatus = 0;
	stConverter.stDown.u8AlarmStatus = 0;

	stConverter.stUp.u8Lock = 0;
	stConverter.stDown.u8Lock = 0;

	stConverter.stUp.u16Atten= 0;
	stConverter.stDown.u16Atten= 0;	

	stConverter.stUp.u8OutputPower= 30;
	stConverter.stDown.u8OutputPower= 30;

	stConverter.stUp.i16OutputPower= -30;
	stConverter.stDown.i16OutputPower= -30;	

	stConverter.stUp.u32OutputFreq= 950000;
	stConverter.stDown.u32InputFreq= 950000;

	stConverter.stUp.u8ALC = FALSE;
	stConverter.stDown.u8ALC = FALSE;

	stConverter.stUp.u8Mute= FALSE;
	stConverter.stDown.u8Mute= FALSE;


	stConverter.stDC.u8LNB_DC_ONOFF= FALSE;
	stConverter.stDC.u8BUC_DC_ONOFF= FALSE;

	stConverter.stDC.u8LNB_REF_ONOFF= FALSE;
	stConverter.stDC.u8BUC_REF_ONOFF= FALSE;

	stI2CUCMsg.u8CtrlStatus= 0;
	stI2CUCMsg.unRfFreq.u32Freq = 950000;
	stI2CUCMsg.unAtten.u16Atten = 0;
	stI2CUCMsg.u16RdPower = 30;
	stI2CUCMsg.u8CtrlStatus = 0x00 | nLOCK; /* Lo Lock, ALC and Mute off */

	stI2CDCMsg.u8CtrlStatus= 0;
	stI2CDCMsg.unRfFreq.u32Freq = 950000;
	stI2CDCMsg.unAtten.u16Atten = 0;
	stI2CDCMsg.u16RdPower = 30;	
	stI2CDCMsg.u8CtrlStatus = 0x00 | nLOCK; /* Lo Lock, ALC and Mute off */


	stI2CREFMsg.u8Status= 0;
	stI2CREFMsg.u16BUCCurrent = 0;
	stI2CREFMsg.u16LNBCurrent = 0;
	stI2CREFMsg.u16BUCCurrentLimit = 0;
	stI2CREFMsg.u16LNBCurrentLimit = 0;


	
	stConverter.stDC.u8EXTREF = 0;
	stConverter.stDC.u8BUC_DC_ONOFF = 0;
	stConverter.stDC.u8LNB_DC_ONOFF = 0;
	stConverter.stDC.u8BUC_REF_ONOFF= 0;
	stConverter.stDC.u8LNB_REF_ONOFF= 0;

	stConverter.stDC.u8BUC_DC_OVERCurrent = 0;

	stConverter.stDC.u8LNB_DC_OVERCurrent = 0;

	stConverter.stDC.u16BUCCurrent = 0;

	stConverter.stDC.u16LNBCurrent = 0;	

	stConverter.stDC.u16BUCCurrentLimit= 0;

	stConverter.stDC.u16LNBCurrentLimit= 0;		

		
}


void main(void)
{
	WORD tempWORD;
	float fAtt = 0;

	
#if 0 //test code
	
	SHORT i8Temp;
	WORD u16Temp;
	SHORT i16Temp = 10;

	char i8Temp2;
	WORD u16Temp2;
	char i8Temp3;
	WORD u16Temp3;
	char acTem[10] = "-30";
	char acTem2[10] = "+13";
	char acTem3[10] = "24";

					//tempWORD = stI2CUCMsg.u16RdPower;
	
					stConverter.stUp.i16OutputPower =-5;
	
	
					sprintf(sUCPower,"%+03d dB",stConverter.stUp.i16OutputPower);

					stConverter.stUp.i16OutputPower =12;
	
	
					sprintf(sUCPower,"%+03d dB",stConverter.stUp.i16OutputPower);


					stConverter.stUp.i16OutputPower =0;
	
					if ((stConverter.stUp.i16OutputPower<0)&&(stConverter.stUp.i16OutputPower>-1))
					{
						sprintf(sUCPower,"-%02d dB",stConverter.stUp.i16OutputPower);
					}
					else
					{
						sprintf(sUCPower,"%+03d dB",stConverter.stUp.i16OutputPower);
					}
					Nop();


					sprintf(AttenString2,"%02d.%1d dB",(int)fAtt, (int)(fAtt*10) %10  );
					Nop();

					fAtt = 2.3;
					sprintf(AttenString2,"%02d.%1d dB",(int)fAtt, (int)(fAtt*10) %10 );
					Nop();

					fAtt = 12.5;
					sprintf(AttenString2,"%02d.%1d dB",(int)fAtt,(int)(fAtt*10) %10 );
					Nop();

stConverter.stUp.u32OutputFreq = 1234567;
	sprintf(RFFreqString1,"%04ld.%03ldMHz",(DWORD)(stConverter.stUp.u32OutputFreq/1000),(DWORD)(stConverter.stUp.u32OutputFreq%1000) );	
     	Nop();

stConverter.stUp.u32OutputFreq = 1234;
	sprintf(RFFreqString1,"%04ld.%03ldMHz",stConverter.stUp.u32OutputFreq/1000,stConverter.stUp.u32OutputFreq%1000 );	
    		Nop();

//sscanf();

	i8Temp = Util_u8String2int8((char*)acTem);

	u16Temp = (	i8Temp *10 + 500);

	i8Temp2 = Util_u8String2int8((char*)acTem2);

	u16Temp2 = (i8Temp2 *10 + 500);

	i8Temp3 = Util_u8String2int8((char*)acTem3);

	u16Temp3 = (i8Temp3 *10 + 500);

	
#endif	

	// Initialize application specific hardware
	InitializeBoard();

	OLED_vSetKepad();  /* Set OLED Keypad  io directions */

	DRI__vConfigSPI(); /* Configure SPI bits and registers */
	
	OLED_vSetIO(); /* Set OLED control  io directions */

	Main_vLEDInit(); /* Set LED bits on main panel */
    
	SYS__u8InitData();
	
	Initial_LCD();
    
	InitalizeMenu();
    
	DisplayCompanyMenu();
	PrintCompanyLCD();

	DelayMs(1000);

	LCD_u8UpdateDisplayContent();
	DelayMs(300);
	PrintLCDWholePage();
    
	//DelayMs(200);

	RCONbits.IPEN=1;	// yt: should enable IPEN before GIE

    TickInit();

	DRI__vEnableGlobalInt();

	Main_vLEDInit();





#if defined(STACK_USE_MPFS) || defined(STACK_USE_MPFS2)
	MPFSInit();
#endif

	// Initialize Stack and application related NV variables into AppConfig.
	InitAppConfig();

	InitData();

    StackInit();

	// init I2C, 
	// set Converter A LO Frequency, Attenuation; 
	// set Converter B LO Frequency, Attenuation;

//	SetDefaultValue();

	OLED_vEnableInterrupt();/* Set OLED key interrupt bits */



	while(1)
    {
        /**
            Task priority:          Time:       Tick Period:                    Trigger:
            
            0 SYS__vLcdKeyPadTask     Long        2nd Check: TICK_SECOND/10       Interrupt(KeyPressedFlag)
            1 StackTask               Long        NULL                            Receiver INT
            2 SYS__vStatusTask        Long        TICK_SECOND/3                   Timer   
            2 StackApplications       Long        NULL                            Receiver INT
            3 SYS__vLcdBlinkTask      Middle      TICK_SECOND                     Timer
            4 HTTP_vClearWebPassword  Short       TICK_SECOND*3                   Timer
        **/

        /* If KeyPressedFlag == TRUE */
        SYS__vLcdKeyPadTask();
		
	


 
		 /* Run ethernet stack task only if PHY is connected */
		 if(MACIsLinked())
		 {
			 /* TCP/IP stack task */
			 StackTask();	
			 
			 /* This tasks invokes each of the core stack application tasks */
			 StackApplications(); 
		 }
        
        /* Run lower priority task only if Keypad is not in use */
       // if( TRUE != KeyPressedFlag )
        {

            SYS__vLcdBlinkTask();
            
            SYS__vStatusTask();

            HTTP_vClearWebPassword();

        }

#if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED)
	/*	snmp_trap_task(); */  /* SNMP is not used in this version */
#endif 

		DRI__vHitWatchDog();
    }
}



