aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristopher Peplin <chris.peplin@rhubarbtech.com>2014-01-06 12:03:18 -0500
committerChristopher Peplin <chris.peplin@rhubarbtech.com>2014-01-06 12:03:18 -0500
commitcc2f44eeedac7d6e86005543c7e1596c3c78d551 (patch)
tree9f6e95739daaa8216abbfd105f6b079e9b5217d6 /src
parentf0d31caa3955365f6a393d788831f4b53fade000 (diff)
DRY up the float parser.
Diffstat (limited to 'src')
-rw-r--r--src/canutil/read.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/canutil/read.c b/src/canutil/read.c
index b662e9bd..d0cbb71a 100644
--- a/src/canutil/read.c
+++ b/src/canutil/read.c
@@ -2,10 +2,14 @@
#include <bitfield/bitfield.h>
#include <bitfield/8byte.h>
+static float decode_float(uint64_t raw, float factor, float offset) {
+ return raw * factor + offset;
+}
+
float eightbyte_parse_float(uint64_t data, uint8_t bit_offset, uint8_t bit_size,
float factor, float offset) {
- uint64_t raw = eightbyte_get_bitfield(data, bit_offset, bit_size, true);
- return raw * factor + offset;
+ return decode_float(eightbyte_get_bitfield(data, bit_offset, bit_size,
+ true), factor, offset);
}
bool eightbyte_parse_bool(uint64_t data, uint8_t bit_offset, uint8_t bit_size,
@@ -17,10 +21,8 @@ bool eightbyte_parse_bool(uint64_t data, uint8_t bit_offset, uint8_t bit_size,
float bitfield_parse_float(const uint8_t source[], const uint16_t source_length,
const uint8_t bit_offset, const uint8_t bit_size, const float factor,
const float offset) {
- uint64_t raw = get_bitfield(source, source_length, bit_offset, bit_size);
- // TODO seems dumb that this is repeated from eightbyte_parse_float - is it
- // really worth keeping around these two implementations?
- return raw * factor + offset;
+ return decode_float(get_bitfield(source, source_length, bit_offset, bit_size),
+ factor, offset);
}
bool bitfield_parse_bool(const uint8_t source[], const uint16_t source_length,