diff options
author | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-24 10:56:35 -0500 |
---|---|---|
committer | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-24 10:56:35 -0500 |
commit | bc1baf25a0844861713829c0e9e69e4a2d447cc6 (patch) | |
tree | 37ad821656ffa979248f6154573f5cd9f30932a3 /src/canutil/write.c |
Initial commit, pulled from openxc/vi-firmware.
Diffstat (limited to 'src/canutil/write.c')
-rw-r--r-- | src/canutil/write.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/canutil/write.c b/src/canutil/write.c new file mode 100644 index 00000000..fdcba1f5 --- /dev/null +++ b/src/canutil/write.c @@ -0,0 +1,18 @@ +#include "write.h" + +uint64_t encodeFloat(float value, float offset, float factor, uint8_t bitPosition, + uint8_t bitSize) { + float rawValue = (value - offset) / factor; + if(rawValue > 0) { + // round up to avoid losing precision when we cast to an int + rawValue += 0.5; + } + uint64_t result = 0; + setBitField(&result, rawValue, bitPosition, bitSize); + return result; +} + +uint64_t encodeBoolean(bool value, float offset, float factor, + uint8_t bitPosition, uint8_t bitSize) { + return encodeFloat(value, offset, factor, bitPosition, bitSize); +} |