#pragma once
#include "lkscard.h"

//扩展位
#define LC_EMERGENCE  LC_PIN0
#define LC_START      LC_PIN4
#define LC_PAUSED     LC_PIN5

//#define LC_PAUSE      LC_PIN4


enum
{
	SM_OK=0,
	SM_FAILED=-1,
	SM_COMM_ERROR=-2,
	SM_COMM_TIMEOUT=-3,
	SM_OUT_OF_RANGE=-4,
	SM_DEV_NOT_READY=-5,
	SM_IN_EMERGENCE=-6,
	SM_SYS_PAUSED =-7,
};

#define SM_NONE  SM_OK

//设备状态
enum
{
	SMS_UNKNOWN=-1,
	SMS_STANDBY=0,
	SMS_EMERGENCE=1,
	SMS_RUNNING=3,
	SMS_PAUSED=4,
	SMS_RESET   =33,
};

#define SM_WM_COMM_ERROR  (WM_USER + 1023)
#define SM_WM_REPORT_POS  (WM_USER + 103)
#define SM_WM_EMERGENCE   (WM_USER + 1120)
#define SM_WM_PAUSED      (WM_USER + 1121)


#define SM_ENABLE_EMERGENCE   (1 )   
#define SM_ENABLE_PAUSE       (1<<1) 
#define SM_RESET4PAUSE        (1<<2) 
#define SM_AUTO_RESET         (1<<3) 


 


#define SM_POS_ACCURANCE     10



class ROBBASE_API CSmtMotion :	public CLksCard
{
public:
	CSmtMotion(void);
	virtual ~CSmtMotion(void);

	virtual BOOL	Start();
	virtual void	Stop();

	BOOL ResetZero( );
	BOOL StepX(int dir,  double dist );
	BOOL StepY(int dir,  double dist );
	 
	 
	void   SetRange(int axis, double range);
	double GetRange(int axis);

	//手动紧急停止
	BOOL  Estop();
	//手动复位
	BOOL  Restore();
	//BOOL  Pause();
	//BOOL  Resume();
	 

	double GetCurPos(int axis);

	int GetCurState() ;
	int GetLastError(){ return m_nLastError; };

public:
	int m_nSpeed; //速度0-100,默认50
	int m_nAcc;   //加速度目前忽略
	HWND m_hParent;

	int m_flags;
//	BOOL m_bWaitWhenPaused;

//内部使用
public:
	void DetectExternalSignal(void);

private:
	BOOL CheckPauseEmergence(int axis, int dir, int speed, int acc, BOOL bCheckPause);
	void SetCurState(int state);

	BOOL ExcGoHome(BOOL bIgnorePause);
	

	CRITICAL_SECTION m_cs;

	double m_nRangeX, m_nRangeY;
	double m_nCurX, m_nCurY;
	 
	int    m_nState;
	int    m_nLastError;
	BOOL   m_bPaused;

	//int    m_nAxisInMotion;
	//int    m_nCurDirX;
	//int    m_nCurDirY;

	

	HANDLE m_hExternalThread;
	
  
	BOOL WaitHomeMotion(BOOL bIgnorePause);
 
	BOOL WaitAxisMotion(int axis, int dir,int speed, int acc);
};

