/*
 * @Date: 2025-07-02 13:06:24
 * @Author: mypx
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-07-22 11:44:02
 * @FilePath: h02_basic_control.c
 * @Description: 
 * Copyright (c) 2025 by mypx, All Rights Reserved.
 */
#include "h02_basic_control.h"

/**
 * @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)
{
    uint16_t value = 0;
    SV630P_ARGS_CHECK(dev);
    if (dev->ops->read_holding_regs(dev, REG_H02_00_CONTROL_MODE, 1, &value) == SV630P_OK)
    {
        *src = (SV630P_ControlMode)value;
        return SV630P_OK;
    }
    return SV630P_BUS_ERR;
}

/**
 * @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)
{
    SV630P_ARGS_CHECK(dev);
    return dev->ops->write_single_reg(dev, REG_H02_00_CONTROL_MODE, (uint16_t)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)
{
    uint16_t value = 0;
    SV630P_ARGS_CHECK(dev);
    if (dev->ops->read_holding_regs(dev, REG_H02_02_ROT_DIR_SELECT, 1, &value) == SV630P_OK)
    {
        *dir = (SV630P_RotDir)value;
        return SV630P_OK;
    }
    return SV630P_BUS_ERR;
}

/**
 * @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)
{
    SV630P_ARGS_CHECK(dev);
    return dev->ops->write_single_reg(dev, REG_H02_02_ROT_DIR_SELECT, (uint16_t)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)
{
    uint16_t value = 0;
    SV630P_ARGS_CHECK(dev);
    if (dev->ops->read_holding_regs(dev, REG_H02_31_SYSTEM_PARAMETER_INIT, 1, &value) == SV630P_OK)
    {
        *param = (SV630P_SystemParamInit)value;
        return SV630P_OK;
    }
    return SV630P_BUS_ERR;
}

/**
 * @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)
{
    SV630P_ARGS_CHECK(dev);
    return dev->ops->write_single_reg(dev, REG_H02_31_SYSTEM_PARAMETER_INIT, (uint16_t)param);
}