Test parsing boolean.
authorChristopher Peplin <chris.peplin@rhubarbtech.com>
Sun, 29 Dec 2013 17:14:29 +0000 (12:14 -0500)
committerChristopher Peplin <chris.peplin@rhubarbtech.com>
Sun, 29 Dec 2013 17:14:29 +0000 (12:14 -0500)
README.mkd
src/canutil/read.h
tests/read_tests.c

index 0d8efb7..8f6ac5f 100644 (file)
@@ -1,6 +1,29 @@
 CAN Message Utilities for C
 ============
 
+## Bitfield Manipulation
+
+## CAN Signal Encoding
+
+## CAN Signal Decoding
+
+The library supports parsing floating point CAN signals as well as booleans.
+
+    uint64_t payload = 0xeb00000000000000;
+    float result = bitfield_parse_float(payload,
+            2, // starting bit
+            4, // width of the signal's field
+            1001.0, // transformation factor for the signal value
+            -30000.0); // transformation offset for the signal value
+    // result == -19990.0
+
+    bool result = bitfield_parse_bool(payload,
+            0, // starting bit
+            1, // width of the signal's field
+            1.0, // transformation factor for the signal value
+            0); // transformation offset for the signal value
+    // result == true
+
 ## Testing
 
 The library includes a test suite that uses the `check` C unit test library.
index 3742d6d..028b03a 100644 (file)
@@ -19,6 +19,14 @@ extern "C" {
 float bitfield_parse_float(uint64_t data, uint8_t bit_offset, uint8_t bit_size,
         float factor, float offset);
 
+/* Public: Parse a CAN signal from a message and interpret it as a boolean.
+ *
+ * signal - The details of the signal to decode and forward.
+ * data   - The raw bytes of the CAN message that contains the signal, assumed
+ *      to be in big-endian byte order from CAN.
+ *
+ * Returns false if the value was 0, otherwise true.
+ */
 bool bitfield_parse_bool(uint64_t data, uint8_t bit_offset, uint8_t bit_size,
         float factor, float offset);
 
index 62ced1f..71b0ab6 100644 (file)
@@ -6,18 +6,29 @@ const uint64_t BIG_ENDIAN_TEST_DATA = __builtin_bswap64(0xEB00000000000000);
 
 START_TEST (test_parse_float)
 {
-    float result = bitfield_parse_float(BIG_ENDIAN_TEST_DATA, 2, 4, 1001.0, -30000.0);
+    float result = bitfield_parse_float(BIG_ENDIAN_TEST_DATA, 2, 4, 1001.0,
+            -30000.0);
     float correctResult = 0xA * 1001.0 - 30000.0;
     fail_unless(result == correctResult,
             "parse is incorrect: %f but should be %f", result, correctResult);
 }
 END_TEST
 
+START_TEST (test_parse_bool)
+{
+    float result = bitfield_parse_bool(BIG_ENDIAN_TEST_DATA, 0, 1, 1.0, 0);
+    float correctResult = true;
+    fail_unless(result == correctResult,
+            "parse is incorrect: %d but should be %d", result, correctResult);
+}
+END_TEST
+
 Suite* canreadSuite(void) {
     Suite* s = suite_create("read");
     TCase *tc_core = tcase_create("core");
     tcase_add_checked_fixture(tc_core, NULL, NULL);
     tcase_add_test(tc_core, test_parse_float);
+    tcase_add_test(tc_core, test_parse_bool);
     suite_add_tcase(s, tc_core);
 
     return s;