summaryrefslogtreecommitdiffstats
path: root/libs/bitfield-c/src/canutil/read.c
blob: 5790a7a1135fdfa94c0cb997ff2738f41b804a05 (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
#include <canutil/read.h>
#include <bitfield/bitfield.h>
#include <bitfield/8byte.h>

static float decode_float(uint64_t raw, float factor, float offset) {
    return raw * factor + offset;
}

float eightbyte_parse_float(uint64_t data, unsigned int bit_offset, unsigned int bit_size,
        float factor, float offset) {
    return decode_float(eightbyte_get_bitfield(data, bit_offset, bit_size,
                true), factor, offset);
}

bool eightbyte_parse_bool(uint64_t data, unsigned int bit_offset, unsigned int bit_size,
        float factor, float offset) {
    float value = eightbyte_parse_float(data, bit_offset, bit_size, factor, offset);
    return value == 0.0 ? false : true;
}

float bitfield_parse_float(const uint8_t source[], const unsigned int source_length,
        const unsigned int bit_offset, const unsigned int bit_size, const float factor,
        const float offset) {
    return decode_float(get_bitfield(source, source_length, bit_offset, bit_size),
            factor, offset);
}

bool bitfield_parse_bool(const uint8_t source[], const unsigned int source_length,
        const unsigned int bit_offset, const unsigned int bit_size, const float factor,
        const float offset) {
    float value = bitfield_parse_float(source, source_length, bit_offset,
            bit_size, factor, offset);
    return value == 0.0 ? false : true;
}