Checkpoint commit renaming some functions for clarity.
[apps/agl-service-can-low-level.git] / src / canutil / write.c
index fdcba1f..09e6caa 100644 (file)
@@ -1,18 +1,30 @@
-#include "write.h"
+#include <canutil/write.h>
+#include <bitfield/bitfield.h>
+#include <bitfield/8byte.h>
 
-uint64_t encodeFloat(float value, float offset, float factor, uint8_t bitPosition,
-        uint8_t bitSize) {
-    float rawValue = (value - offset) / factor;
-    if(rawValue > 0) {
+uint64_t eightbyte_encode_float(float value, uint8_t bit_offset, uint8_t bit_size,
+        float factor, float offset) {
+    float raw = (value - offset) / factor;
+    if(raw > 0) {
         // round up to avoid losing precision when we cast to an int
-        rawValue += 0.5;
+        // TODO do we need a way to encode an int back to a signal without any
+        // rounding?
+        raw += 0.5;
     }
     uint64_t result = 0;
-    setBitField(&result, rawValue, bitPosition, bitSize);
+    if(!set_bit_field(&result, (uint64_t)raw, bit_offset, bit_size)) {
+        // debug("%f will not fit in a %d bit field", value, bit_size);
+    }
     return result;
 }
 
-uint64_t encodeBoolean(bool value, float offset, float factor,
-                uint8_t bitPosition, uint8_t bitSize) {
-        return encodeFloat(value, offset, factor, bitPosition, bitSize);
+uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset,
+        const uint8_t bit_size) {
+    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;
 }