107 lines
2.8 KiB
C
107 lines
2.8 KiB
C
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
/**
|
|
* @brief Write a 8 bit unsigned int to the buffer
|
|
*
|
|
* @param buf The buffer to write to
|
|
* @param value The 8 bit unsigned int to write
|
|
* @param index The index read/write pointer
|
|
*/
|
|
void buf_append_u8(uint8_t *buf, uint8_t value, size_t *index) {
|
|
buf[(*index)++] = value;
|
|
}
|
|
|
|
/**
|
|
* @brief Write a 16 bit signed int to the buffer
|
|
*
|
|
* @param buf The buffer to write to
|
|
* @param value The 16 bit signed int to write
|
|
* @param index The index read/write pointer
|
|
*/
|
|
void buf_append_i16(uint8_t *buf, int16_t value, size_t *index) {
|
|
buf[(*index)++] = (value >> 8) & 0xFF;
|
|
buf[(*index)++] = value & 0xFF;
|
|
}
|
|
|
|
/**
|
|
* @brief Write a 32 bit signed int to the buffer
|
|
*
|
|
* @param buf The buffer to write to
|
|
* @param value The 32 bit signed int to write
|
|
* @param index The index read/write pointer
|
|
*/
|
|
void buf_append_i32(uint8_t *buf, int32_t value, size_t *index) {
|
|
buf[(*index)++] = (value >> 24) & 0xFF;
|
|
buf[(*index)++] = (value >> 16) & 0xFF;
|
|
buf[(*index)++] = (value >> 8) & 0xFF;
|
|
buf[(*index)++] = value & 0xFF;
|
|
}
|
|
|
|
/**
|
|
* @brief Write a 32 bit float to the buffer
|
|
*
|
|
* @param buf The buffer to write to
|
|
* @param value The 32 bit float to write
|
|
* @param index The index read/write pointer
|
|
*/
|
|
void buf_append_f32(uint8_t *buf, float value, size_t *index) {
|
|
uint32_t u;
|
|
memcpy(&u, &value, sizeof(float)); // Safe conversion
|
|
buf_append_i32(buf, (int32_t)u, index);
|
|
}
|
|
|
|
/**
|
|
* @brief Read a 8 bit unsigned int from the buffer
|
|
*
|
|
* @param buf The buffer to read from
|
|
* @param index The index read/write pointer
|
|
* @return 8 bit unsigned int
|
|
*/
|
|
uint8_t buf_read_u8(const uint8_t *buf, size_t *index) {
|
|
return buf[(*index)++];
|
|
}
|
|
|
|
/**
|
|
* @brief Read a 16 bit int from the buffer
|
|
*
|
|
* @param buf The buffer to read from
|
|
* @param index The index read/write pointer
|
|
* @return 16 bit signed int
|
|
*/
|
|
uint8_t buf_read_i16(const uint8_t *buf, size_t *index) {
|
|
int16_t val = 0;
|
|
val |= ((int16_t)buf[(*index)++]) << 8;
|
|
val |= ((int16_t)buf[(*index)++]);
|
|
return val;
|
|
}
|
|
|
|
/**
|
|
* @brief Read a 32 bit int from the buffer
|
|
*
|
|
* @param buf The buffer to read from
|
|
* @param index The index read/write pointer
|
|
* @return 32 bit signed int
|
|
*/
|
|
int32_t buf_read_i32(const uint8_t *buf, size_t *index) {
|
|
int32_t val = 0;
|
|
val |= ((int32_t)buf[(*index)++]) << 24;
|
|
val |= ((int32_t)buf[(*index)++]) << 16;
|
|
val |= ((int32_t)buf[(*index)++]) << 8;
|
|
val |= ((int32_t)buf[(*index)++]);
|
|
return val;
|
|
}
|
|
|
|
/**
|
|
* @brief Read a 32 bit float from the buffer
|
|
*
|
|
* @param buf The buffer to read from
|
|
* @param index The index read/write pointer
|
|
* @return 32 bit float
|
|
*/
|
|
float buf_read_f32(const uint8_t *buf, size_t *index) {
|
|
uint32_t u = (uint32_t)buf_read_i32(buf, index);
|
|
float f;
|
|
memcpy(&f, &u, sizeof(float));
|
|
return f;
|
|
}
|