Make float_to_fixed_point a public function.
[apps/low-level-can-service.git] / src / canutil / write.h
1 #ifndef __WRITE_H__
2 #define __WRITE_H__
3
4 #include <stdint.h>
5 #include <stdbool.h>
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 /* Public: Encode a floating point number into a fixed point, fixed bit width
12  *      field in a bit array.
13  *
14  * value - the floating point value to encode.
15  * bit_offset - the starting point for the encoded bits in the returned value.
16  * bit_size - The max width of the field in the resulting bit array. If bit_size
17  *      isn't big enough to store the fixed point version of the value, the
18  *      bitfeld will *not* be set. TODO some error reporting would be nice.
19  * factor - a factor used to transform from floating to fixed point before
20  *      encoding. Use 1.0 for no factor.
21  * offset - an offset used to transform from floating to fixed point before
22  *      encoding. Use 0 for no offset.
23  *
24  * Returns a big-endian uint64_t with the value encoded as a bitfield.
25  */
26 uint64_t eightbyte_encode_float(float value, uint8_t bit_offset,
27         uint8_t bit_size, float factor, float offset);
28
29 uint64_t float_to_fixed_point(const float value, const float factor,
30         const float offset);
31
32 bool bitfield_encode_float(const float value, const uint8_t bit_offset,
33         const uint8_t bit_size, const float factor, const float offset,
34         uint8_t destination[], const uint8_t destination_length);
35
36 /* Public: Encode a boolean into fixed bit width field in a bit array.
37  *
38  * value - the boolean value to encode - true will be 1, false will be 0.
39  * bit_offset - the starting point for the encoded bits in the returned value.
40  * bit_size - The max width of the field in the resulting bit array. If bit_size
41  *      isn't big enough to store the fixed point version of the value, the
42  *      bitfeld will *not* be set. TODO some error reporting would be nice.
43  *
44  * Returns a big-endian uint64_t with the value encoded as a bitfield.
45  */
46 uint64_t eightbyte_encode_bool(const bool value, const uint8_t bit_offset,
47         const uint8_t bit_size);
48
49 bool bitfield_encode_bool(const bool value, const uint8_t bit_offset, const
50         uint8_t bit_size, uint8_t destination[],
51         const uint16_t destination_length);
52
53 #ifdef __cplusplus
54 }
55 #endif
56
57 #endif // __WRITE_H__