Files
ch32v203-eth-node/include/utils.h
2024-11-12 00:55:52 +06:00

52 lines
1.1 KiB
C

#ifndef UTILS_H
#define UTILS_H
#include <stdint.h>
/**
* @brief Combines two bytes into a 16-bit word
* @param hi High byte
* @param lo Low byte
* @return Combined 16-bit word
*/
static inline uint16_t to_word(uint8_t hi, uint8_t lo) {
return (hi << 8) | lo;
}
/**
* @brief Splits a 16-bit word into two bytes
* @param val Value to split
* @param hi Pointer to store high byte
* @param lo Pointer to store low byte
*/
static inline void to_bytes(uint16_t val, uint8_t* hi, uint8_t* lo) {
*hi = (val >> 8) & 0xFF;
*lo = val & 0xFF;
}
/**
* @brief Parses a decimal number from a string
* @param str String to parse
* @return Parsed decimal number
*/
static inline uint8_t parse_decimal(const char* str) {
uint8_t num = 0;
while (*str >= '0' && *str <= '9') {
num = num * 10 + (*str - '0');
str++;
}
return num;
}
/**
* @brief Extracts the node number from an ID string
* @param id ID string to extract from
* @return Extracted node number
*/
static inline uint8_t parse_node_number(const char* id) {
const char* last_dash = strrchr(id, '-');
return last_dash ? parse_decimal(last_dash + 1) : 0;
}
#endif // UTILS_H