//////////////////////////////////////////////////////////////////////
//
// Request.h: interface for the Request module.
//
// This header file is a part of the PMCA COM package
//
// Amptek Inc. 2000
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_REQUEST_H__2CF7C963_F14C_11D1_B630_9CFF0CC10000__INCLUDED_)
#define AFX_REQUEST_H__2CF7C963_F14C_11D1_B630_9CFF0CC10000__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include "windows.h"
#include "PmcaCom.h"
#include "PmcaErr.h"
#include "Queue.h"

#define MAX_COM_PORT 9
#define MIN_BAUD_RATE 300
#define MAX_BAUD_RATE 115200
#define TOTAL_MEMORY_SIZE (32 * 1024)
#define MAX_GAIN (16 * 1024)
#define MIN_GAIN_CODE 0
#define MAX_GAIN_CODE 6
#define MIN_GAIN (MAX_GAIN >> MAX_GAIN_CODE)
#define MAX_GROUP (2 << MAX_GAIN_CODE)
#define MaxGroup(gainCode) (2 << gainCode)

typedef enum
{
    OP_DUMMY,
    OP_POWER_UP,
    OP_CONNECT,
    OP_DISCONNECT,
//  Get codes
    OP_GET_SERIAL_NUMBER,
    OP_GET_BATTERY_TYPE,
    OP_GET_BATTERY_CONDITION,
    OP_GET_BATTERY_CAPACITY,
    OP_GET_GROUP,
    OP_GET_GAIN,
    OP_GET_REAL_TIME,
    OP_GET_LIVE_TIME,
    OP_GET_FLAGS,
    OP_GET_STATUS,
    OP_GET_DATA,
    OP_GET_DATE_TIME,
    OP_GET_THRESHOLD,
//  Set codes
    OP_SET_ABSOLUTE_PEAK_MODE,
    OP_SET_FIRST_PEAK_MODE,
    OP_SELECT_REAL_TIMER,
    OP_SELECT_LIVE_TIMER,
    OP_CLEAR_DATA,
    OP_CLEAR_TIME,
    OP_CLEAR_DATA_AND_TIME,
    OP_START_ACQUISITION,
    OP_STOP_ACQUISITION,
    OP_SET_GROUP,
    OP_SET_GAIN,
    OP_SET_TIME,
    OP_SET_DATE,
    OP_SET_LOCK,
    OP_SET_BATTERY_TYPE,
    OP_SET_ACQUISITION_TIME,
    OP_SET_THRESHOLD,
    OP_STAY_ALIVE,
    OP_TERMINATE,
    OP_END_CODE
} OpCodeType;

#pragma pack(push)
#pragma pack(1)

typedef struct
{
    BYTE spare;
    BYTE hours;
    BYTE minutes;
    BYTE seconds;
} TimeType;

typedef struct
{
    BYTE century;
    BYTE year;
    BYTE month;
    BYTE day;
} DateType;

typedef struct
{
    DateType date;
    TimeType time;
} DateTimeType;

typedef union
{
    WORD powerUpPort;
    struct
    {
        int baudRate;
        BYTE port;
        PmcaDeviceType device;
    } connect;
    int group;
    int gain;
    unsigned short threshold;
    int setAcquisitionStartTime;
    PmcaFlagsType *flagsPtr;
    PmcaStatusType *statusPtr;
    struct
    {
        BYTE *buffer;
        int channel;
        int dwordCount;
    } data;
    char *strDateTime;
    TimeType timeStamp;
    DateType dateStamp;
    int lock;
    PmcaBatteryType batteryType;
    int time;
} RequestParameterType;
        
typedef struct
{
    QueueEntryType queueEntry;
    OpCodeType code;
    HANDLE wait;
    int waitCount;
    int *result;
    RequestParameterType p;
} RequestType;

#pragma pack(pop)

#define MAX_EXEC_PRIORITY 6

extern PmcaDeviceType GetDeviceType(void);
extern int TerminateCom(void);
extern int SetPriority(int priority);
extern int CancelRequest(int requestId);
extern int WaitForRequestCompletion(int requestId);
extern int PutRequest(RequestType *request);
extern void FreeRequest(RequestType *request);
extern RequestType *AllocateRequest(OpCodeType code, int *result);

#endif // !defined(AFX_REQUEST_H__2CF7C963_F14C_11D1_B630_9CFF0CC10000__INCLUDED_)
