You are here

gpio_24.h

#ifndef GPIO24_H_INCLUDED
#define GPIO24_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
/*
 * Compatibility stuff
 */
#include <sys/types.h>
#include <sys/time.h>

typedef unsigned long long cycles_t;

static inline cycles_t get_cycles (void)
{
   unsigned long long ret;
   __asm__ __volatile__("rdtsc" : "=A" (ret));
   return ret;
}

typedef bool   BOOL;
#define TRUE   true
#define FALSE   false

typedef u_int8_t   BYTE;
typedef u_int8_t   UCHAR;
typedef u_int16_t   WORD;
typedef u_int16_t   USHORT;
typedef u_int32_t   DWORD;
typedef u_int32_t   HWND;
typedef u_int32_t   UINT;
typedef u_int32_t   ULONG;
typedef cycles_t   LARGE_INTEGER;
typedef void   *HANDLE;
typedef void   *LPVOID;
typedef void   *GUID;
typedef void   *LPOVERLAPPED;

static inline LARGE_INTEGER RDTSC(void)
{
   return get_cycles();
}

#define _stdcall   /* nothing */
#define __stdcall   /* nothing */
#define INVALID_HANDLE_VALUE ((HANDLE)(-1))
#define INFINITE   (-1)
#define WAIT_OBJECT_0   0
#define WAIT_TIMEOUT   ETIMEDOUT

#include <pthread.h>

typedef struct _EVENT {
   pthread_mutex_t mutex;   
   pthread_mutexattr_t attr;   
   pthread_cond_t cond;   
   int broadcast;   
} EVENT, *PEVENT;

#endif /* defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)*/

#ifdef __GNUC__
#define __PACKED_ATTR __attribute__ ((__packed__))
#else
#ifdef WIN32
#include <windows.h>
#endif /* WIN32 */
#define __PACKED_ATTR   /*nothing*/
#pragma pack(push, 1)
#endif /* __GNUC__ */

typedef long HGPIO;
const HGPIO HGPIO_INVALID_HANDLE = -1;
const HGPIO HGPIO_ALL_DEVICES = -2;
const HGPIO HGPIO_DLL_NOTIFICATION = -3; 

enum GPIO_RESULT
{
// Successful return codes
   GPIO_S_SUCCESS = 0,
   GPIO_S_SUCCESSFUL_REINIT = 1,
   GPIO_S_ALREADY_OPENED = 2,
// Error codes
   GPIO_E_OUT_OF_MEMORY = -1,
   GPIO_E_NOT_INITIALIZED = -2,
   GPIO_E_INVALIDARG = -3,
   GPIO_E_HANDLE = -4,
   GPIO_E_FAIL = -5,
   GPIO_E_EVENT_ABSENT = -6,
};

#define GPIO_SUCCEEDED(Result) (Result >= 0)
#define GPIO_FAILED(Result) (Result < 0)

typedef enum _GPIO_NOTIFICATION_TYPE
{
   ntEvent,
   ntCallback,
   ntWindowMessage,
   ntThreadMessage,
   ntNoNotification,
} GPIO_NOTIFICATION_TYPE;

typedef void (__stdcall * PGPIO_CALLBACK)(HGPIO);

typedef struct _GPIO_NOTIFICATION
{
   GPIO_NOTIFICATION_TYPE type;
   PGPIO_CALLBACK callback;
   DWORD thread;
   HWND wnd;
   UINT msg;
   HANDLE event;
} GPIO_NOTIFICATION, *PGPIO_NOTIFICATION;

const unsigned int GPIO_COMMAND_LENGTH = 8;
typedef struct _GPIO_COMMAND
{
   unsigned char command[GPIO_COMMAND_LENGTH]; 
} __PACKED_ATTR GPIO_COMMAND, *PGPIO_COMMAND;


const unsigned int GPIO_EVENT_LENGTH = 8;
typedef struct _GPIO_EVENT
{
   unsigned char event[GPIO_EVENT_LENGTH];
   LARGE_INTEGER timestamp; 
   HGPIO device; 
} GPIO_EVENT, *PGPIO_EVENT;

GPIO_RESULT _stdcall GPIO_Init(GPIO_NOTIFICATION Notification);
GPIO_RESULT _stdcall GPIO_Uninit();
GPIO_RESULT _stdcall GPIO_GetDeviceCount(long *pDevNumber);
GPIO_RESULT _stdcall GPIO_OpenDevice(long nDevice, HGPIO *phDevice);
GPIO_RESULT _stdcall GPIO_CloseDevice(HGPIO hDevice);
GPIO_RESULT _stdcall GPIO_CloseAllDevices();
GPIO_RESULT _stdcall GPIO_SendCommand(HGPIO hDevice, GPIO_COMMAND Command);
GPIO_RESULT _stdcall GPIO_GetEvent(HGPIO hDevice, GPIO_EVENT *pEvent);
GPIO_RESULT _stdcall GPIO_Transaction(HGPIO hDevice, GPIO_COMMAND Command, 
   GPIO_EVENT *pResponse);

#ifndef __GNUC__
#pragma pack(pop)
#endif
#ifdef __cplusplus
}
#endif
#endif //GPIO24_H_INCLUDED