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

#pragma once

#include <string>

#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 std::string g_strHEX[] 
{
    "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(std::string 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);
    std::string ReadStroke(char *cStation);         // V1.14 31Jul06
    std::string ReadVerticalOffset(char *cStation); // V1.14 31Jul06
    std::string ReadCassetteDepth(char *cStation);  // V1.14 31Jul06
    std::string ReadScannedData(int nOption = 0);
    std::string GetReceiveData();
    int     WaitForScanningComplete(DWORD dwTimeout);

    CRobotComm();
    virtual ~CRobotComm();

private:
    void    UpdateCurrentCommand(std::string csCmd);
    int     GetIntValue(std::string csValue);
    int     ConvertHexToInt(std::string csHexString);
    std::string GetCommand();
    void    SetCommand(std::string csCmd, bool bUpdateCommand = 0);
    bool    IsCurrentCommand(std::string csCmd);
    bool    TraceCurrentCommand(std::string 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;

    std::string m_strCurrentCommand;
    bool    m_bCheckPrealignBusy;
    bool    m_bCheckRobotBusy;
    std::string m_strScannedData;
    std::string m_strCmd;
    std::string m_strStrokeData, m_strVerticalOffsetData, m_strCassetteDepthData; // V1.14 31Jul06

    bool m_bVacuumSensed;
    bool m_bPrealignVacuumSensed;
};

