/*
 * @Date: 2025-06-26 09:31:38
 * @Author: mypx
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-07-02 15:14:09
 * @FilePath: h03_basic_control.h
 * @Description: 
 * Copyright (c) 2025 by mypx, All Rights Reserved.
 */
#ifndef __H02_BASIC_CONTROL_H__
#define __H02_BASIC_CONTROL_H__
#include "sv_common.h"

#define REG_H02_00_CONTROL_MODE 0x0200 // Control mode selection

/**
 * @brief Servo control mode enumeration
 * @details Corresponds to H02.00 parameter (control mode selection)
 */
typedef enum
{
    SV630P_CONTROL_MODE_SPEED           = 0, // Speed mode
    SV630P_CONTROL_MODE_POSITION        = 1, // Position mode
    SV630P_CONTROL_MODE_TORQUE          = 2, // Torque mode
    SV630P_CONTROL_MODE_TORQUE_SPEED    = 3, // Torque <-> Speed mode
    SV630P_CONTROL_MODE_SPEED_POSITION  = 4, // Speed <-> Position mode
    SV630P_CONTROL_MODE_TORQUE_POSITION = 5, // Torque <-> Position mode
    SV630P_CONTROL_MODE_MIXED           = 6  // Torque <-> Speed <-> Position mixed mode
} SV630P_ControlMode;

#define REG_H02_01_ABS_POSITION_SYSTEM 0x0201 // Absolute position detection system selection
#define REG_H02_02_ROT_DIR_SELECT      0x0202 // Rotation direction selection

/**
 * @brief Rotation direction selection
 * @details Set value and direction description:
 *          0: CCW is positive direction. When positive command is given, viewed from motor shaft side, motor rotates CCW (counterclockwise).
 *          1: CW is positive direction. When positive command is given, viewed from motor shaft side, motor rotates CW (clockwise).
 */
typedef enum
{
    SV630P_ROT_DIR_CCW = 0, ///< CCW is positive direction (counterclockwise)
    SV630P_ROT_DIR_CW  = 1  ///< CW is positive direction (clockwise)
} SV630P_RotDir;

#define REG_H02_03_OUTPUT_PULSE_PHASE                    0x0203 // Output pulse phase
#define REG_H02_05_SERVO_OFF_STOP_MODE                   0x0205 // Servo OFF stop mode selection
#define REG_H02_06_FAULT_NO2_STOP_MODE                   0x0206 // Fault NO.2 stop mode selection
#define REG_H02_07_OVERTR_TRAVEL_STOP_MODE               0x0207 // Overtravel stop mode selection
#define REG_H02_08_FAULT_NO1_STOP_MODE                   0x0208 // Fault NO.1 stop mode selection
#define REG_H02_09_BRAKE_OUTPUT_DELAY                    0x0209 // Brake output ON to command receive delay
#define REG_H02_10_STATIONARY_BRAKE_DELAY                0x020A // Stationary brake output OFF to motor power-off delay
#define REG_H02_11_ROTATING_BRAKE_SPEED_THRESHOLD        0x020B // Rotating brake output OFF speed threshold
#define REG_H02_12_ROTATING_SERVO_OFF_BRAKE_DELAY        0x020C // Rotating servo enable OFF to brake output OFF delay
#define REG_H02_14_POWER_OFF_STOP_SPEED_CONDITION        0x020E // Stop mode and stop state switching speed condition value
#define REG_H02_15_LED_WARNING_DISPLAY                   0x020F // LED warning display selection
#define REG_H02_17_POWER_OFF_ZERO_SPEED_ENABLE           0x0211 // Main circuit power-off zero speed stop enable
#define REG_H02_18_SERVO_ON_FILTER_TIME                  0x0212 // Servo enable (S-ON) filter time constant
#define REG_H02_19_SERVO_ON_POWER_BRAKE_DELAY            0x0213 // Servo ON power brake delay open time
#define REG_H02_20_DB_RELAY_COIL_DELAY                   0x0214 // DB relay coil energizing delay
#define REG_H02_21_BRAKE_RESISTOR_MIN_VALUE              0x0215 // Minimum allowable brake resistor value
#define REG_H02_22_BUILT_IN_BRAKE_RESISTOR_POWER         0x0216 // Built-in brake resistor power
#define REG_H02_23_BUILT_IN_BRAKE_RESISTOR_RESISTANCE    0x0217 // Built-in brake resistor resistance
#define REG_H02_24_RESISTOR_HEAT_DISSIPATION_COEFFICIENT 0x0218 // Resistor heat dissipation coefficient
#define REG_H02_25_BRAKE_RESISTOR_SETTING                0x0219 // Brake resistor setting
#define REG_H02_26_EXTERNAL_BRAKE_RESISTOR_POWER         0x021A // External brake resistor power
#define REG_H02_27_EXTERNAL_BRAKE_RESISTOR_RESISTANCE    0x021B // External brake resistor resistance
#define REG_H02_28_VOLTAGE_LEVEL_MIN_BUS_VOLTAGE         0x021C // 220V voltage level minimum bus voltage
#define REG_H02_30_USER_PASSWORD                         0x021E // User password
#define REG_H02_31_SYSTEM_PARAMETER_INIT                 0x021F // System parameter initialization
typedef enum
{
    NO_OPERATION = 0, ///< No operation, does not perform any initialization or clearing
    /* Restore factory settings, all parameters except H00 and H01 groups are reset to factory values*/
    RESTORE_FACTORY_DEFAULTS = 1,
    CLEAR_FAULT_RECORDS      = 2 ///< Clear fault records, clears the last 10 fault and warning codes
} SV630P_SystemParamInit;
#define REG_H02_32_PANEL_DEFAULT_DISPLAY_FUNCTION  0x0220 // Panel default display function
#define REG_H02_34_CAN_SOFTWARE_VERSION            0x0222 // CAN software version
#define REG_H02_35_PANEL_DISPLAY_REFRESH_FREQUENCY 0x0223 // Panel display refresh frequency
#define REG_H02_37_BUS_VOLTAGE_HOLD_TIME           0x0225 // Bus voltage hold time
#define REG_H02_41_MANUFACTURER_PASSWORD           0x0229 // Manufacturer password

#if defined(__cplusplus)
extern "C"
{
#endif

/**
 * @brief Read control mode from servo
 * @param dev Pointer to servo handle
 * @param src Pointer to store the read control mode
 * @return SV630P_OK on success, error code otherwise
 */
int sv630p_h02_00_read_control_mode(sv630p_handle_t *dev, SV630P_ControlMode *src);

/**
 * @brief Write control mode to servo
 * @param dev Pointer to servo handle
 * @param src Control mode to write
 * @return SV630P_OK on success, error code otherwise
 * @note Takes effect immediately, can be changed when stopped
 */
int sv630p_h02_00_write_control_mode(sv630p_handle_t *dev, SV630P_ControlMode src);

/**
 * @brief Read rotation direction from servo
 * @param dev Pointer to servo handle
 * @param dir Pointer to store the read rotation direction
 * @return SV630P_OK on success, error code otherwise
 */
int sv630p_h02_02_read_rot_dir(sv630p_handle_t *dev, SV630P_RotDir *dir);

/**
 * @brief Write rotation direction to servo
 * @param dev Pointer to servo handle
 * @param dir Rotation direction to write
 * @return SV630P_OK on success, error code otherwise
 * @note Takes effect immediately, can be changed when stopped
 */
int sv630p_h02_02_write_rot_dir(sv630p_handle_t *dev, SV630P_RotDir dir);

/**
 * @brief Read system parameter initialization value from servo
 * @param dev Pointer to servo handle
 * @param param Pointer to store the read value
 * @return SV630P_OK on success, error code otherwise
 */
int sv630p_h02_31_read_system_param_init(sv630p_handle_t *dev, SV630P_SystemParamInit *param);

/**
 * @brief Write system parameter initialization value to servo
 * @param dev Pointer to servo handle
 * @param param Value to write (restore factory, clear fault, etc.)
 * @return SV630P_OK on success, error code otherwise
 */
int sv630p_h02_31_write_system_param_init(sv630p_handle_t *dev, SV630P_SystemParamInit param);

#if defined(__cplusplus)
}
#endif

#endif // __SV_BASIC_CONTROL_H__
