/*
 * Servo To Go controller
 *
 * Copyright (c) 1995 Servo To Go Inc.
 * 1716  221st PL NE
 * Redmond, WA  98053
 *
 * Version 0.1 November 1995
 */

#ifndef _ServoToGo_H_
#define _ServoToGo_H_

#define STG_SUCCESS  0
#define STG_FAILURE  1

#define STG_NUM_AXIS_IN_SYS     8
#define STG_MAX_AXIS            8


typedef union
{ 
    unsigned long               all;
    struct {unsigned char  A, B, C, D;} port;
} STG_IO32;

typedef union
{  
    long           Long;
    unsigned char  Byte[4];
} STG_LONGBYTE;

class ServoToGo
{
public:
                   ServoToGo(void){}; // AFTER THIS, NEED TO CALL Init() BEFORE USING!
                   ServoToGo(unsigned short RequestedIrq, unsigned short BaseAddress);
                   ~ServoToGo(void);
    int            Init(unsigned short RequestedIrq, unsigned short BaseAddress);
    void           RawDAC(unsigned short nAxis, long lCounts);
    void           StartADC(unsigned short wAxis);
    short          SpinReadADC(unsigned short wAxis);
    long           ReadADC(unsigned short wAxis, short& counts);
    void           AutoZeroAdc(void);
    void           DontAutoZeroAdc(void);
    void           CalADC(void);
    short	   GetADC(unsigned char iChannel); 
    void           EncoderInit(void);
    void           EncoderLatch(void);
    void           EncoderResetAddr(void);
    void           SetEncoderCounts(unsigned short nAxis, long lCounts);
    void           SelectIndexAxis(unsigned char byAxis, unsigned char byPol);
    void           SelectIndexAxis(unsigned char byAxis);
    void           SelectIndexOrExtLatch(unsigned char bySelectBits);
    void           EnableCounterLatchOnIndexOrExt(unsigned char bySelectBits);
    short          IndexPulse(void);
    unsigned short IndexPulseLatch(void);
    void           ResetIndexLatches(unsigned char byLatchBits);
    unsigned char  CurrentIRR(void);
    void           ResetIndexLatch(void);
    char           GetIndexLatches(void);
    char           GetSELDI(void);
    char           GetIDLEN(void);
    void           EncReadAll(STG_LONGBYTE *lbEnc);
    unsigned long  RawDI(void);
    unsigned char  RawDI(short nPort);
    unsigned long  RawDI(long lPort, long lBitNumber);
    unsigned char  RawDI(unsigned char byBitNumber, short nPort);
    void           RawDO(unsigned long lOutBits);
    void           RawDO(STG_IO32 xOutBits);
    void           RawDO(unsigned char byData, short nPort);
    void           RawDO(unsigned char byBitNumber, unsigned char bySet0or1, unsigned short nPort);
    void           DioDirection(const unsigned short nSwDir);
    void           DioDirection(unsigned short nPort, unsigned short nDirection);
    void           StopTimer(void);
    void           StartInterrupts(void);
    void           SelectInterruptPeriod(long lPeriodSelect);
    void           StopInterrupts(void);
    unsigned short GetBoardPresence(void) {return wNoBoardFlag;};
    unsigned short GetAxes(void) {return wAxesInSys;};
    unsigned short GetIrq(void) {return wIrq;};
    unsigned short GetAddr(void) {return wBaseAddress;};
    unsigned short GetModel(void) {return wModel;};
    void           Timer2Delay(unsigned short counts);
    void           StartTimer2TerminalCount(unsigned short count);
    void           StartTimer2RTI(unsigned short count);
    unsigned short ReadTimer2TerminalCount(void);
    short          PollTimer2(void);
    void           MaskTimer2Interrupt(void);
    void           UnMaskTimer2Interrupt(void);

    void	   HandOpen();
    void	   HandClose();

protected:
    unsigned short wBaseAddress;
    void                  MotSim(void);

private:
    unsigned char  byIndexPollAxis;
    unsigned char  byIndexPulsePolarity;
//    static unsigned char  byABC_Dir;
    void                  SetIrq(unsigned short wRequestedIrq);
    unsigned short        BaseFind(void);
    unsigned short        BrdtstOK(unsigned short wBaseAddress);
    void                  SetDDir(unsigned short nSwDir);
    unsigned short wSaveDirs;
    unsigned short wModel;
    unsigned short wIrq;
    unsigned short wNoBoardFlag;
    unsigned short wAxesInSys;
    long           lSimDac[STG_MAX_AXIS];
    long           lSimEnc[STG_MAX_AXIS];
    unsigned char  byOldByte2[STG_MAX_AXIS];
    unsigned char  byEncHighByte[STG_MAX_AXIS];

};

#endif   // _ServoToGo_H_
