// Robot.h: interface for the CRobot class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ROBOT_COMM_H__6FE8AE0D_5CAC_43F9_B267_2A5399FA5798__INCLUDED_)
#define AFX_ROBOT_COMM_H__6FE8AE0D_5CAC_43F9_B267_2A5399FA5798__INCLUDED_

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

#include "Scanner.h"
#include "Utility.h"

#ifdef  BUILD_COMMDLL
#define     COMMDLL_DLLCLASS    __declspec(dllexport)
#else
#define     COMMDLL_DLLCLASS    __declspec(dllimport)
#endif

enum SCAN_RESULT
{
    SCAN_UNKNOWN,
    SCAN_COMPLETE,
    SCAN_IN_PROGRESS,
    SCAN_MOTION_ERROR,
    SCAN_PROTRUDE_WAFER,
    SCAN_CROSS_SLOTTED_WAFER,
};

enum ROBOT_POSITION
{
    T_POSITION,
    R_POSITION,
    Y_POSITION,
    Z_POSITION,
    MAX_ROBOT_POSITION,
};

struct stPosition
{
    double dPos[MAX_ROBOT_POSITION];
};

const CString g_csHEX[] =
{
    "0",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
};

class COMMDLL_DLLCLASS CRobotComm : public CScanner, public CMyWait
{
public:
    bool SetAlignAngle(double dAngle);
    bool DisableTrajectoryMode();
    bool EnableTrajectoryMode();
    bool MoveStraight(int nDistance);
    bool SetMoveSpeed(int nSpeed);
    void ResetCommand();
    void Disconnect(); // chan 8Dec05
    bool MoveToCustomHome();
    bool OffPrealignVacuum();
    bool OnPrealignVacuum();
    bool OffVacuum();
    bool OnVacuum();
    bool SendCommand();
    bool Send(CString csMsg); // chan 7Dec05
    bool ScanCassetteFast(char cStation, int nOption = 1);
    bool Prealign();
    bool SetWaferDiameter(int nDia);
    bool OffRobot();
    bool OnRobot();
    void ResetReplyEvents();
    bool WaitAck();
    void Stop();
    bool HomeAllAxis();
    bool SetInformMode(int nMode);
    void ReceiveComm(const char *cpcBuffer, int nSize);
    bool Input(int nPoint);
    bool Output(int nOutput, int nState);
    bool MoveToPoint(double dT_Position, double dR_Position, double dZ_Position, DWORD dwTimeout);
    bool MoveAbs(char cAxis, double dPosition, DWORD dwTimeout);
    bool MoveRel(char cAxis, double dDistance, DWORD dwTimeout);
    bool MoveToPickup(char cStation, int nSlot, int nOption, DWORD dwTimeout, int nArm = 0);
    bool MoveToCoordinates(char cStation, int nSlot, int nOption, DWORD dwTimeout, int nArm = 0);
    bool MoveToStartScan(char cStation, int nSlot, int nIndex, DWORD dwTimeout, int nArm = 0);
    bool MoveToApproach(char cStation, int nSlot, int nOption, DWORD dwTimeout, int nArm = 0);
    bool MoveToStation(char cStation, int nSlot, DWORD dwTimeout, int nArm = 0);
    bool PutWafer(char cStation, int nSlot, int nOption, DWORD dwTimeout, int nArm = 0);
    bool GetWafer(char cStation, int nSlot, int nOption, DWORD dwTimeout, int nArm = 0);

// read robot status
    bool    CheckOnLine();
    bool    IsPrealignVacuumSensed();
    bool    IsVacuumSensed();
    bool    GetHomeStatus();
    bool    WaitForHomeComplete(DWORD dwTimeout);
    bool    WaitForRobotComplete(DWORD dwTimeout, bool bPrealigner = false);
    bool    WaitForPrealignComplete(DWORD dwTimeout);
    CString ReadStroke(char *cStation);         // V1.14 31Jul06
    CString ReadVerticalOffset(char *cStation); // V1.14 31Jul06
    CString ReadCassetteDepth(char *cStation);  // V1.14 31Jul06
    CString ReadScannedData(int nOption = 0);
    CString GetReceiveData();
    int     WaitForScanningComplete(DWORD dwTimeout);

    CRobotComm();
    virtual ~CRobotComm();

private:
    void    UpdateCurrentCommand(CString csCmd);
    int     GetIntValue(CString csValue);
    int     ConvertHexToInt(CString csHexString);
    CString GetCommand();
    void    SetCommand(CString csCmd, bool bUpdateCommand = 0);
    bool    IsCurrentCommand(CString csCmd);
    bool    TraceCurrentCommand(CString csCmd, int iCmdLength = 3); // V1.14 31Jul06-compare command only.
    bool   m_bWaitForTerminator2;
    bool   m_bWaitForTerminator3;
    HANDLE m_hAck;
    HANDLE m_hNck;
    HANDLE m_hPrealignComplete;
    HANDLE m_hStopRobot;
    HANDLE m_hRobotComplete;
    HANDLE m_hScanningComplete;
    HANDLE m_hRobotHomeComplete;
    HANDLE m_hReadPositionComplete;
    HANDLE m_hReadVacuumComplete;
    HANDLE m_hReadPrealignVacuumComplete;
    HANDLE m_hStatusReceived;
    HANDLE m_hReadStrokeComplete, m_hReadVerticalOffsetComplete, m_hReadCassetteDepthComplete; // V1.14 31Jul06

    int m_nScanningResult;

    CString m_csCurrentCommand;
    bool    m_bCheckPrealignBusy;
    bool    m_bCheckRobotBusy;
    CString m_csScannedData;
    CString m_csCmd;
    CString m_csStrokeData, m_csVerticalOffsetData, m_csCassetteDepthData; // V1.14 31Jul06

    bool m_bVacuumSensed;
    bool m_bPrealignVacuumSensed;
};

#endif // !defined(AFX_ROBOT_COMM_H__6FE8AE0D_5CAC_43F9_B267_2A5399FA5798__INCLUDED_)
