-
-
Notifications
You must be signed in to change notification settings - Fork 556
/
motohawk_no_floating_point_numbers.c
154 lines (127 loc) · 3.92 KB
/
motohawk_no_floating_point_numbers.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* The MIT License (MIT)
*
* Copyright (c) 2018-2019 Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
*/
#include <string.h>
#include "motohawk.h"
static inline uint8_t pack_left_shift_u8(
uint8_t value,
uint8_t shift,
uint8_t mask)
{
return (uint8_t)((uint8_t)(value << shift) & mask);
}
static inline uint8_t pack_left_shift_u16(
uint16_t value,
uint8_t shift,
uint8_t mask)
{
return (uint8_t)((uint8_t)(value << shift) & mask);
}
static inline uint8_t pack_right_shift_u16(
uint16_t value,
uint8_t shift,
uint8_t mask)
{
return (uint8_t)((uint8_t)(value >> shift) & mask);
}
static inline uint16_t unpack_left_shift_u16(
uint8_t value,
uint8_t shift,
uint8_t mask)
{
return (uint16_t)((uint16_t)(value & mask) << shift);
}
static inline uint8_t unpack_right_shift_u8(
uint8_t value,
uint8_t shift,
uint8_t mask)
{
return (uint8_t)((uint8_t)(value & mask) >> shift);
}
static inline uint16_t unpack_right_shift_u16(
uint8_t value,
uint8_t shift,
uint8_t mask)
{
return (uint16_t)((uint16_t)(value & mask) >> shift);
}
int motohawk_example_message_pack(
uint8_t *dst_p,
const struct motohawk_example_message_t *src_p,
size_t size)
{
uint16_t temperature;
if (size < 8u) {
return (-EINVAL);
}
memset(&dst_p[0], 0, 8);
dst_p[0] |= pack_left_shift_u8(src_p->enable, 7u, 0x80u);
dst_p[0] |= pack_left_shift_u8(src_p->average_radius, 1u, 0x7eu);
temperature = (uint16_t)src_p->temperature;
dst_p[0] |= pack_right_shift_u16(temperature, 11u, 0x01u);
dst_p[1] |= pack_right_shift_u16(temperature, 3u, 0xffu);
dst_p[2] |= pack_left_shift_u16(temperature, 5u, 0xe0u);
return (8);
}
int motohawk_example_message_unpack(
struct motohawk_example_message_t *dst_p,
const uint8_t *src_p,
size_t size)
{
uint16_t temperature;
if (size < 8u) {
return (-EINVAL);
}
dst_p->enable = unpack_right_shift_u8(src_p[0], 7u, 0x80u);
dst_p->average_radius = unpack_right_shift_u8(src_p[0], 1u, 0x7eu);
temperature = unpack_left_shift_u16(src_p[0], 11u, 0x01u);
temperature |= unpack_left_shift_u16(src_p[1], 3u, 0xffu);
temperature |= unpack_right_shift_u16(src_p[2], 5u, 0xe0u);
if ((temperature & (1u << 11)) != 0u) {
temperature |= 0xf000u;
}
dst_p->temperature = (int16_t)temperature;
return (0);
}
int motohawk_example_message_init(struct motohawk_example_message_t *msg_p)
{
if (msg_p == NULL) return -1;
memset(msg_p, 0, sizeof(struct motohawk_example_message_t));
return 0;
}
bool motohawk_example_message_enable_is_in_range(uint8_t value)
{
return (value <= 1u);
}
bool motohawk_example_message_average_radius_is_in_range(uint8_t value)
{
return (value <= 50u);
}
bool motohawk_example_message_temperature_is_in_range(int16_t value)
{
return ((value >= -2048) && (value <= 2047));
}