fdcba1f54824d23dd6f0f453701db09e230b0da8
[apps/agl-service-can-low-level.git] / src / canutil / write.c
1 #include "write.h"
2
3 uint64_t encodeFloat(float value, float offset, float factor, uint8_t bitPosition,
4         uint8_t bitSize) {
5     float rawValue = (value - offset) / factor;
6     if(rawValue > 0) {
7         // round up to avoid losing precision when we cast to an int
8         rawValue += 0.5;
9     }
10     uint64_t result = 0;
11     setBitField(&result, rawValue, bitPosition, bitSize);
12     return result;
13 }
14
15 uint64_t encodeBoolean(bool value, float offset, float factor,
16                 uint8_t bitPosition, uint8_t bitSize) {
17         return encodeFloat(value, offset, factor, bitPosition, bitSize);
18 }