2 Star 1 Fork 0

YHG/bldc-tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utility.cpp 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
Benjamin Vedder 提交于 2015-06-24 02:29 . Some refactoring
/*
* buffer.c
*
* Created on: 13 maj 2013
* Author: benjamin
*/
#include "utility.h"
#include <cmath>
namespace {
inline double roundDouble(double x) {
return x < 0.0 ? ceil(x - 0.5) : floor(x + 0.5);
}
}
namespace utility {
void buffer_append_int32(uint8_t* buffer, int32_t number, int32_t *index) {
buffer[(*index)++] = number >> 24;
buffer[(*index)++] = number >> 16;
buffer[(*index)++] = number >> 8;
buffer[(*index)++] = number;
}
void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index) {
buffer[(*index)++] = number >> 24;
buffer[(*index)++] = number >> 16;
buffer[(*index)++] = number >> 8;
buffer[(*index)++] = number;
}
void buffer_append_int16(uint8_t* buffer, int16_t number, int32_t *index) {
buffer[(*index)++] = number >> 8;
buffer[(*index)++] = number;
}
void buffer_append_uint16(uint8_t* buffer, uint16_t number, int32_t *index) {
buffer[(*index)++] = number >> 8;
buffer[(*index)++] = number;
}
void buffer_append_double16(uint8_t* buffer, double number, double scale, int32_t *index) {
buffer_append_int16(buffer, (int16_t)(roundDouble(number * scale)), index);
}
void buffer_append_double32(uint8_t* buffer, double number, double scale, int32_t *index) {
buffer_append_int32(buffer, (int32_t)(roundDouble(number * scale)), index);
}
int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index) {
int16_t res = ((uint8_t) buffer[*index]) << 8 |
((uint8_t) buffer[*index + 1]);
*index += 2;
return res;
}
uint16_t buffer_get_uint16(const uint8_t *buffer, int32_t *index) {
uint16_t res = ((uint8_t) buffer[*index]) << 8 |
((uint8_t) buffer[*index + 1]);
*index += 2;
return res;
}
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index) {
int32_t res = ((uint8_t) buffer[*index]) << 24 |
((uint8_t) buffer[*index + 1]) << 16 |
((uint8_t) buffer[*index + 2]) << 8 |
((uint8_t) buffer[*index + 3]);
*index += 4;
return res;
}
uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index) {
uint32_t res = ((uint8_t) buffer[*index]) << 24 |
((uint8_t) buffer[*index + 1]) << 16 |
((uint8_t) buffer[*index + 2]) << 8 |
((uint8_t) buffer[*index + 3]);
*index += 4;
return res;
}
double buffer_get_double16(const uint8_t *buffer, double scale, int32_t *index) {
return (double)buffer_get_int16(buffer, index) / scale;
}
double buffer_get_double32(const uint8_t *buffer, double scale, int32_t *index) {
return (double)buffer_get_int32(buffer, index) / scale;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yhg-kkk/bldc-tool.git
git@gitee.com:yhg-kkk/bldc-tool.git
yhg-kkk
bldc-tool
bldc-tool
master

搜索帮助