diff options
Diffstat (limited to 'src/canutil')
-rw-r--r-- | src/canutil/write.c | 32 | ||||
-rw-r--r-- | src/canutil/write.h | 9 |
2 files changed, 32 insertions, 9 deletions
diff --git a/src/canutil/write.c b/src/canutil/write.c index 5b91aaf9..3b3ae25d 100644 --- a/src/canutil/write.c +++ b/src/canutil/write.c @@ -2,8 +2,8 @@ #include <bitfield/bitfield.h> #include <bitfield/8byte.h> -uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_size, - float factor, float offset) { +static uint64_t float_to_fixed_point(const float value, const float factor, + const float offset) { float raw = (value - offset) / factor; if(raw > 0) { // round up to avoid losing precision when we cast to an int @@ -11,8 +11,14 @@ uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_siz // rounding? raw += 0.5; } + return (uint64_t)raw; +} + +uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_size, + float factor, float offset) { uint64_t result = 0; - if(!eightbyte_set_bitfield(&result, (uint64_t)raw, bit_offset, bit_size)) { + if(!eightbyte_set_bitfield(float_to_fixed_point(value, factor, offset), + bit_offset, bit_size, &result)) { // debug("%f will not fit in a %d bit field", value, bit_size); } return result; @@ -23,8 +29,20 @@ uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset, return eightbyte_encode_float(value, bit_offset, bit_size, 1.0, 0); } -bool bitfield_encode_float(float value, uint8_t bit_offset, - uint8_t bit_size, float factor, float offset, uint8_t destination[]) { - // TODO - return 0; +bool bitfield_encode_float(const float value, const uint8_t bit_offset, + const uint8_t bit_size, const float factor, const float offset, + uint8_t destination[], const uint8_t destination_length) { + if(!set_bitfield(float_to_fixed_point(value, factor, offset), bit_offset, + bit_size, destination, destination_length)) { + // debug("%f will not fit in a %d bit field", value, bit_size); + return false; + } + return true; +} + +bool bitfield_encode_bool(const bool value, const uint8_t bit_offset, + const uint8_t bit_size, uint8_t destination[], + const uint16_t destination_length) { + return bitfield_encode_float(value, bit_offset, bit_size, 1.0, 0, + destination, destination_length); } diff --git a/src/canutil/write.h b/src/canutil/write.h index 8fd18cdb..28b7e052 100644 --- a/src/canutil/write.h +++ b/src/canutil/write.h @@ -26,8 +26,9 @@ extern "C" { uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_size, float factor, float offset); -bool bitfield_encode_float(float value, uint8_t bit_offset, - uint8_t bit_size, float factor, float offset, uint8_t destination[]); +bool bitfield_encode_float(const float value, const uint8_t bit_offset, + const uint8_t bit_size, const float factor, const float offset, + uint8_t destination[], const uint8_t destination_length); /* Public: Encode a boolean into fixed bit width field in a bit array. * @@ -42,6 +43,10 @@ bool bitfield_encode_float(float value, uint8_t bit_offset, uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset, const uint8_t bit_size); +bool bitfield_encode_bool(const bool value, const uint8_t bit_offset, const + uint8_t bit_size, uint8_t destination[], + const uint16_t destination_length); + #ifdef __cplusplus } #endif |