#ifndef __CIA402_DEFS_H__
#define __CIA402_DEFS_H__

#include "stdint.h"

/* CiA402 Object Dictionary indices */
#define CIA402_INDEX_CONTROLWORD                0x6040
#define CIA402_INDEX_STATUSWORD                 0x6041
#define CIA402_INDEX_OP_MODE                    0x6060
#define CIA402_INDEX_OP_MODE_DISPLAY            0x6061
#define CIA402_INDEX_POS_ACTUAL                 0x6064
#define CIA402_INDEX_FOLLOWING_ERROR_WINDOW     0x6065
#define CIA402_INDEX_VEL_ACTUAL                 0x606C
#define CIA402_INDEX_TARGET_POS                 0x607A
#define CIA402_INDEX_PROFILE_ACC                0x6083
#define CIA402_INDEX_PROFILE_DEC                0x6084
#define CIA402_INDEX_FOLLOWING_ERROR_ACTUAL     0x60F4
#define CIA402_INDEX_TARGET_VEL                 0x60FF

/* Statusword bits (0x6041) */
#define CIA402_STATUS_READY_TO_SWITCH_ON        (1 << 0)
#define CIA402_STATUS_SWITCHED_ON               (1 << 1)
#define CIA402_STATUS_OPERATION_ENABLED         (1 << 2)
#define CIA402_STATUS_FAULT                     (1 << 3)
#define CIA402_STATUS_VOLTAGE_ENABLED           (1 << 4)
#define CIA402_STATUS_QUICK_STOP                (1 << 5)
#define CIA402_STATUS_SWITCH_ON_DISABLED        (1 << 6)
#define CIA402_STATUS_WARNING                   (1 << 7)
#define CIA402_STATUS_REMOTE                    (1 << 9)
#define CIA402_STATUS_TARGET_REACHED            (1 << 10)
#define CIA402_STATUS_INTERNAL_LIMIT_ACTIVE     (1 << 11)
#define CIA402_STATUS_OMS_12                    (1 << 12)
#define CIA402_STATUS_OMS_13                    (1 << 13)

/* Controlword bits (0x6040) */
#define CIA402_CONTROL_SWITCH_ON                (1 << 0)
#define CIA402_CONTROL_ENABLE_VOLTAGE           (1 << 1)
#define CIA402_CONTROL_QUICK_STOP               (1 << 2)
#define CIA402_CONTROL_ENABLE_OPERATION         (1 << 3)
#define CIA402_CONTROL_FAULT_RESET              (1 << 7)
#define CIA402_CONTROL_HALT                     (1 << 8)

/* Mode specific control bits */
#define CIA402_CONTROL_PP_NEW_SET_POINT         (1 << 4)
#define CIA402_CONTROL_PP_CHANGE_IMM            (1 << 5)
#define CIA402_CONTROL_PP_ABS_REL               (1 << 6)
#define CIA402_CONTROL_HM_START                 (1 << 4)

#define CIA402_STATUS_MASK_OP_ENABLE            (0x0027)

#define CIA402_CMD_SHUTDOWN                     (CIA402_CONTROL_ENABLE_VOLTAGE | CIA402_CONTROL_QUICK_STOP)
#define CIA402_CMD_SWITCH_ON                    (CIA402_CONTROL_SWITCH_ON | CIA402_CONTROL_ENABLE_VOLTAGE | CIA402_CONTROL_QUICK_STOP)
#define CIA402_CMD_ENABLE_OP                    (CIA402_CMD_SWITCH_ON | CIA402_CONTROL_ENABLE_VOLTAGE | \
                                                 CIA402_CONTROL_QUICK_STOP | CIA402_CONTROL_ENABLE_OPERATION)

typedef enum
{
    STATE_NOT_READY_TO_SWITCH_ON,
    STATE_SWITCH_ON_DISABLED,
    STATE_READY_TO_SWITCH_ON,
    STATE_SWITCHED_ON,
    STATE_OPERATION_ENABLED,
    STATE_FAULT,
    STATE_QUICK_STOP_ACTIVE
} Motor_State_t;

typedef enum
{
    HOMING_IDLE,
    HOMING_START,
    HOMING_MOVING,
    HOMING_DONE
} Homing_State_t;

#endif
