Test all canutil functions and document in README.
[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     if(!set_bit_field(&result, (uint64_t)raw, bit_offset, bit_size)) {
13         // debug("%f will not fit in a %d bit field", value, bit_size);
14     }
15     return result;
16 }
17
18 uint64_t bitfield_encode_bool(const bool value, const uint8_t bit_offset,
19         const uint8_t bit_size) {
20     return bitfield_encode_float(value, bit_offset, bit_size, 1.0, 0);
21 }