-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCP.cpp
197 lines (178 loc) · 4.45 KB
/
CCP.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "Arduino.h"
#include "CCP.h"
/*
if (id < 0x40)
{
// string
}
else if (id < 0x80)
{
// uint32
}
else if (id < 0xC0)
{
// float
}
else
{
// fp16
}
*/
/*--CANtransfer--*/
byte CCP::byte_to_device(long unsigned int _id, byte _data_byte[8])
{
id = _id;
memcpy(msg.msg_byte, _data_byte, 8);
return write_device();
}
byte CCP::string_to_device(long unsigned int _id, char *_string)
{
id = _id;
msg.string_msg.time16 = millis() / 1000;
for (int i = 0; i < 6; i++)
{
msg.string_msg.string[i] = ' ';
}
for (int i = 0; i < 6; i++)
{
if (_string[i] <= 0x1f)
{
break;
}
msg.string_msg.string[i] = _string[i];
}
return write_device();
}
byte CCP::uint32_to_device(long unsigned int _id, uint32_t _data_uint32)
{
id = _id;
msg.uint32_msg.time32 = millis();
msg.uint32_msg.data_uint32 = _data_uint32;
return write_device();
}
byte CCP::uint16_to_device(long unsigned int _id, uint16_t _data_uint16_0, uint16_t _data_uint16_1, uint16_t _data_uint16_2)
{
id = _id;
msg.uint16_msg.time16 = millis() / 1000;
msg.uint16_msg.data_uint16_0 = _data_uint16_0;
msg.uint16_msg.data_uint16_1 = _data_uint16_1;
msg.uint16_msg.data_uint16_2 = _data_uint16_2;
return write_device();
}
byte CCP::float_to_device(long unsigned int _id, float _data_float)
{
id = _id;
msg.float_msg.time32 = millis();
msg.float_msg.data_float = _data_float;
return write_device();
}
byte CCP::fp16_to_device(long unsigned int _id, float _data_fp16_0, float _data_fp16_1, float _data_fp16_2)
{
id = _id;
msg.fp16_msg.time16 = millis() / 1000;
float_to_fp16(msg.fp16_msg.data_fp16_0, _data_fp16_0);
float_to_fp16(msg.fp16_msg.data_fp16_1, _data_fp16_1);
float_to_fp16(msg.fp16_msg.data_fp16_2, _data_fp16_2);
return write_device();
}
/*--receive,decode-*/
void CCP::string(char *str_buf, int str_len)
{
int i = 0;
for (; i < 6 && i < str_len - 1; i++)
{
str_buf[i] = msg.string_msg.string[i];
}
for (; i < str_len - 1; i++)
{
str_buf[i] = ' ';
}
str_buf[i + 1] = '\0';
}
bool CCP::str_match(char *str_to_cmp, int str_len)
{
bool isMatch = true;
for (int i = 0; i < str_len && i < 6; i++)
{
if (str_to_cmp[i] != msg.string_msg.string[i])
{
isMatch = false;
}
}
return isMatch;
}
uint16_t CCP::time16()
{
// time16 = time32 / 1000;
return msg.string_msg.time16;
}
uint32_t CCP::time32()
{
// time32 = time16 * 1000;
return msg.uint32_msg.time32;
}
uint32_t CCP::data_uint32()
{
return msg.uint32_msg.data_uint32;
}
uint16_t CCP::data_uint16_0()
{
return msg.uint16_msg.data_uint16_0;
}
uint16_t CCP::data_uint16_1()
{
return msg.uint16_msg.data_uint16_1;
}
uint16_t CCP::data_uint16_2()
{
return msg.uint16_msg.data_uint16_2;
}
float CCP::data_float()
{
return msg.float_msg.data_float;
}
float CCP::data_fp16_0()
{
return fp16_to_float(msg.fp16_msg.data_fp16_0);
}
float CCP::data_fp16_1()
{
return fp16_to_float(msg.fp16_msg.data_fp16_1);
}
float CCP::data_fp16_2()
{
return fp16_to_float(msg.fp16_msg.data_fp16_2);
}
/*--float-fp16--*/
typedef union
{
float fp32;
byte fp32_byte[4];
} float_byte_union;
void CCP::float_to_fp16(byte fp16[2], float data_fp16)
{
float_byte_union float_byte;
float_byte.fp32 = data_fp16;
// float_byte→fp16
fp16[1] = (float_byte.fp32_byte[3] & 0b11000000) + ((float_byte.fp32_byte[3] & 0b00000111) << 3) + ((float_byte.fp32_byte[2] & 0b11100000) >> 5);
fp16[0] = ((float_byte.fp32_byte[2] & 0b00011111) << 3) + ((float_byte.fp32_byte[1] & 0b11100000) >> 5);
}
float CCP::fp16_to_float(byte fp16[2])
{
float_byte_union float_byte;
float_byte.fp32_byte[3] = (fp16[1] & 0b11000000) + ((fp16[1] & 0b00111100) >> 3);
float_byte.fp32_byte[2] = ((fp16[1] & 0b00000100) << 5) + ((fp16[1] & 0b00000011) << 5) + ((fp16[0] & 0b11111000) >> 3);
float_byte.fp32_byte[1] = ((fp16[0] & 0b00000111) << 5);
float_byte.fp32_byte[0] = 0;
if (!(fp16[1] & 0b01000000))
{
float_byte.fp32_byte[3] += 0b00111000;
}
// mantissa0→exp0
if (((fp16[1] | 0b11111100) == 0b11111100) && (fp16[0] == 0))
{
float_byte.fp32_byte[3] = (float_byte.fp32_byte[1] & 0b10000000);
float_byte.fp32_byte[2] = (float_byte.fp32_byte[2] & 0b01111111);
}
return float_byte.fp32;
}