Checkpoint commit renaming some functions for clarity.
[apps/agl-service-can-low-level.git] / src / canutil / write.c
1 #include <canutil/write.h>
2 #include <bitfield/bitfield.h>
3 #include <bitfield/8byte.h>
4
5 uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_size,
6         float factor, float offset) {
7     float raw = (value - offset) / factor;
8     if(raw > 0) {
9         // round up to avoid losing precision when we cast to an int
10         // TODO do we need a way to encode an int back to a signal without any
11         // rounding?
12         raw += 0.5;
13     }
14     uint64_t result = 0;
15     if(!set_bit_field(&result, (uint64_t)raw, bit_offset, bit_size)) {
16         // debug("%f will not fit in a %d bit field", value, bit_size);
17     }
18     return result;
19 }
20
21 uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset,
22         const uint8_t bit_size) {
23     return eightbyte_encode_float(value, bit_offset, bit_size, 1.0, 0);
24 }
25
26 bool bitfield_encode_float(float value, uint8_t bit_offset,
27         uint8_t bit_size, float factor, float offset, uint8_t destination[]) {
28     // TODO
29     return 0;
30 }