#define    IN_BUF_SIZE      1024

typedef unsigned int  uint;
typedef unsigned int  WORD;
typedef unsigned char uchar;
typedef unsigned char BYTE;
typedef unsigned long ulong;
typedef unsigned long DWORD;

#define    NoError	    0
#define    InitPinIsOpen    0
#define    InitPinIsNotopen 1
#define    QueueIsEmpty     0
#define    QueueIsNotEmpty  1
#define    PortError	   -1
#define    DataError	   -2
#define    ParityError	   -3
#define    StopError	   -4
#define    TimeOut	   -5
#define    QueueEmpty	   -6
#define    QueueOverflow   -7
#define    PosError	   -8
#define    AddrError	   -9
#define    BlockError	   -10
#define    WriteError	   -11
#define    SegmentError    -12
#define    BaudRateError   -13
#define    CheckSumError   -14
#define    ChannelError    -15
#define    BaudrateError   -16
#define    TriggerLevelError   -17
#define    DateError    -18
#define    TimeError    -19
#define    TimeIsUp        1

#ifndef __FILE_DATA__
#define __FILE_DATA__
typedef struct  {
  unsigned mark;   /* 0x7188 -> is file */
  unsigned char fname[12];
  unsigned char year;
  unsigned char month;
  unsigned char day;
  unsigned char hour;
  unsigned char minute;
  unsigned char sec;
  unsigned long size;
  char far *addr;
  unsigned CRC;
  unsigned CRC32;
} FILE_DATA;
#endif


#define SEND_CMD(port,cmd,timeout,checksum)  SendCmdTo7000(port,cmd,checksum)
#define RECEIVE_CMD ReceiveResponseFrom7000

extern char hex_to_ascii[16];
extern unsigned long far *TimeTicks;

#ifdef __cplusplus
extern "C" {
#endif

void InitLib(void);
int  ReadInitPin(void);
void LedOff(void);
void LedOn(void);
void LedToggle(void);
void Init5DigitLed(void);
int  Show5DigitLed(int position, int value);
int  Show5DigitLedSeg(int pos, unsigned char data);

int  ReadNVRAM(int addr);
int  WriteNVRAM(int addr, int data);

int  WriteEEP(int block, int addr, int data);
int  ReadEEP(int block, int addr);
void EnableEEP(void);
void ProtectEEP(void);

void EnableWDT(void);
void RefreshWDT(void);
void DisableWDT(void);

int FlashReadId(void);
int FlashWrite(unsigned int seg, unsigned int offset, char data);
int FlashErase(unsigned int seg);
int FlashRead(unsigned int seg, unsigned int offset);

void Delay(unsigned ms);
void Delay_1(unsigned ms);

int Is7188xb(void);
int Getch(void);
int Kbhit(void);
int Ungetch(int key);
void Putch(int data);
void Puts(char *str);
int Print(char *fmt,...);

int ascii_to_hex(char ascii);
int SendCmdTo7000(int iPort, unsigned char *cCmd, int iChksum);
int ReceiveResponseFrom7000(int iPort, unsigned char *cCmd, long lTimeout, int iChksum);

void far *_MK_FP(unsigned segment,unsigned offset);
int IsResetByWatchDogTimer(void);
int IsResetByPowerOff(void); /* for bios date 12/12/98 or later */

int Show5DigitLedWithDot(int pos, int data);
void Set5DigitLedTestMode(int mode);
void Set5DigitLedIntensity(int mode);
void Disable5DigitLed(void);
void Enable5DigitLed(void);
unsigned GetLibVersion(void);

/* 01/07/1999  Add Timer function */
int TimerOpen(void);
int TimerClose(void);
void TimerResetValue(void);
unsigned long TimerReadValue(void);
void DelayMs(unsigned t);
int StopWatchReset(int channel);
int StopWatchStart(int channel);
int StopWatchStop(int channel);
int StopWatchPause(int channel);
int StopWatchContinue(int channel);
int StopWatchReadValue(int channel,unsigned long *value);
int CountDownTimerStart(int channel,unsigned long count);
int CountDownTimerReadValue(int channel,unsigned long *value);
void InstallUserTimer(void (*fun)(void));
void InstallUserTimer1C(void (*fun)(void));

/* 12/29/1999 */
/*
  Add functions for ReadOnly file system of 7188R/7188X
*/
int GetFileNo(void);
int GetFileName(int no,char *fname);
char far *GetFilePositionByNo(int no);
char far *GetFilePositionByName(char *fname);
FILE_DATA far *GetFileInfoByNo(int no);           /* 04/11/2000 */
FILE_DATA far *GetFileInfoByName(char *fname);    /* 04/11/2000 */

/*
 02/15/2000
 Add functions for system DATE/TIME
*/
void GetTime(int *hour,int *minute,int *sec);
int SetTime(int hour,int minute,int sec);
void GetDate(int *year,int *month,int *day);
int SetDate(int year,int month,int day);

/*
 03/09/2000
 Add functions for DIO pins
*/
void SetDio4Dir(int dir);
void SetDio4High(void);
void SetDio4Low(void);
int GetDio4(void);
void SetDio9Dir(int dir);
void SetDio9High(void);
void SetDio9Low(void);
int GetDio9(void);
void SetDio14Dir(int dir);
void SetDio14High(void);
void SetDio14Low(void);
int GetDio14(void);
void SetTi1Dir(int dir);
void SetTi1High(void);
void SetTi1Low(void);
int GetTi1(void);
void SetTo1Dir(int dir);
void SetTo1High(void);
void SetTo1Low(void);
int GetTo1(void);
void SetTi0Dir(int dir);
void SetTi0High(void);
void SetTi0Low(void);
int GetTi0(void);
void SetTo0Dir(int dir);
void SetTo0High(void);
void SetTo0Low(void);
int GetTo0(void);

void SetDo1On(void);
void SetDo1Off(void);
int GetDo1(void);
int GetDi1(void);

void ClockHighLow(void);
void ClockHigh(void);
void ClockLow(void);

/*
 03/15/2000
 Add function Scanf ...
*/
int LineInput(char *buf,int maxlen); /* input one line from StdInput */
void ResetScanBuffer(void); /* Set Scanf to use default buffer(maxlen=80) */
void SetScanBuffer(unsigned char *buf,int len); /* Set user's buffer for Scanf*/
int Scanf(char *fmt, ...); /* like C's scanf */

/*
 10/16/2000  start Ver 1.17
 Add new EEPROM functions
*/
void EE_WriteEnable(void);
void EE_WriteProtect(void);
int EE_RandomRead(int Block,int Addr);
int EE_ReadNext(int Block);
int EE_MultiRead(int StartBlock,int StartAddr,int no,char *databuf);
int EE_RandomWrite(int Block,int Addr,int Data);
int EE_MultiWrite(int Block,int Addr,int no,char *Data);

/* 
  Functions for all com ports
*/
int GetComportNumber(void);

int InstallCom(int port, unsigned long baud, int data, int parity);  /* for COM1/COM2 only */
int _InstallCom(int port, unsigned long baud, int data, int parity,int stop); /* for ALL COM PORTS */
int RestoreCom(int port);
int IsCom(int port);
int ReadCom(int port);
int ToCom(int port, int data);
int ToComStr(int port, char *str);
int ToComBufn(int port,char *buf,int no);
int ClearCom(int port);
int WaitTransmitOver(int port);
int ReadComn(int port,unsigned char *buf,int n);
/* Function: ReadComn/ReadCom1n/ReadCom2n/...

   Read at max n byte data from COM port input buffer,no wait.
   return the byte number read in.(0: no data.)
*/
int SetDataFormat(int port,int databit,int parity,int stopbit);
int DataSizeInCom(int port);
int ClearTxBuffer(int port);
int printCom(int port,char *fmt,...);
int IsTxBufEmpty(int port);
int SetBaudrate(int port,unsigned long baud);
int IsDetectBreak(int port);
int SendBreak(int port,unsigned timems);

/* 
  Functions for COM1
*/
int InstallCom1(unsigned long baud, int data, int parity);
int RestoreCom1(void);
int IsCom1(void);
int ReadCom1(void);
int ToCom1(int data);
int ToCom1Bufn(char *buf,int no);
int ToCom1Str(char *str);
int ClearCom1(void);
int WaitTransmitOver1(void);
void SetRtsActive1(void);
void SetRtsInactive1(void);
int GetCtsStatus1(void);
int ReadCom1n(unsigned char *buf,int n);
int SetDataFormat1(int databit,int parity,int stopbit);
int PushDataToCom1(int data);
/* Function: PushDataToCom1/PushDataToCom2

   Push one byte data back to COM1/2 input buffer.
   So later use ReadCom1 will read the data in.
   This function like Ungetch().
*/
void SetCom1Timeout(unsigned t);
/*
  Set the timeout period for function ToCom1/ToCom2, unit is ms.
  ToCom1Str/ToCom1Bufn/ToCom2Str/ToCom2Bufn will also use this setting.
  The default setting is 3ms.
*/
int DataSizeInCom1(void);
void SetRtsActive1(void);
int printCom1(char *fmt,...);
int IsTxBufEmpty1(void);
int IsCom1OutBufEmpty(void);
int SetBaudrate1(unsigned long baud);
void SendBreak1(unsigned timems);
int IsDetectBreak1(void);
void ClearTxBuffer1(void);


/* 
  Functions for COM2
*/
int InstallCom2(unsigned long baud, int data, int parity);
int RestoreCom2(void);
int IsCom2(void);
int ReadCom2(void);
int ToCom2(int data);
int ToCom2Bufn(char *buf,int no);
int ToCom2Str(char *str);
int ClearCom2(void);
int WaitTransmitOver2(void);
int ReadCom2n(unsigned char *buf,int n);
int SetDataFormat2(int databit,int parity,int stopbit);
int PushDataToCom2(int data);
void SetCom2Timeout(unsigned t);
int DataSizeInCom2(void);
void SetRtsInactive1(void);
int printCom2(char *fmt,...);
int GetCtsStatus(void);
int IsTxBufEmpty2(void);
int IsCom2OutBufEmpty(void);
int SetBaudrate2(unsigned long baud);
void SendBreak2(unsigned timems);
int IsDetectBreak2(void);
void ClearTxBuffer2(void);

/*
 09/14/2001
 Add new variable for system hardware serial number
*/
extern unsigned char far const *SystemSerialNumber;

/*
 Functions for COM3
  COM3 for X503/X504/X505/X506/X507 
*/
#define InstallCom3	InstallCom_3
#define RestoreCom3	RestoreCom_3
#define IsCom3		IsCom_3
#define ToCom3		ToCom_3
#define ToCom3Str	ToComStr_3
#define ToCom3Bufn	ToComBufn_3
#define printCom3	printCom_3
#define ClearTxBuffer3	ClearTxBuffer_3
#define SetCom3FifoTriggerLevel		SetComFifoTriggerLevel_3
#define SetBaudrate3	SetBaudrate_3
#define ReadCom3	ReadCom_3
#define ClearCom3	ClearCom_3
#define DataSizeInCom3  DataSizeInCom_3
#define WaitTransmitOver3	WaitTransmitOver_3
#define IsTxBufEmpty3		IsTxBufEmpty_3
#define IsCom3OutBufEmpty	IsComOutBufEmpty_3
#define ReadCom3n		ReadComn_3
#define SetDataFormat3		SetDataFormat_3
#define SetRtsActive3		SetRtsActive_3
#define SetRtsInactive3		SetRtsInactive_3
#define GetCtsStatus3		GetCtsStatus_3

int InstallCom_3(unsigned long baud, int data, int parity, int stop);
int RestoreCom_3(void);
int IsCom_3(void);
int ToCom_3(int data);
int ToComStr_3(char *str);
int ToComBufn_3(char *buf,int no);
int printCom_3(char *fmt,...);
void ClearTxBuffer_3(void);
int SetComFifoTriggerLevel_3(int level);
int SetBaudrate_3(unsigned long baud);
int ReadCom_3(void);
int ClearCom_3(void);
int DataSizeInCom_3(void);
int WaitTransmitOver_3(void);
int IsTxBufEmpty_3(void);
int IsComOutBufEmpty_3(void);
int SetDataFormat_3(int databit,int parity,int stopbit);
int ReadComn_3(unsigned char *buf,int n);
void SetRtsActive_3(void);
void SetRtsInactive_3(void);
int GetCtsStatus_3(void);

/*
 Functions for COM4
  COM4 for X504/X505/X506 
*/
#define InstallCom4	InstallCom_4
#define RestoreCom4	RestoreCom_4
#define IsCom4		IsCom_4
#define ToCom4		ToCom_4
#define ToCom4Str	ToComStr_4
#define ToCom4Bufn	ToComBufn_4
#define printCom4	printCom_4
#define ClearTxBuffer4	ClearTxBuffer_4
#define SetCom4FifoTriggerLevel		SetComFifoTriggerLevel_4
#define SetBaudrate4	SetBaudrate_4
#define ReadCom4	ReadCom_4
#define ClearCom4	ClearCom_4
#define DataSizeInCom4  DataSizeInCom_4
#define WaitTransmitOver4	WaitTransmitOver_4
#define IsTxBufEmpty4		IsTxBufEmpty_4
#define IsCom4OutBufEmpty	IsComOutBufEmpty_4
#define ReadCom4n		ReadComn_4
#define SetDataFormat4		SetDataFormat_4
#define SetRtsActive4		SetRtsActive_4
#define SetRtsInactive4		SetRtsInactive_4
#define GetCtsStatus4		GetCtsStatus_4

int InstallCom_4(unsigned long baud, int data, int parity, int stop);
int RestoreCom_4(void);
int IsCom_4(void);
int ToCom_4(int data);
int ToComStr_4(char *str);
int ToComBufn_4(char *buf,int no);
int printCom_4(char *fmt,...);
void ClearTxBuffer_4(void);
int SetComFifoTriggerLevel_4(int level);
int SetBaudrate_4(unsigned long baud);
int ReadCom_4(void);
int ClearCom_4(void);
int DataSizeInCom_4(void);
int WaitTransmitOver_4(void);
int IsTxBufEmpty_4(void);
int IsComOutBufEmpty_4(void);
int SetDataFormat_4(int databit,int parity,int stopbit);
int ReadComn_4(unsigned char *buf,int n);
void SetRtsActive_4(void);
void SetRtsInactive_4(void);
int GetCtsStatus_4(void);

/*
 Functions for COM5
  COM5 for X505/X506 
*/
#define InstallCom5	InstallCom_5
#define RestoreCom5	RestoreCom_5
#define IsCom5		IsCom_5
#define ToCom5		ToCom_5
#define ToCom5Str	ToComStr_5
#define ToCom5Bufn	ToComBufn_5
#define printCom5	printCom_5
#define ClearTxBuffer5	ClearTxBuffer_5
#define SetCom5FifoTriggerLevel		SetComFifoTriggerLevel_5
#define SetBaudrate5	SetBaudrate_5
#define ReadCom5	ReadCom_5
#define ClearCom5	ClearCom_5
#define DataSizeInCom5  DataSizeInCom_5
#define WaitTransmitOver5	WaitTransmitOver_5
#define IsTxBufEmpty5		IsTxBufEmpty_5
#define IsCom5OutBufEmpty	IsComOutBufEmpty_5
#define ReadCom5n		ReadComn_5
#define SetDataFormat5		SetDataFormat_5
#define SetRtsActive5		SetRtsActive_5
#define SetRtsInactive5		SetRtsInactive_5
#define GetCtsStatus5		GetCtsStatus_5

int InstallCom5(unsigned long baud, int data, int parity, int stop);
int RestoreCom5(void);
int IsCom5(void);
int ToCom5(int data);
int ToCom5Str(char *str);
int ToCom5Bufn(char *buf,int no);
int printCom5(char *fmt,...);
void ClearTxBuffer5(void);
int SetCom5FifoTriggerLevel(int level);
int SetBaudrate5(unsigned long baud);
int ReadCom5(void);
int ClearCom5(void);
int DataSizeInCom5(void);
int WaitTransmitOver5(void);
int IsTxBufEmpty5(void);
int IsCom5OutBufEmpty(void);
int SetDataFormat5(int databit,int parity,int stopbit);
int ReadCom5n(unsigned char *buf,int n);
void SetRtsActive5(void);
void SetRtsInactive5(void);
int GetCtsStatus5(void);
/*
  [11/01/2001] Add function for COM6/7/8 on X506 
*/
/*
 Functions for COM6
  COM6 for X506 
*/
#define InstallCom36	InstallCom_36
#define RestoreCom36	RestoreCom_36
#define IsCom6		IsCom_6
#define ToCom6		ToCom_6
#define ToCom6Str	ToComStr_6
#define ToCom6Bufn	ToComBufn_6
#define printCom6	printCom_6
#define ClearTxBuffer6	ClearTxBuffer_6
#define SetCom6FifoTriggerLevel		SetComFifoTriggerLevel_6
#define SetBaudrate6	SetBaudrate_6
#define ReadCom6	ReadCom_6
#define ClearCom6	ClearCom_6
#define DataSizeInCom6  DataSizeInCom_6
#define WaitTransmitOver6	WaitTransmitOver_6
#define IsTxBufEmpty6		IsTxBufEmpty_6
#define IsCom6OutBufEmpty	IsComOutBufEmpty_6
#define ReadCom6n		ReadComn_6
#define SetDataFormat6		SetDataFormat_6
#define SetRtsActive6		SetRtsActive_6
#define SetRtsInactive6		SetRtsInactive_6
#define GetCtsStatus6		GetCtsStatus_6

int InstallCom_36(unsigned long baud, int data, int parity, int stop);
int RestoreCom_36(void);
int IsCom_6(void);
int ToCom_6(int data);
int ToComStr_6(char *str);
int ToComBufn_6(char *buf,int no);
int printCom_6(char *fmt,...);
void ClearTxBuffer_6(void);
int SetComFifoTriggerLevel_6(int level);
int SetBaudrate_6(unsigned long baud);
int ReadCom_6(void);
int ClearCom_6(void);
int DataSizeInCom_6(void);
int WaitTransmitOver_6(void);
int IsTxBufEmpty_6(void);
int IsComOutBufEmpty_6(void);
int ReadCom6n(unsigned char *buf,int n);
int SetDataFormat6(int databit,int parity,int stopbit);

/* COM6 WITHOUT RTS/CTS SIGNAL
void SetRtsActive6(void);
void SetRtsInactive6(void);
int GetCtsStatus6(void);
*/

/*
 Functions for COM7
  COM7 for X506 
*/
#define InstallCom47	InstallCom_47
#define RestoreCom47	RestoreCom_47
#define IsCom7		IsCom_7
#define ToCom7		ToCom_7
#define ToCom7Str	ToComStr_7
#define ToCom7Bufn	ToComBufn_7
#define printCom7	printCom_7
#define ClearTxBuffer7	ClearTxBuffer_7
#define SetCom7FifoTriggerLevel		SetComFifoTriggerLevel_7
#define SetBaudrate7	SetBaudrate_7
#define ReadCom7	ReadCom_7
#define ClearCom7	ClearCom_7
#define DataSizeInCom7  DataSizeInCom_7
#define WaitTransmitOver7	WaitTransmitOver_7
#define IsTxBufEmpty7		IsTxBufEmpty_7
#define IsCom7OutBufEmpty	IsComOutBufEmpty_7
#define ReadCom7n		ReadComn_7
#define SetDataFormat7		SetDataFormat_7
/* COM7 without CTS/RTS
#define SetRtsActive7		SetRtsActive_7
#define SetRtsInactive7		SetRtsInactive_7
#define GetCtsStatus7		GetCtsStatus_7
*/
int InstallCom_47(unsigned long baud, int data, int parity, int stop);
int RestoreCom_47(void);
int IsCom_7(void);
int ToCom_7(int data);
int ToComStr_7(char *str);
int ToComBufn_7(char *buf,int no);
int printCom_7(char *fmt,...);
void ClearTxBuffer_7(void);
int SetComFifoTriggerLevel_7(int level);
int SetBaudrate_7(unsigned long baud);
int ReadCom_7(void);
int ClearCom_7(void);
int DataSizeInCom_7(void);
int WaitTransmitOver_7(void);
int IsTxBufEmpty_7(void);
int IsComOutBufEmpty_7(void);
int ReadComn_7(unsigned char *buf,int n);
int SetDataFormat_7(int databit,int parity,int stopbit);

/*
 Functions for COM8
  COM8 for X506 
*/
#define InstallCom58	InstallCom_58
#define RestoreCom58	RestoreCom_58
#define IsCom8		IsCom_8
#define ToCom8		ToCom_8
#define ToCom8Str	ToComStr_8
#define ToCom8Bufn	ToComBufn_8
#define printCom8	printCom_8
#define ClearTxBuffer8	ClearTxBuffer_8
#define SetCom8FifoTriggerLevel		SetComFifoTriggerLevel_8
#define SetBaudrate8	SetBaudrate_8
#define ReadCom8	ReadCom_8
#define ClearCom8	ClearCom_8
#define DataSizeInCom8  DataSizeInCom_8
#define WaitTransmitOver8	WaitTransmitOver_8
#define IsTxBufEmpty8		IsTxBufEmpty_8
#define IsCom8OutBufEmpty	IsComOutBufEmpty_8
#define ReadCom8n		ReadComn_8
#define SetDataFormat8		SetDataFormat_8
/* COM8 without CTS/RTS
#define SetRtsActive8		SetRtsActive_8
#define SetRtsInactive8		SetRtsInactive_8
#define GetCtsStatus8		GetCtsStatus_8
*/
int InstallCom_58(unsigned long baud, int data, int parity, int stop);
int RestoreCom_58(void);
int IsCom_8(void);
int ToCom_8(int data);
int ToComStr_8(char *str);
int ToComBufn_8(char *buf,int no);
int printCom_8(char *fmt,...);
void ClearTxBuffer_8(void);
int SetComFifoTriggerLevel_8(int level);
int SetBaudrate_8(unsigned long baud);
int ReadCom_8(void);
int ClearCom_8(void);
int DataSizeInCom_8(void);
int WaitTransmitOver_8(void);
int IsTxBufEmpty_8(void);
int IsComOutBufEmpty_8(void);
int ReadComn_8(unsigned char *buf,int n);
int SetDataFormat_8(int databit,int parity,int stopbit);


#ifdef __cplusplus
}
#endif
