1 #include <canutil/write.h>
2 #include <bitfield/bitfield.h>
3 #include <bitfield/8byte.h>
5 static uint64_t float_to_fixed_point(const float value, const float factor,
7 float raw = (value - offset) / factor;
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
17 uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_size,
18 float factor, float offset) {
20 if(!eightbyte_set_bitfield(float_to_fixed_point(value, factor, offset),
21 bit_offset, bit_size, &result)) {
22 // debug("%f will not fit in a %d bit field", value, bit_size);
27 uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset,
28 const uint8_t bit_size) {
29 return eightbyte_encode_float(value, bit_offset, bit_size, 1.0, 0);
32 bool bitfield_encode_float(const float value, const uint8_t bit_offset,
33 const uint8_t bit_size, const float factor, const float offset,
34 uint8_t destination[], const uint8_t destination_length) {
35 if(!set_bitfield(float_to_fixed_point(value, factor, offset), bit_offset,
36 bit_size, destination, destination_length)) {
37 // debug("%f will not fit in a %d bit field", value, bit_size);
43 bool bitfield_encode_bool(const bool value, const uint8_t bit_offset,
44 const uint8_t bit_size, uint8_t destination[],
45 const uint16_t destination_length) {
46 return bitfield_encode_float(value, bit_offset, bit_size, 1.0, 0,
47 destination, destination_length);