/******************************************************************************
//                                                                           //
//  Project: Software Interface for LPT-I2C Adapters                         //
//                                                                           //
//  CGC Instruments, (c) 2009-2013, Version 1.00. All rights reserved.       //
//                                                                           //
//  Definition for LPT-I2C DLL routines                                      //
//                                                                           //
******************************************************************************/

#ifndef __LPT_I2C_H__
#define __LPT_I2C_H__

#include <windows.h>

#ifdef __cplusplus
extern "C" {
#endif

/************************************************************************************************************************
* The LPT_I2C_Open must be called before the first usage of other routines.
* It assigns the port address, initializes the interface, and purges the communication buffers.
*
* The return values of the routines are the following defines LPT_I2C_ERR_XXX.
*
* The last error code can be obtained by calling LPT_I2C_State.
* The routine LPT_I2C_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 LPT_I2C_IO_State,
* The routine LPT_I2C_IO_ErrorMessage provides the corresponding error message.
************************************************************************************************************************/

/* Return Values */
#define LPT_I2C_NOERR               0 /* Routine finished without any error */
#define LPT_I2C_ERR_OPEN           -1 /* Error opening the LPT port */
#define LPT_I2C_ERR_COMMAND_SEND   -2 /* Error sending command */
#define LPT_I2C_ERR_STATUS_RECEIVE -3 /* Error receiving adapter status */
#define LPT_I2C_ERR_READ_DATA      -4 /* Error receiving data */
#define LPT_I2C_ERR_SEND_DATA      -5 /* Error sending data */
#define LPT_I2C_ERR_DATA_SIZE      -6 /* Data number exceeds the buffer size */
#define LPT_I2C_ERR_DATA_LENGTH    -7 /* Wrong length of received data */
#define LPT_I2C_ERR_GET_STATUS     -8 /* Error getting status lines */
#define LPT_I2C_ERR_PARAM_READ     -9 /* Wrong parameter received */
#define LPT_I2C_ERR_PARAM_SENT    -10 /* Wrong parameter sent */
#define LPT_I2C_ERR_STR_LEN       -11 /* String is too long */
#define LPT_I2C_ERR_PARAM_SPEC   -100 /* Wrong parameter specified */
#define LPT_I2C_ERR_TIMEOUT         1 /* Timeout error */
#define LPT_I2C_ERR_INI_OPEN      100 /* Cannot open the ini file */
#define LPT_I2C_ERR_INI_PORT      101 /* Wrong Port specified in the ini file */
#define LPT_I2C_ERR_INI_MODE      102 /* Wrong Mode specified in the ini file */

/* Bits of the adapter status, returned by LPT_I2C_Status */
#define LPT_I2C_ST_INBUF_NEMPTY  0x0001 /* Input buffer not empty */
#define LPT_I2C_ST_INBUF_FULL    0x0002 /* Input buffer full */
#define LPT_I2C_ST_OUTBUF_NEMPTY 0x0004 /* Output buffer not empty */
#define LPT_I2C_ST_OUTBUF_FULL   0x0008 /* Output buffer full */
#define LPT_I2C_ST_LPT_ERROR     0x0010 /* LPT interface error, if nonzero, LPT_I2C_Status_LPT should be called to obtain the LPT interface status */
#define LPT_I2C_ST_I2C_ERROR     0x0020 /* I2C interface error, if nonzero, LPT_I2C_Status_I2C should be called to obtain the I2C interface status */
#define LPT_I2C_ST_LINE_ERR      0x0100 /* Status line ERR: LPT controller error, line active low */
#define LPT_I2C_ST_LINE_DAV      0x0200 /* Status line DAV: Data available, line active low */
#define LPT_I2C_ST_LINE_EOT      0x0400 /* Status line EOT: End of transmission, line active low */
#define LPT_I2C_ST_LINE_SRQ      0x0800 /* Status line SRQ: Service request, line active low */
#define LPT_I2C_ST_LINE_RDY      0x1000 /* Status line RDY: Device ready, line active low */

/* Bits of the LPT interface status, returned by LPT_I2C_Status_LPT */
#define LPT_I2C_ST_LPT_STATUS_TIMEOUT  0x01 /* Host could not acknowledge the status byte reception within the given time period */
#define LPT_I2C_ST_LPT_OUTPUT_TIMEOUT  0x02 /* Host could not acknowledge the data reception within the given time period */
#define LPT_I2C_ST_LPT_INPUT_TIMEOUT   0x04 /* Host could not send the data within the given time period */
#define LPT_I2C_ST_LPT_OUTBUF_EMPTY    0x08 /* Data requested on output buffer empty */
#define LPT_I2C_ST_LPT_INBUF_FULL      0x10 /* Data sent on input buffer full */
#define LPT_I2C_ST_LPT_ERR_FORMAT      0x20 /* Wrong data format detected */
#define LPT_I2C_ST_LPT_ERR_HANDSHAKE   0x40 /* Wrong handshake detected */
#define LPT_I2C_ST_LPT_ERR_REBOOT      0x80 /* Controller rebooted due to watchdog timeout */

/* Bits of the I2C interface error status, returned by LPT_I2C_Status_I2C and LPT_I2C_Wait4Finish */
#define LPT_I2C_ST_I2C_NACK      0x01 /* Device did not acknowledge */
#define LPT_I2C_ST_I2C_ARB_LOST  0x02 /* Arbitration lost */
#define LPT_I2C_ST_I2C_BUSY      0x80 /* Controller busy */

/* Bits of the ADC status byte, returned by LPT_I2C_Status_ADC */
#define LPT_I2C_ST_ADC_12V   0x01 /* 12V data ready */
#define LPT_I2C_ST_ADC_5V    0x02 /* 5V data ready */
#define LPT_I2C_ST_ADC_3V    0x04 /* 3V data ready */
#define LPT_I2C_ST_ADC_TEMP  0x08 /* Temperature data ready */

WORD _export pascal LPT_I2C_Version(); /* Get the LPT-I2C software version */

/* Communication */
int _export pascal LPT_I2C_Open();                        /* Open the LPT port  */
int _export pascal LPT_I2C_Flush_LPT();                   /* Flush LPT interface buffers of the LPT-I2C adapter */
int _export pascal LPT_I2C_Reboot();                      /* Reboot the controller */

int _export pascal LPT_I2C_Status     (WORD & State, BYTE Full = TRUE); /* Get adapter status, if Full is zero, only the bits LPT_I2C_ST_LINE_xxx are returned */
int _export pascal LPT_I2C_Status_LPT (BYTE & State);                   /* Get LPT interface status */
int _export pascal LPT_I2C_Status_I2C (BYTE & State);                   /* Get I2C interface status */

int _export pascal LPT_I2C_DataAvailable (BYTE & DataAvailable); /* Get status of the Data Available line */
int _export pascal LPT_I2C_Wait4Data();                          /* Wait for data, i.e. low level at the Data Available line */
int _export pascal LPT_I2C_SetTimeout    (DWORD Timeout);        /* Set Timeout value, default = 10000 */

/* I2C control */
int _export pascal LPT_I2C_SetI2CFrequency (DWORD   Frequency); /* Set I2C SCK frequency in Hz */
int _export pascal LPT_I2C_GetI2CFrequency (DWORD & Frequency); /* Get I2C SCK frequency in Hz */
#define LPT_I2C_GENERAL_CALL  0x8000 /* I2C address bit indicating that the controller response to the I2C general call */
int _export pascal LPT_I2C_SetI2CAddress   (WORD    Address);   /* Set I2C address of the 2nd controller */
int _export pascal LPT_I2C_GetI2CAddress   (WORD  & Address);   /* Get I2C address of the 2nd controller */


int _export pascal LPT_I2C_Send          (BYTE Address, const BYTE * Data,   WORD Size, BYTE Stop); /* Send data to an I2C slave, if Stop is non-zero, finish by STOP */
int _export pascal LPT_I2C_SendString    (BYTE Address, const char * String,            BYTE Stop); /* Send Pascal string to an I2C slave, if specified, finish with STOP */
int _export pascal LPT_I2C_Receive       (BYTE Address,       BYTE * Data,   WORD Size, BYTE Stop); /* Receive data from an I2C slave, if Stop is non-zero, finish by STOP */
int _export pascal LPT_I2C_ReceiveString (BYTE Address,       char * String, WORD Size, BYTE Stop); /* Receive Pascal string from an I2C slave, if specified, finish with STOP */
int _export pascal LPT_I2C_Check         (BYTE Address, BYTE & Present);                            /* Check for presence of a device, if device responds, return non-zero in Present */

int _export pascal LPT_I2C_Wait4Finish (BYTE & State);  /* Wait until all active operations finish, return the status of the I2C interface */

/* UDB interface */
/*   Slot is the UDB slot number, if already known from a previous operation, the stored slot value is used */
/*     if ForceIO is non-zero, the I/O operation will be performed regardless of the knowledge of the stored value, otherwise the I/O operation will be executed only if necessary */
/*   if Stop is non-zero, the I/O operation will be finished by STOP */
int _export pascal LPT_I2C_GetSlot (BYTE * Slot, BYTE Stop, BYTE ForceIO = FALSE); /* Get the UDB slot number */
int _export pascal LPT_I2C_SetSlot (BYTE   Slot, BYTE Stop, BYTE ForceIO = FALSE); /* Set the UDB slot number */
int _export pascal LPT_I2C_CheckIFC (BYTE  Slot, BYTE & Present);                  /* Check for presence of an IFC card, if the card responds, return non-zero in Present, Note: Slot must be a multiple of 16 */

/* General information */
int _export pascal LPT_I2C_FW_Ver    (WORD & Version);        /* Get firmware version */
int _export pascal LPT_I2C_FW_Date   (char * DateString);     /* Get firmware date */
int _export pascal LPT_I2C_Prod_ID   (char * Identification); /* Get product identification */
int _export pascal LPT_I2C_Prod_No   (DWORD & Number);        /* Get product number */

/* Housekeeping data */
int _export pascal LPT_I2C_Get12V  (float & Voltage    ); /* Get external 12V supply voltage in Volt */
int _export pascal LPT_I2C_Get5V   (float & Voltage    ); /* Get internal 5V supply voltage in Volt */
int _export pascal LPT_I2C_Get3V   (float & Voltage    ); /* Get internal 3V supply voltage in Volt */
int _export pascal LPT_I2C_GetTemp (float & Temperature); /* Get controller temperature */
int _export pascal LPT_I2C_Status_ADC (BYTE & State);  /* Get ADC status */

/* Various */
int _export pascal LPT_I2C_GetUptime   (double & Uptime); /* Get the uptime in seconds */
int _export pascal LPT_I2C_GetIniParams  (char * Buffer); /* Return parameters read from the ini file */
int _export pascal LPT_I2C_GetPortParams (char * Buffer); /* Return parameters get during opening the port */

/* Error handling */
int          _export pascal LPT_I2C_State();           /* Get last error code */
const char * _export pascal LPT_I2C_ErrorMessage();    /* Get error message corresponding to the last error code */
int          _export pascal LPT_I2C_IO_State();        /* Get interface state */
const char * _export pascal LPT_I2C_IO_ErrorMessage(); /* Get error message corresponding to the interface state */

#ifdef __cplusplus
}
#endif

#endif//__LPT_I2C_H__
