aboutsummaryrefslogtreecommitdiffstats
path: root/README.mkd
blob: 76feddbe63689907d7d1a3a4fd9462ff6ec235de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
CAN Message Utilities for C
===========================

This is a C library with functions to help encode and decode Controller Area
Network (CAN) message payloads. Some of the bitfield functions may be useful for
other areas, too.

The header files contain complete function documentation, but to get you
started, here are examples using the API:

## Bitfield Manipulation

    uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
    uint8_t result = getByte(data, sizeof(data), 0);
    uint8_t result = getNibble(data, sizeof(data), 0);
    fail_unless(copyBitsRightAligned(data, 4, 4, 12, result, 4));

## 8 Byte Bitfield Decoding

    uint64_t data = 0x8000000000000000;
    uint64_t result = get_bit_field(data, 0, 1, false);
    // result == 0x1

    data = 0x0402574d555a0401;
    result = get_bit_field(data, 16, 32, false);
    // result = 0x574d555a;

    data = 0x00000000F34DFCFF;
    result = nth_byte(data, 0);
    //result = 0x0

    result = nth_byte(data, 4);
    //result = 0xF3

## 8 Byte Bitfield Encoding

    uint64_t data = 0;
    fail_unless(set_bit_field(&data, 1, 0, 1));
    uint64_t result = get_bit_field(data, 0, 1, false);
    ck_assert_int_eq(result, 0x1);

TODO setting bit fields is just copying

## CAN Signal Encoding

The library supports encoding floating point CAN signals as well as booleans
into a uint64_t payload.

    uint64_t payload = bitfield_encode_float(1, 1, 3, 1, 0)
    // payload == 0x1000000000000000

    payload = bitfield_encode_bool(true, 1, 3);
    // payload == 0x1000000000000000

## CAN Signal Decoding

The library supports parsing floating point CAN signals as well as booleans.

    uint64_t payload = 0xeb00000000000000;
    float 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
    // float_result == -19990.0

    bool 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
    // bool_result == true

## Testing

The library includes a test suite that uses the `check` C unit test library.

    $ make test

## Authors

Chris Peplin cpeplin@ford.com

## License

Copyright (c) 2013 Ford Motor Company

Licensed under the BSD license.