/******************************************************************************
//                                                                           //
//  Project: Software Interface for COM-RFID-DIO4+4-AIO4 Devices             //
//                                                                           //
//  CGC Instruments, (c) 2009, Version 1.00. All rights reserved.            //
//                                                                           //
//  Definition for COM-RFID DLL Routines                                     //
//                                                                           //
******************************************************************************/

#ifndef __COM_RFID_H__
#define __COM_RFID_H__


#ifdef __cplusplus
extern "C" {
#endif


#define MAX_PORT 8 /* Maximum PortNumber */

/*
	The listed routines accept the number of the port PortNumber in the range 0..MAX_PORT-1,
	thus maximum MAX_PORT independent communication channels (ports) can be defined

	Each communication channel must be opened before the first usage. The Open routine assigns a specified COMx port to the channel.
	The ComNumber specifies the number of the port - 1 stands for COM1, 2 for COM2, etc.

	If necessary, the channel may be closed and assigned by the Open routine to a new COMx port.
	The channels are closed automatically at the end of the program, the main program does not need to take care about it.

	The return values of the routines are the following defines COM_RFID_ERR_XXX
		 0: routine finished without any error, COM_RFID_CheckAutoInput returns 0 if all notification messages could be decoded
		 1: COM_RFID_CheckAutoInput returns 1 if the received command does not match any notification message
		<0: errors COM_RFID_ERR_XXX

	The last error code can be obtained by calling COM_RFID_State.
	The routine COM_RFID_ErrorMessage returns a pointer to a zero-terminated character string or NULL in case of a failure.

	If a communication error occurred, the error code can be red again by COM_RFID_IO_State,
	The routine COM_RFID_IO_ErrorMessage provides the corresponding error message.
*/

#define COM_RFID_ERR_PORT_RANGE      -1 /* PortNumber out of range */
#define COM_RFID_ERR_MEMORY_ALLOC    -2 /* Memory for the port data could not be allocated */
#define COM_RFID_ERR_OPEN            -3 /* Error opening the port */
#define COM_RFID_ERR_CLOSE           -4 /* Error closing the port  */
#define COM_RFID_ERR_PURGE           -5 /* Error purging the port  */
#define COM_RFID_ERR_COMMAND_SEND    -6 /* Error sending command */
#define COM_RFID_ERR_COMMAND_RECEIVE -7 /* Error receiving command */
#define COM_RFID_ERR_COMMAND_WRONG   -8 /* Wrong command received */
#define COM_RFID_ERR_ARGUMENT_WRONG  -9 /* Wrong argument received */

#define AUTO_AIN_0     1 /* Automatic analog input data for channel 0 received */
#define AUTO_AIN_1     2 /* Automatic analog input data for channel 1 received */
#define AUTO_AIN_2     4 /* Automatic analog input data for channel 2 received */
#define AUTO_AIN_3     8 /* Automatic analog input data for channel 3 received */
#define AUTO_AIN   (AUTO_AIN_0 | AUTO_AIN_1 | AUTO_AIN_2 | AUTO_AIN_3) /* Automatic analog input data for any channel received */
#define AUTO_DIN      16 /* Automatic digital input data received */
#define AUTO_KEYPAD   32 /* Automatic keypad data received */
#define AUTO_RFID     64 /* Automatic RFID data received */
int FAR _export pascal COM_RFID_Open  (WORD PortNumber, WORD ComNumber);      /* Open the port  */
int FAR _export pascal COM_RFID_Close (WORD PortNumber);                      /* Close the port */

int FAR _export pascal COM_RFID_Purge        (WORD PortNumber);               /* Clear data buffers for the port */
int FAR _export pascal COM_RFID_Buffer_State (WORD PortNumber, BOOL & empty); /* Return true if the input data buffer of the device is empty */
int FAR _export pascal COM_RFID_Device_Purge (WORD PortNumber, BOOL & empty); /* Clear output data buffer of the device, return value as COM_RFID_Buffer_State */

int FAR _export pascal COM_RFID_CheckAutoInput (WORD PortNumber, WORD & CommandMask); /* Check for new automatic notification data */
int FAR _export pascal COM_RFID_GetAutoMask    (WORD PortNumber, WORD & CommandMask); /* Get the mask of the last automatic notification data */

/* Analog I/O */
#define AIN_0           0 /* Analog input channel 0 */
#define AIN_1           1 /* Analog input channel 1 */
#define AIN_2           2 /* Analog input channel 2 */
#define AIN_3           3 /* Analog input channel 3 */
#define AIN_NUM_CHANNEL 4 /* Number of analog input channels */
#define AIO_REF      2500 /* Analog reference volatge in mV */
int FAR _export pascal COM_RFID_EnableAnalogInput  (WORD PortNumber, BOOL Enable);                  /* Enable analog input */
int FAR _export pascal COM_RFID_GetAnalogInput     (WORD PortNumber, WORD Channel, WORD & Voltage); /* Get analog input voltage in mV */
int FAR _export pascal COM_RFID_AutoAnalogInput    (WORD PortNumber, WORD Channel);                 /* Set automatic transmission of analog input voltage */
int FAR _export pascal COM_RFID_GetLastAnalogInput (WORD PortNumber, WORD Channel, WORD & Voltage); /* Get last automatically transmitted analog input voltage in mV */

int FAR _export pascal COM_RFID_EnableAnalogOutput (WORD PortNumber, BOOL   Enable ); /* Enable analog output */
int FAR _export pascal COM_RFID_SetAnalogOutput    (WORD PortNumber, WORD   Voltage); /* Set analog output voltage in mV */
int FAR _export pascal COM_RFID_GetAnalogOutput    (WORD PortNumber, WORD & Voltage); /* Read analog output voltage in mV */

/* Digital I/O */
#define DIN_0            1 /* Digital input data for channel 0 */
#define DIN_1            2 /* Digital input data for channel 1 */
#define DIN_2            4 /* Digital input data for channel 2 */
#define DIN_3            8 /* Digital input data for channel 3 */
#define DIN_NUM_CHANNEL  4 /* Number of digital input channels */
int FAR _export pascal COM_RFID_GetDigitalInput     (WORD PortNumber, WORD & Value); /* Read digital input state */
int FAR _export pascal COM_RFID_AutoDigitalInput    (WORD PortNumber);               /* Set automatic transmission of digital input state */
int FAR _export pascal COM_RFID_GetLastDigitalInput (WORD PortNumber, WORD & Value); /* Get last automatically transmitted digital input state */

#define DOUT_0           1 /* Digital output data for channel 0 */
#define DOUT_1           2 /* Digital output data for channel 1 */
#define DOUT_2           4 /* Digital output data for channel 2 */
#define DOUT_3           8 /* Digital output data for channel 3 */
#define DOUT_NUM_CHANNEL 4 /* Number of digital output channels */
int FAR _export pascal COM_RFID_SetDigitalOutput  (WORD PortNumber, WORD   Value); /* Set digital output state */
int FAR _export pascal COM_RFID_GetDigitalOutput  (WORD PortNumber, WORD & Value); /* Read digital output state */

/* Keypad */
#define KEY_NO      0x0000 /* key code if no key is presset */
#define KEY_L_B     0x0001 /* code for the key in the bottom row left */
#define KEY_L_M     0x0002 /* code for the key in the middle row left */
#define KEY_L_T     0x0004 /* code for the key in the top row left */
#define KEY_LM_B    0x0008 /* code for the key in the bottom row middle left */
#define KEY_LM_M    0x0010 /* code for the key in the middle row middle left */
#define KEY_LM_T    0x0020 /* code for the key in the top row middle left */
#define KEY_RM_B    0x0040 /* code for the key in the bottom row middle right */
#define KEY_RM_M    0x0080 /* code for the key in the middle row middle right */
#define KEY_RM_T    0x0100 /* code for the key in the top row middle right */
#define KEY_R_B     0x0200 /* code for the key in the bottom row right */
#define KEY_R_M     0x0400 /* code for the key in the middle row right */
#define KEY_R_T     0x0800 /* code for the key in the top row right */
#define KEY_INVALID 0xFFFF /* invalid key code, i.e. the keypad state could not be decoded unambigously */
#define KEY_NUM     (4*3)  /* number of keys */
int FAR _export pascal COM_RFID_GetKeypad     (WORD PortNumber, WORD & Value); /* Get keypad state */
int FAR _export pascal COM_RFID_AutoKeypad    (WORD PortNumber);               /* Receive keypad state automatically */
int FAR _export pascal COM_RFID_GetLastKeypad (WORD PortNumber, WORD & Value); /* Get last automatically transmitted keypad state */

/* RFID */
#define RFID_INVALID_BYTE  0xFF       /* RFID data if no transponder is present or if its code is invalid */
#define RFID_INVALID_DWORD 0xFFFFFFFF
#define RFID_INVALID       -1
int FAR _export pascal COM_RFID_EnableRFID  (WORD PortNumber, BOOL Enable);    /* Enable RFID */
int FAR _export pascal COM_RFID_GetRFID     (WORD PortNumber, BYTE Value [8]); /* Getting RFID data */
int FAR _export pascal COM_RFID_AutoRFID    (WORD PortNumber);                 /* Receiving RFID data automatically */
int FAR _export pascal COM_RFID_GetLastRFID (WORD PortNumber, BYTE Value [8]); /* Getting last automatically transmitted RFID data */

/* Beeper */
#define TONE_c      -9
#define TONE_cis    -8
#define TONE_des    -8
#define TONE_d      -7
#define TONE_dis    -6
#define TONE_es     -6
#define TONE_e      -5
#define TONE_f      -4
#define TONE_fis    -3
#define TONE_ges    -3
#define TONE_g      -2
#define TONE_gis    -1
#define TONE_as     -1
#define TONE_a       0
#define TONE_b       1
#define TONE_h       2
#define TONE_OCTAVE 12
#define TONE_MIN   -34
#define TONE_MAX    44
#define LENGHT_MAX (255*1000/64)
int FAR _export pascal COM_RFID_EnableBeeper (WORD PortNumber, BOOL Enable);                 /* Enable beeper amplifier */
int FAR _export pascal COM_RFID_PlayTone     (WORD PortNumber, short Height, WORD Duration); /* Play tone, Height: 0='a', 2='h', 3='c', etc., Duration in ms <= LENGHT_MAX, Duration=0 => infinite */
int FAR _export pascal COM_RFID_StopTone     (WORD PortNumber);                              /* Stop playing tones */

/* Display */
#define LCD_WIDTH_PIXEL  128
#define LCD_HEIGHT_PIXEL  64
#define LCD_MEM_SIZE     (LCD_WIDTH_PIXEL * LCD_HEIGHT_PIXEL / 8)
#define LCD_CHAR_WIDTH     6
#define LCD_CHAR_HEIGHT    8
#define LCD_CHAR_FIRST  0x20
#define LCD_CHAR_LAST   0x7F
#define LCD_WIDTH        (LCD_WIDTH_PIXEL / LCD_CHAR_WIDTH)
#define LCD_HEIGHT       (LCD_HEIGHT_PIXEL / LCD_CHAR_HEIGHT)
#define LCD_MAX_BRIGTHESS 63
int FAR _export pascal COM_RFID_SetDisplayBrightness (WORD PortNumber, WORD   Value);                 /* Adjust display brightness, Value <= LCD_MAX_BRIGTHESS */
int FAR _export pascal COM_RFID_SetBacklight         (WORD PortNumber, WORD   Value);                 /* Set backlight intensity in %, Value <= 100 */
int FAR _export pascal COM_RFID_SetBacklightSmooth   (WORD PortNumber, WORD   Value, WORD TimeConst); /* Set backlight intensity smoothly, TimeConst in ms, TimeConst <= LENGHT_MAX */
int FAR _export pascal COM_RFID_GetBacklight         (WORD PortNumber, WORD & Value);                 /* Get backlight intensity in % */

int FAR _export pascal COM_RFID_ClearDisplay    (WORD PortNumber);                         /* Clear display */
int FAR _export pascal COM_RFID_SetDisplay      (WORD PortNumber, BYTE Pattern);           /* Clear display with a pattern */
int FAR _export pascal COM_RFID_ClearDisplayRow (WORD PortNumber, WORD Row);               /* Clear display row, Row < LCD_HEIGHT */
int FAR _export pascal COM_RFID_SetDisplayRow   (WORD PortNumber, WORD Row, BYTE Pattern); /* Clear display row with a pattern, Row < LCD_HEIGHT */

int FAR _export pascal COM_RFID_SetPixel   (WORD PortNumber, WORD Row, WORD Column); /* Set pixel */
int FAR _export pascal COM_RFID_ClearPixel (WORD PortNumber, WORD Row, WORD Column); /* Clear pixel */

int FAR _export pascal COM_RFID_SetDisplayXY     (WORD PortNumber, WORD Row, WORD Column); /* Set character position */
int FAR _export pascal COM_RFID_SetDisplayXYfine (WORD PortNumber, WORD Row, WORD Column); /* Set graphic position */

int FAR _export pascal COM_RFID_DisplayString       (WORD PortNumber, const char * FAR String); /* Display character string */
int FAR _export pascal COM_RFID_SetDisplayInversion (WORD PortNumber, BOOL Enable);             /* Set the display inversion flag */

int FAR _export pascal COM_RFID_DisplayLogo         (WORD PortNumber);                          /* Display logo */

int FAR _export pascal COM_RFID_WriteDisplayMemory (WORD PortNumber, WORD Size, const BYTE * Buffer); /* Write data to display memory */
int FAR _export pascal COM_RFID_ReadDisplayMemory  (WORD PortNumber, WORD Size,       BYTE * Buffer); /* Read data from display memory */

int FAR _export pascal COM_RFID_WriteDisplayRow (WORD PortNumber, WORD Row, const BYTE Buffer [LCD_WIDTH_PIXEL]); /* Write character row to display memory */
int FAR _export pascal COM_RFID_ReadDisplayRow  (WORD PortNumber, WORD Row,       BYTE Buffer [LCD_WIDTH_PIXEL]); /* Read character row from display memory */

int FAR _export pascal COM_RFID_SetDisplayMemory (WORD PortNumber, WORD Row, WORD Column, WORD Height, WORD Width, const BYTE * Buffer); /* Set display memory */
int FAR _export pascal COM_RFID_GetDisplayMemory (WORD PortNumber, WORD Row, WORD Column, WORD Height, WORD Width,       BYTE * Buffer); /* Get display memory */

/* General */
WORD FAR _export pascal COM_RFID_Version();                                                        /* Get the COM-DIO software version */

int FAR _export pascal COM_RFID_GetUptime (WORD PortNumber, DWORD & Seconds, WORD & Milliseconds); /* Get device uptime */

int FAR _export pascal COM_RFID_FW_Ver  (WORD PortNumber, WORD & Version);            /* Get the COM-DIO firmware version */
int FAR _export pascal COM_RFID_FW_Date (WORD PortNumber, char * FAR DateString);     /* Get the COM-DIO firmware date, the buffer DateString should be 16 bytes long */
int FAR _export pascal COM_RFID_Prod_ID (WORD PortNumber, char * FAR Identification); /* Get the COM-DIO product identification, the buffer Identification should be 81 bytes long */
int FAR _export pascal COM_RFID_Prod_No (WORD PortNumber, DWORD & Number);            /* Get the COM-DIO product number */

int          FAR _export pascal COM_RFID_State (WORD PortNumber);           /* Get software interface state */
const char * FAR _export pascal COM_RFID_ErrorMessage (WORD PortNumber);    /* Get the error message corresponding to the software interface state */
int          FAR _export pascal COM_RFID_IO_State (WORD PortNumber);        /* Get serial port interface state */
const char * FAR _export pascal COM_RFID_IO_ErrorMessage (WORD PortNumber); /* Get the error message corresponding to the serial port interface state */


#ifdef __cplusplus
}
#endif


#endif//__COM_RFID_H__
