// Motor_GALIL.h: interface for the CMotor_GALIL class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MOTOR_GALIL_H__B32C658E_7AF3_44AE_A7F0_CA13719016EA__INCLUDED_)
#define AFX_MOTOR_GALIL_H__B32C658E_7AF3_44AE_A7F0_CA13719016EA__INCLUDED_

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

#include "MotorBase.h"

class CMotor_GALIL : public CMotorBase
{
public:
    CMotor_GALIL(CMtrProfile mtrProfile, double *dpPos, CMotionControllerBase *pBaseController);
    virtual ~CMotor_GALIL();

// **************************************************************
// Methods declaration
// **************************************************************

// method to close the axis
    void CloseAxis() override;

// method to initialize and configure the axis
    void ConstructAxis() override;

//  method to On/Off motor
    bool OnMotor() override;
    bool OffMotor() override;

//  method to stop motor with decceleration
    bool DecelStop() override;

//  method to trigger Emergency stop the motor  int EStop();
    bool EmergStop() override;

// method to get commanded position
    bool GetMotorCommandedPosition(int iMtrNo, double *pRaw) override;

// method Check other axis home sensor
    BOOL IsHomed(const CMtrProfile &mtrProfile) override;

// method to get other motor axis IO status
    int GetMotionIOStatus(const CMtrProfile &mtrProfile) override;

// method to get Other axis motor ready status
    BOOL IsMotorReady(const CMtrProfile &mtrProfile) override;

// method to get Other axis negative/ccw limit status
    BOOL IsNegLmtHit(const CMtrProfile &mtrProfile) override;

// method to get Other axis positive/cw limit status
    BOOL IsPosLmtHit(const CMtrProfile &mtrProfile) override;

// method to get Other axis home sensor status
    bool IsMtrOn(const CMtrProfile &mtrProfile) override;

// method to get other motor axis current position
    bool GetMotorPosition(const CMtrProfile &mtrProfile, double *pRawPosition) override;

// method to get motor axis current position error
    bool GetMotorPositionError(double *pRawPositionError) override;

    int SetHomeMode(int iHomeMode) override;

// method to do homing by index signal
    BOOL HomeByIndex() override;

// method to do homing by Home Sensor
    BOOL HomeBySensor() override;

// method to move absolute
    bool AbsoluteMove(double dPosition, int nMode, PROFILE_CURVE curve) override;

// method to move relative distance
    bool RelativeMove(double dDistance, PROFILE_CURVE curve) override;

// method to move continuously
    int MoveContinuously(bool bCW = true) override;

// method to process interrupt to decode what happen to the motor
    void ProcessInt(int nIntCode = 0) override;

// method to reset single trigger
    int ResetSingleTrigger() override;

// method to set the commanded position register
    bool SetMotorCommandedPosition(double dPosition) override;

// method to set the mechanical coversion factor
    bool RefreshMotorConversionFactor() override;

// method to force set the current position
    bool SetMotorPosition(double dPosition) override;

// method to arm controller axis for single trigger
    int SetSingleTrigger(double dPosition,  int nCmpSource = 1, int nPulseStretch = STRETCH_NON) override;

// method to arm multiaxis for simultaneous motion
    int SimultaneousMove(int nNoOfAxis, CMtrProfile *mtrProfile,  double *dPosition, double *dStartVel, double *dMaxVel, double *dAcc, int nMode = 0) override;

// method to move motor in s-curve continuously
    int SMoveContinuously(bool bCW = true) override;

    void SetMotorProfile(double dValue, motion_profile::PROFILE_TYPE type) override;

// **************************************************************
// Methods declaration applicable to GALIL ONLY - STARTS
// **************************************************************
// method to get motion stop code
    int GetStopCode() override;

// method to get motion stop code
    CString GetVariable(CString csVariableName) override;

// method to send direct command string to controller
    int SendCommand(CString csCommand) override;

// method to stop any running script program and stop all motion
    int StopProgramAndMotion() override;

// method to update motion profile
    int UpdateProfile() override;
// **************************************************************
// Methods declaration applicable to GALIL ONLY - ENDS
// **************************************************************

private:
    int GalilFindHomeIndex(HOME_DIR homeDir, MOTION_SPEED speed);

    int GalilUpdateHomeSlowProfile();

    bool GalilUpdateProfile(MOTION_SPEED speed);

    int GalilFindHomeSensor(HOME_DIR homeDir, MOTION_SPEED speed);

    int GalilMove();

// Gary 20Sep11, V1.29.03, cater configurable trigger pulse width
    int m_nPulseWidth;

    bool SendCommand(HANDLEDMC handle, char *format, ...);

    bool SendStartMove(HANDLEDMC handle, int nMotorNo);

    bool SendStartWaitHomeThread(HANDLEDMC handle, int nMotorNo);

    bool SendStartWaitMotionDoneThread(HANDLEDMC handle, int nMotorNo);

    bool SendCommandGetResponse(HANDLEDMC handle, char *pResponse, char *format, ...);

    bool SendHomeSensorLogic(HANDLEDMC handle, int nLogic);

    bool SendJog(HANDLEDMC handle, int nMotorNo, int nDir, long lSpeed);

    HANDLEDMC m_handle;

    const char m_cGalilAxis;

    bool FindHome(MOTION_SPEED speed);

    void SetVirtualHomePosition();
};

#endif // !defined(AFX_MOTOR_GALIL_H__B32C658E_7AF3_44AE_A7F0_CA13719016EA__INCLUDED_)
