#define    IN_BUF_SIZE      1024

#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; /* 02/16/2001 add, for get System Time Ticks. */

#ifdef __cplusplus
extern "C" {
#endif

void InitLib(void);
int  InstallCom(int port, unsigned long baud, int data, int parity);
int  InstallCom1(unsigned long baud, int data, int parity);
int  InstallCom2(unsigned long baud, int data, int parity);
int  RestoreCom(int port);
int  RestoreCom1(void);
int  RestoreCom2(void);
int  IsCom(int port);
int  IsCom1(void);
int  IsCom2(void);
int  ReadCom(int port);
int  ReadCom1(void);
int  ReadCom2(void);
int  ToCom(int port, int data);
int  ToCom1(int data);
int  ToCom2(int data);
int ToComBufn(int port,char *buf,int no);
int ToCom1Bufn(char *buf,int no);
int ToCom2Bufn(char *buf,int no);
int  ToComStr(int port, char *str);
int  ToCom1Str(char *str);
int  ToCom2Str(char *str);
int  ClearCom(int port);
int  ClearCom1(void);
int  ClearCom2(void);
int  WaitTransmitOver(int port);
int  WaitTransmitOver1(void);
int  WaitTransmitOver2(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);
void DelayMs(unsigned int time);

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 Is7188x(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));
int DataSizeInCom(int port);
int DataSizeInCom1(void);
int DataSizeInCom2(void);
/* New for Ver 1.13 */
/* 07-23-1999 add 4 function for COM1 /DTR,/RTS output pins */
void SetRtsActive(void);
void SetRtsInactive(void);

/* [New for Ver 1.14] */
/**** 07/27/1999 add 5 function for COM1/2/3/4 formated output ****/
int printCom(int port,char *fmt,...);
int printCom1(char *fmt,...);
int printCom2(char *fmt,...);
/* 08/06/1999 add 2 function for COM1 CTS,DSR input pins */
int GetCtsStatus(void);

/* 08/25/1999 add 5 function for COM1-4 */
int IsTxBufEmpty(int port);
int IsTxBufEmpty1(void);
int IsTxBufEmpty2(void);
int IsCom1OutBufEmpty(void);
int IsCom2OutBufEmpty(void);

/* 10/19/1999 add 5 function for COM1/2/3/4 change baudrate */
int SetBaudrate(int port,unsigned long baud);
int SetBaudrate1(unsigned long baud);
int SetBaudrate2(unsigned long baud);

/* 10/20/1999 */
/* (1) add 10 function for COM1/2/3/4 send/detect BREAK signal */
int SendBreak(int port,unsigned timems);
void SendBreak1(unsigned timems);
void SendBreak2(unsigned timems);
int IsDetectBreak(int port);
int IsDetectBreak1(void);
int IsDetectBreak2(void);

/* (2) Add 3 functions for clear COM1/2/3/4 output buffer */
int ClearTxBuffer(int port);
void ClearTxBuffer1(void);
void ClearTxBuffer2(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 SetDo1High(void);
void SetDo1Low(void);
int GetDo1(void);
void SetDo2High(void);
void SetDo2Low(void);
int GetDo2(void);
void SetDo3High(void);
void SetDo3Low(void);
int GetDo3(void);
int GetDi2(void);
int GetDi3(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 */

/*
 06/09/2000
 Add function for COM3/COM4
  COM3 for 7522/7523 only
  COM4 for 7523 only
*/

int InstallCom3(unsigned long baud, int data, int parity, int stop);
int RestoreCom3(void);
int IsCom3(void);
int ToCom3(int data);
int ToCom3Str(char *str);
int ToCom3Bufn(char *buf,int no);
int printCom3(char *fmt,...);
void ClearTxBuffer3(void);
int SetCom3FifoTriggerLevel(int level);
int SetBaudrate3(unsigned long baud);
int ReadCom3(void);
int ClearCom3(void);
int DataSizeInCom3(void);
int WaitTransmitOver3(void);
int IsTxBufEmpty3(void);
int IsCom3OutBufEmpty(void);

int InstallCom4(unsigned long baud, int data, int parity, int stop);
int RestoreCom4(void);
int IsCom4(void);
int ToCom4(int data);
int ToCom4Str(char *str);
int ToCom4Bufn(char *buf,int no);
int printCom4(char *fmt,...);
void ClearTxBuffer4(void);
int SetCom4FifoTriggerLevel(int level);
int SetBaudrate4(unsigned long baud);
int ReadCom4(void);
int ClearCom4(void);
int DataSizeInCom4(void);
int WaitTransmitOver4(void);
int IsTxBufEmpty4(void);
int IsCom4OutBufEmpty(void);
int IsCom2OutBufEmpty(void);

/*
  06/15/2000
 Add function for COM3
  COM3 for 7522/7523 only
*/
void SetRtsActive3(void);
void SetRtsInactive3(void);
int GetCtsStatus3(void);
int _InstallCom(int port, unsigned long baud, int data, int parity,int stop);

/*
  06/16/2000
 Add function for COM1
*/
void SetRtsActive1(void);
void SetRtsInactive1(void);
int GetCtsStatus1(void);

/*
 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);

/*
 02/16/2001  Ver 1.17
 Add 3 new Flash functions
*/
unsigned char FlashReadByte(unsigned int seg, unsigned int offset); /* read one byte (char) */
unsigned int FlashReadWord(unsigned int seg, unsigned int offset);  /* read one word (unsigned int) */
unsigned long FlashReadDword(unsigned int seg, unsigned int offset);/* read one dword (unsigned long int) */

/*
 10/02/2001  Ver 1.17
 Add 1 new function
*/
int ReceiveResponseFrom7000_1(int iPort, unsigned char *cCmd, long lTimeout, int iChksum);
/*
  iPort: 1-4 for COM1-COM4.
  cCmd: buffer to stroe the response.
  lTimeout: unit is ms.
  iChksum: 0 : disable checksum. 1: enable checksum.
*/
#ifdef __cplusplus
}
#endif
