// Motor_GALIL.h: interface for the CMotor_GALIL class.
//
//////////////////////////////////////////////////////////////////////
#pragma once

#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(double dPosition = 0.0) 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;

    /// Ported from v2.3.8.53
    // method to set multiple trigger based on point table
    int SetMultipleTrigger(double* pPosition, int nNoOfTrigger, int nCmpSource = 1) override;
    ///
    bool SetMotorMultipleTrigger(const multiple_trigger& trigger) override;

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

    // method to get motion stop code
    std::string GetVariable(const std::string& csVariableName) override;

    // method to send direct command string to controller
    int SendCommand(const std::string& 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();

    // cater configurable trigger pulse width
    int m_nPulseWidth;

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

    bool SendStartMove(GCon handle, int nMotorNo);

    bool SendStartWaitHomeThread(GCon handle, int nMotorNo);

    bool SendStartWaitMotionDoneThread(GCon handle, int nMotorNo);

    bool SendCommandGetResponse(GCon handle, char* pResponse, size_t size, char* format, ...);

    bool SendHomeSensorLogic(GCon handle, int nLogic);

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

    GCon m_handle;

    const char m_cGalilAxis;

    bool FindHome(MOTION_SPEED speed);

    void SetVirtualHomePosition();

    CCriticalSection m_CritSection;
};
