summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.mkd23
-rw-r--r--src/canutil/read.h8
-rw-r--r--tests/read_tests.c13
3 files changed, 43 insertions, 1 deletions
diff --git a/README.mkd b/README.mkd
index 0d8efb74..8f6ac5f1 100644
--- a/README.mkd
+++ b/README.mkd
@@ -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.
diff --git a/src/canutil/read.h b/src/canutil/read.h
index 3742d6d1..028b03a6 100644
--- a/src/canutil/read.h
+++ b/src/canutil/read.h
@@ -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);
diff --git a/tests/read_tests.c b/tests/read_tests.c
index 62ced1f9..71b0ab65 100644
--- a/tests/read_tests.c
+++ b/tests/read_tests.c
@@ -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;