#ifndef _USMCDLL_H
 
#define _USMCDLL_H
 
 
 
 
 
#include        <stdlib.h>
 
 
 
 
 
#define         USMC_ERROR_REF_DEV_DISCONNECTED         0x14141413
 
#define         USMC_ERROR_NOT_INITIALIZED              0x14141414
 
#define         USMC_ERROR_NO_DEVICES_CONNECTED         0x14141415
 
#define         USMC_SUCCESS                            0
 
#define         TRUE                                    1
 
#define         FALSE                                   0
 
 
 
 
 
typedef         unsigned char   BYTE;
 
typedef         unsigned int    DWORD;
 
typedef         unsigned short  WORD;
 
typedef         unsigned int    BOOL;
 
typedef         void *          PVOID;
 
 
 
 
 
typedef struct  _USMC_Devices
 
{
 
        DWORD   NOD;     // Number of devices connected to computer.
 
        char ** Serial;  // Array of pointers to 16√byte ASCII strings of length √ NOD.
 
        char ** Version; // Array of pointers to 4√byte ASCII strings of length √ NOD.
 
} USMC_Devices, * PUSMC_Devices, * LPUSMC_Devices;
 
 
 
 
 
typedef struct  _USMC_Parameters
 
{
 
        float AccelT;     // Acceleration time (in ms).
 
        float DecelT;     // Deceleration time (in ms).
 
        float PTimeout;   // Time (in ms) after which current will be reduced to 60% of normal.
 
        float BTimeout1;  // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO1P field in this structure.
 
        float BTimeout2;  // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO2P field in this structure.
 
        float BTimeout3;  // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO3P field in this structure.
 
        float BTimeout4;  // Time (in ms) after which speed of step motor rotation will be equal to the one specified at BTO4P field in this structure.
 
        float BTimeoutR;  // Time (in ms) after which reset command will be performed.
 
        float BTimeoutD;  // This field is reserved for future use.
 
        float MinP;               // Speed (steps/sec) while performing reset operation.
 
        float BTO1P;      // Speed (steps/sec) after BTIMEOUT1 time has passed.
 
        float BTO2P;      // Speed (steps/sec) after BTIMEOUT2 time has passed.
 
        float BTO3P;      // Speed (steps/sec) after BTIMEOUT3 time has passed.
 
        float BTO4P;      // Speed (steps/sec) after BTIMEOUT4 time has passed.
 
        WORD  MaxLoft;    // Value in full steps that will be used performing backlash operation.
 
        DWORD StartPos;   // Current Position saved to FLASH.
 
        WORD  RTDelta;    // Revolution distance √ number of full steps per one full revolution.
 
        WORD  RTMinError; // Number of full steps missed to raise the error flag.
 
        float MaxTemp;    // Maximum allowed temperature (centigrade degrees).
 
        BYTE  SynOUTP;    // Duration of the output synchronization pulse.
 
        float LoftPeriod; // Speed (steps/sec) of the last phase of the backlash operation.
 
        float EncMult;    // Encoder step multiplier. Should be <Encoder Steps per Evolution> / <SM Steps per Evolution> and should be integer multiplied by 0.25.
 
} USMC_Parameters, * PUSMC_Parameters, * LPUSMC_Parameters;
 
 
 
 
 
typedef struct  _USMC_StartParameters
 
{
 
        BYTE SDivisor;  // Step is divided by this factor (1,2,4,8).
 
        BOOL DefDir;    // Direction for backlash operation (relative).
 
        BOOL LoftEn;    // Enable automatic backlash operation (works if slow start/stop mode is off).
 
        BOOL SlStart;   // If TRUE slow start/stop mode enabled.
 
        BOOL WSyncIN;   // If TRUE controller will wait for input synchronization signal to start.
 
        BOOL SyncOUTR;  // If TRUE output synchronization counter will be reset.
 
        BOOL ForceLoft; // If TRUE and destination position is equal to the current position backlash operation will be performed.
 
} USMC_StartParameters, * PUSMC_StartParameters, * LPUSMC_StartParameters;
 
 
 
 
 
typedef struct  _USMC_Mode
 
{                                               // Masked               Saved To Flash          Description:
 
        BOOL  PMode;            //              √                     YES                                     Turn off buttons (TRUE - buttons disabled).
 
        BOOL  PReg;                     //              √                     YES                                     Current reduction regime (TRUE - regime is on).
 
        BOOL  ResetD;           //              √                     YES                                     Turn power off and make a whole step (TRUE - apply).
 
        BOOL  EMReset;          //              √                     √                                     Quick power off.
 
        BOOL  Tr1T;                     //              √                     YES                                     Trailer 1 TRUE state (TRUE : +3/+5б; FALSE : 0б).
 
        BOOL  Tr2T;                     //              √                     YES                                     Trailer 2 TRUE state (TRUE : +3/+5б; FALSE : 0б).
 
        BOOL  RotTrT;           //              √                     YES                                     Rotary Transducer TRUE state (TRUE : +3/+5б; FALSE : 0б).
 
        BOOL  TrSwap;           //              √                     YES                                     If TRUE, Trailers are treated to be swapped.
 
        BOOL  Tr1En;            //              √                     YES                                     If TRUE Trailer 1 Operation Enabled.
 
        BOOL  Tr2En;            //              √                     YES                                     If TRUE Trailer 2 Operation Enabled.
 
        BOOL  RotTeEn;          //              √                     YES                                     If TRUE Rotary Transducer Operation Enabled.
 
        BOOL  RotTrOp;          //              √                     YES                                     Rotary Transducer Operation Select (stop on error if TRUE).
 
        BOOL  Butt1T;           //              √                     YES                                     Button 1 TRUE state (TRUE : +3/+5б; FALSE : 0б).
 
        BOOL  Butt2T;           //              √                     YES                                     Button 2 TRUE state (TRUE : +3/+5б; FALSE : 0б).
 
        BOOL  ResetRT;          //              YES                     √                                     Reset Rotary Transducer Check Positions (need one full revolution before it can detect error).
 
        BOOL  SyncOUTEn;        //              √                     YES                                     If TRUE output synchronization enabled.
 
        BOOL  SyncOUTR;         //              YES                     √                                     If TRUE output synchronization counter will be reset.
 
        BOOL  SyncINOp;         //              √                     YES                                     Synchronization input mode: 
 
                                                //                                                                              TRUE - Step motor will move one time to the DestPos 
 
                                                //                                                                              FALSE - Step motor will move multiple times by DestPos microsteps as distance.
 
        DWORD SyncCount;        //              √                     YES                                     Number of steps after which synchronization output signal occurs.
 
        BOOL  SyncInvert;       //              √                     YES                                     Set this bit to TRUE to invert output synchronization polarity.
 
        BOOL  EncoderEn;        //              √                     YES                                     Enable Encoder on pins {SYNCIN,ROTTR} - disables Synchronization input and Rotary Transducer.
 
        BOOL  EncoderInv;       //              √                     YES                                     Invert Encoder Counter Direction.
 
        BOOL  ResBEnc;          //              YES                     √                                     Reset <EncoderPos> and <ECurPos> to 0.
 
        BOOL  ResEnc;           //              YES                     √                                     Reset <ECurPos> to <EncoderPos>.
 
} USMC_Mode, * PUSMC_Mode, * LPUSMC_Mode;
 
 
 
 
 
typedef struct  _USMC_State
 
{
 
        int CurPos;             // Current position (in 1/8 steps).
 
        float Temp;             // Current temperature of the power driver.
 
        BYTE SDivisor;  // Step is divided by this factor.
 
        BOOL Loft;              // Indicates backlash status.
 
        BOOL FullPower; // Full power if TRUE.
 
        BOOL CW_CCW;    // Current direction of rotation (relatively to some direction √ dependent on step motor circuits connection and on its construction).
 
        BOOL Power;             // If TRUE then Step Motor power is ON.
 
        BOOL FullSpeed; // If TRUE then full speed. Valid in "Slow Start" mode only.
 
        BOOL AReset;    // TRUE After Device reset, FALSE after "Set Position".
 
        BOOL RUN;               // TRUE if step motor is rotating.
 
        BOOL SyncIN;    // Logical state directly from input synchronization PIN (pulses treated as positive).
 
        BOOL SyncOUT;   // Logical state directly from output synchronization PIN (pulses are positive).
 
        BOOL RotTr;             // Indicates current rotary transducer logical press state.
 
        BOOL RotTrErr;  // Indicates rotary transducer error flag (reset by USMC_SetMode function with ResetRT bit √ TRUE).
 
        BOOL EmReset;   // Indicates state of emergency disable button (TRUE √ Step motor power off).
 
        BOOL Trailer1;  // Indicates trailer 1 logical press state.
 
        BOOL Trailer2;  // Indicates trailer 2 logical press state.
 
        float Voltage;  // Input power source voltage (6-39V) -= 24 version 0nly=-.
 
} USMC_State, * PUSMC_State, * LPUSMC_State;
 
 
 
 
 
typedef struct  _USMC_EncoderState
 
{
 
        int EncoderPos; // Current position measured by encoder.
 
        int ECurPos;    // Current position (in Encoder Steps) - Synchronized with request call.
 
} USMC_EncoderState, * PUSMC_EncoderState, * LPUSMC_EncoderState;
 
 
 
 
 
typedef USMC_Devices const * PCUSMC_Devices;
 
typedef USMC_Devices const * LPCUSMC_Devices;
 
typedef USMC_Parameters const * PCUSMC_Parameters;
 
typedef USMC_Parameters const * LPCUSMC_Parameters;
 
typedef USMC_StartParameters const * PCUSMC_StartParameters;
 
typedef USMC_StartParameters const * LPCUSMC_StartParameters;
 
typedef USMC_Mode const * PCUSMC_Mode;
 
typedef USMC_Mode const * LPCUSMC_Mode;
 
typedef USMC_State const * PCUSMC_State;
 
typedef USMC_State const * LPCUSMC_State;
 
typedef USMC_EncoderState const * PCUSMC_EncoderState;
 
typedef USMC_EncoderState const * LPCUSMC_EncoderState;
 
 
 
 
 
DWORD USMC_Init ( USMC_Devices * Str );
 
DWORD USMC_GetState ( DWORD Device, USMC_State * Str );
 
DWORD USMC_SaveParametersToFlash ( DWORD Device );
 
DWORD USMC_GetMode ( DWORD Device, USMC_Mode * Str );
 
DWORD USMC_SetMode ( DWORD Device, USMC_Mode * Str );
 
DWORD USMC_GetParameters ( DWORD Device, USMC_Parameters * Str );
 
DWORD USMC_SetParameters ( DWORD Device, USMC_Parameters * Str );
 
DWORD USMC_GetStartParameters ( DWORD Device, USMC_StartParameters * Str );
 
DWORD USMC_Start ( DWORD Device, int DestPos, 
 
                                   float * Speed, 
 
                                   USMC_StartParameters * Str );
 
DWORD USMC_Stop ( DWORD Device );
 
DWORD USMC_SetCurrentPosition ( DWORD Device, int Position );
 
DWORD USMC_GetEncoderState ( DWORD Device, USMC_EncoderState * Str );
 
void  USMC_GetLastErr ( char * str, ssize_t len );
 
DWORD USMC_Close ( void );
 
#endif  // _USMCDLL_H