/*
 * @Author: WZH jinxiangwzh@163.com
 * @Date: 2023-04-17 14:52:50
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-08-15 15:28:56
 * @FilePath: et_byte_conversion.h
 * @Description: 
 * 
 * Copyright (c) 2023 by wzh, All Rights Reserved. 
 */
#ifndef __ETK_BYTE_CONV_H__
#define __ETK_BYTE_CONV_H__
#include <stdint.h>
#include <stdbool.h>
#include "etk_utils.h"

#ifndef ET_USING_AUTO_LITTLE_ENDIAN
#define ET_USING_AUTO_LITTLE_ENDIAN 1
#endif

#if (ET_USING_AUTO_LITTLE_ENDIAN == 0)
#ifndef ET_USING_LITTLE_ENDIAN
#define ET_USING_LITTLE_ENDIAN 1
#endif
#endif

#ifdef __cplusplus
extern "C"
{
#endif
/**
 * @brief Check the byte order of the system.
 * @return 1 if the system is little endian, 0 if big endian.
 */
bool et_is_little_endian(void);

/**
 * @brief Convert an array of bytes to a 32-bit unsigned integer.
 * @param arr Pointer to the byte array.
 * @return The converted 32-bit unsigned integer.
 */
uint32_t et_array_to_uint32(const uint8_t *arr);

/**
 * @brief Convert a 32-bit unsigned integer to an array of bytes.
 * @param n The 32-bit unsigned integer to convert.
 * @param arr Pointer to the byte array to store the result.
 * @return The number of bytes written to the array.
 */
int et_uint32_to_array(uint32_t n, uint8_t *arr);

/**
 * @brief Convert an array of bytes to a 32-bit signed integer.
 * @param arr Pointer to the byte array.
 * @return The converted 32-bit signed integer.
 */
int32_t et_array_to_int32(const uint8_t *arr);

/**
 * @brief Convert a 32-bit signed integer to an array of bytes.
 * @param n The 32-bit signed integer to convert.
 * @param arr Pointer to the byte array to store the result.
 * @return The number of bytes written to the array.
 */
int et_int32_to_array(int32_t n, uint8_t *arr);

/**
 * @brief Convert a 16-bit unsigned integer to an array of bytes.
 * @param n The 16-bit unsigned integer to convert.
 * @param arr Pointer to the byte array to store the result.
 * @return The number of bytes written to the array.
 */
int et_uint16_to_array(uint16_t n, uint8_t *arr);

/**
 * @brief Convert an array of bytes to a 16-bit unsigned integer.
 * @param arr Pointer to the byte array.
 * @return The converted 16-bit unsigned integer.
 */
uint16_t et_array_to_uint16(const uint8_t *arr);

/**
 * @brief Convert a 16-bit signed integer to an array of bytes.
 * @param n The 16-bit signed integer to convert.
 * @param arr Pointer to the byte array to store the result.
 * @return The number of bytes written to the array.
 */
int et_int16_to_array(int16_t n, uint8_t *arr);

/**
 * @brief Convert an array of bytes to a 16-bit signed integer.
 * @param arr Pointer to the byte array.
 * @return The converted 16-bit signed integer.
 */
int16_t et_array_to_int16(const uint8_t *arr);

/**
 * @brief Convert an array of bytes to a 64-bit unsigned integer.
 * @param arr Pointer to the byte array.
 * @return The converted 64-bit unsigned integer.
 */
uint64_t et_array_to_uint64(const uint8_t *arr);

/**
 * @brief Convert a 64-bit unsigned integer to an array of bytes.
 * @param n The 64-bit unsigned integer to convert.
 * @param arr Pointer to the byte array to store the result.
 * @return The number of bytes written to the array.
 */
int et_uint64_to_array(uint64_t n, uint8_t *arr);

/**
 * @brief Convert an array of bytes to a floating-point number.
 * @param array Pointer to the byte array.
 * @return The converted floating-point number.
 */
float et_array_to_float(const uint8_t *array);

/**
 * @brief Convert a floating-point number to an array of bytes.
 * @param f The floating-point number to convert.
 * @param array Pointer to the byte array to store the result.
 * @return The number of bytes written to the array.
 */
int et_float_to_array(float f, uint8_t *array);

/**
 * @brief Convert two Modbus registers to a floating-point number in ABCD byte order.
 * @param reg_high The high 16-bit register.
 * @param reg_low The low 16-bit register.
 * @return The converted floating-point number.
 */
float et_mb_to_float_abcd(uint16_t reg_high, uint16_t reg_low);

/**
 * @brief Convert two Modbus registers to a floating-point number in CDAB byte order.
 * @param reg_high The high 16-bit register.
 * @param reg_low The low 16-bit register.
 * @return The converted floating-point number.
 */
float et_mb_to_float_cdab(uint16_t reg_high, uint16_t reg_low);

/**
 * @brief Convert two Modbus registers to a floating-point number in BADC byte order.
 * @param reg_high The high 16-bit register.
 * @param reg_low The low 16-bit register.
 * @return The converted floating-point number.
 */
float et_mb_to_float_badc(uint16_t reg_high, uint16_t reg_low);

/**
 * @brief Convert two Modbus registers to a floating-point number in DCBA byte order.
 * @param reg_high The high 16-bit register.
 * @param reg_low The low 16-bit register.
 * @return The converted floating-point number.
 */
float et_mb_to_float_dcba(uint16_t reg_high, uint16_t reg_low);

/**
 * @brief Convert a floating-point number to two Modbus registers in ABCD byte order.
 * @param f The floating-point number to convert.
 * @param reg_high Pointer to store the high 16-bit register.
 * @param reg_low Pointer to store the low 16-bit register.
 * @return The number of registers written (always 2).
 */
int et_float_to_mb_abcd(float f, uint16_t *reg_high, uint16_t *reg_low);

/**
 * @brief Convert a floating-point number to two Modbus registers in CDAB byte order.
 * @param f The floating-point number to convert.
 * @param reg_high Pointer to store the high 16-bit register.
 * @param reg_low Pointer to store the low 16-bit register.
 * @return The number of registers written (always 2).
 */
int et_float_to_mb_cdab(float f, uint16_t *reg_high, uint16_t *reg_low);

/**
 * @brief Convert a floating-point number to two Modbus registers in BADC byte order.
 * @param f The floating-point number to convert.
 * @param reg_high Pointer to store the high 16-bit register.
 * @param reg_low Pointer to store the low 16-bit register.
 * @return The number of registers written (always 2).
 */
int et_float_to_mb_badc(float f, uint16_t *reg_high, uint16_t *reg_low);

/**
 * @brief Convert a floating-point number to two Modbus registers in DCBA byte order.
 * @param f The floating-point number to convert.
 * @param reg_high Pointer to store the high 16-bit register.
 * @param reg_low Pointer to store the low 16-bit register.
 * @return The number of registers written (always 2).
 */
int et_float_to_mb_dcba(float f, uint16_t *reg_high, uint16_t *reg_low);
#ifdef __cplusplus
}
#endif
#endif // @page end
