Standardize on snake_case naming as this is a C library.
[apps/agl-service-can-low-level.git] / src / canutil / write.c
1 #include "write.h"
2 #include <bitfield/bitfield.h>
3
4 uint64_t bitfield_encode_float(float value, uint8_t bit_offset, uint8_t bit_size,
5         float factor, float offset) {
6     float raw = (value - offset) / factor;
7     if(raw > 0) {
8         // round up to avoid losing precision when we cast to an int
9         raw += 0.5;
10     }
11     uint64_t result = 0;
12     set_bit_field(&result, (uint64_t)raw, bit_offset, bit_size);
13     return result;
14 }
15
16 uint64_t bitfield_encode_bool(bool value, uint8_t bit_offset, uint8_t bit_size,
17         float factor, float offset) {
18     return bitfield_encode_float(value, offset, factor, bit_offset, bit_size);
19 }