#pragma once

#include "utility.h"

// Input Card
#define PCI_7433_ADLINK 0

// Input & Output Card
#define PCI_7296_ADLINK     1
#define PCI_9112_ADLINK     2
#define PCI_7851_ADLINK     3
#define PCI_7432_ADLINK     4 // 32 in, 32 out
#define PCI_9222_ADLINK     5 // AO, AI, Encoder, DI, DO card
#define PCI_7856_HSL_ADLINK 6
// advantech
#define PCI_1203_ETHERCAT_IO_ADVANTECH 7
// adlink ethercat
#define PCIe_ADLINK_ETHERCAT_IO 8
#define MAX_INPUT_CARD_TYPE     9

// 7856 Port Definition
#define PCI_7856_HSL_PORT       0
#define PCI_7856_MOTIONNET_PORT 1

// Advantech EtherCAT Ring Definition
#define PCI_1203_MOTION_RING 0
#define PCI_1203_IO_RING     1

// Output Card
#define PCI_7434_ADLINK      0
#define MAX_OUTPUT_CARD_TYPE MAX_INPUT_CARD_TYPE

// note that the output array size is set based on 6*16*144 = 13824 output pts total
// 1 card type can have max 16*144=2304 output pts
#define MAX_CARD_TYPE            MAX_OUTPUT_CARD_TYPE
#define MAX_CARD_FOR_EACH_TYPE   16
#define MAX_POINTS_FOR_EACH_CARD 144

// for remote IO 7851, max IO 63*32=2016. This value must be smaller than 2304 (as above).
// for remote IO 7856 (HSL), max IO 63*32=2016. This value must be smaller than 2304 (as above).
#define MAX_SLAVE_ID                 63
#define MAX_POINTS_FOR_EACH_SLAVE_ID 32

// ETEL
#define MAX_ETEL_AXIS 32

enum CARD_TYPE
{
    INPUT_CARD,
    OUTPUT_CARD,
    MOTION_CARD,
    ENCODER_CARD
};

enum INPUT_STATE
{
    INPUT_UNKNOWN,
    INPUT_ON,
    INPUT_OFF,
};

enum INPUT_CARD_INIT_RESULT
{
    INPUT_CARD_INIT_FAIL,
    INPUT_CARD_INIT_PASS,
};

enum INPUT_CARD_CLOSE_RESULT
{
    INPUT_CARD_CLOSE_FAIL,
    INPUT_CARD_CLOSE_PASS,
};

enum OUTPUT_CARD_INIT_RESULT
{
    OUTPUT_CARD_INIT_FAIL,
    OUTPUT_CARD_INIT_PASS,
};

enum OUTPUT_CARD_CLOSE_RESULT
{
    OUTPUT_CARD_CLOSE_FAIL,
    OUTPUT_CARD_CLOSE_PASS,
};

using stCard = struct _stCard
{
    std::string strCardType; // standardise to std for portability
    BOOL        Initialised;
    // ctor
    _stCard(const std::string& _strCardType = "NoCard", BOOL _Initialised = FALSE)
        : strCardType(_strCardType), Initialised(_Initialised)
    {
    }
};
//
using union_uint = union _union_uint
{
    unsigned int uintValue;
    struct
    {
        unsigned short lo;
        unsigned short hi;
    } s;
    unsigned char byte[4];

    // ctor
    _union_uint()
    {
        uintValue = 0;
    }
};

// Advantech EtherCAT
// Bit Definition
// 0 RDY-- RDY pin input
// 1 ALM -- Alarm signal input
// 2 LMT+-- Limit switch+
// 3 LMT-- Limit switch-
// 4 ORG-- Original switch
// 5 DIR -- DIR output
// 6 EMG -- Emergency signal input
// 7 PCS -- PCS signal input
// 8 ERC -- Output offset counter clear signal to servo motor drvier
// 9 EZ -- Encoder Z signal
// 10 CLR -- External output toward clear position counter
// 11 LTC -- Latch signal input
// 12 SD -- Deceleration signal input
// 13 INP -- In-position signal input
// 14 14: SVON -- Servo On (OUT6)
// 15 ALRM -- Alarm reset output status
// 16 SLMT+ -- Softwarte limit+
// 17 SLMT-- Softwarte limit -
// 18 CMP--Compare signal
enum ACM_AX_MIO
{
    MIO_RDY = 0x00000001, // AX_MOTION_IO_RDY
    MIO_ALM = 0x00000002, // AX_MOTION_IO_ALM
    MIO_PEL = 0x00000004, // AX_MOTION_IO_LMTP
    MIO_MEL = 0x00000008,

    MIO_ORG = 0x00000010,
    MIO_DIR = 0x00000020,
    MIO_EMG = 0x00000040,
    MIO_PCS = 0x00000080,

    MIO_ERC = 0x00000100,
    MIO_EZ  = 0x00000200,
    MIO_CLR = 0x00000400,
    MIO_LTC = 0x00000800,

    MIO_SD      = 0x00001000,
    MIO_INP     = 0x00002000,
    MIO_SVON    = 0x00004000,
    MIO_ALM_RST = 0x00008000,

    MIO_SPLIM = 0x00010000,
    MIO_SNLIM = 0x00020000,
    MIO_CMP   = 0x00040000,
};

// Bit Definition
// 0 Stop. Stop
// 1 Res1. Reserved
// 2 WaitERC. Wait ERC finished
// 3 Res2. Reserved
// 4 CorrectBksh. Correcting Backlash;
// 5 Res3. Reserved
// 6 InFA. Feeding in return velocity= FA
// 7 InFL. Feeding in StrVel speed=FL;
// 8 InACC. Accelerating
// 9 WaitINP. Wait in position.
enum ACM_AX_MST
{
    MST_STOP     = 0x1,
    MST_RES1     = 0x2, // Reserved
    MST_WAIT_ERC = 0x4,
    MST_RES2     = 0x8, // Reserved

    MST_BACKLASH_CORRECTION = 0x10, // Correcting Backlash;
    MST_RES3                = 0x20, // Reserved
    MST_FEEDING_FA          = 0x40, // Feeding in return velocity= FA
    MST_FEADING_FL          = 0x80, // Feeding in StrVel speed=FL;

    MST_ACCEL    = 0x100, // 8 InACC. Accelerating
    MST_WAIT_INP = 0x200, // Wait in position.

};

// Galil
enum Galil_Axis
{
    AXIS_X,
    AXIS_Y,
    AXIS_Z,
    AXIS_W,
    AXIS_E,
    AXIS_F,
    AXIS_G,
    AXIS_H,
    MAX_GALIL_AXIS
};

const std::array<char, MAX_GALIL_AXIS> GalilAxis = {
    'X', 'Y', 'Z', 'W', 'E', 'F', 'G', 'H'
};

