forked from tangjie133/pxt-huskylens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuskyLensProtocolCore.c
177 lines (155 loc) · 5.36 KB
/
HuskyLensProtocolCore.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "HuskyLensProtocolCore.h"
#include "stdio.h"
#include "stdbool.h"
#include "string.h"
#include <stdlib.h>
#define FRAME_BUFFER_SIZE 128
#define HEADER_0_INDEX 0
#define HEADER_1_INDEX 1
#define ADDRESS_INDEX 2
#define CONTENT_SIZE_INDEX 3
#define COMMAND_INDEX 4
#define CONTENT_INDEX 5
#define PROTOCOL_SIZE 6
static uint8_t send_buffer[FRAME_BUFFER_SIZE];
static uint8_t receive_buffer[FRAME_BUFFER_SIZE];
static short send_index = 0;
static short receive_index = 0;
static bool send_fail = false;
static bool receive_fail = false;
static short content_current = 0;
static short content_end = 0;
static bool content_read_end = false;
bool validateCheckSum(){
uint8_t stackSumIndex=receive_buffer[CONTENT_SIZE_INDEX] + CONTENT_INDEX;
uint8_t sum = 0;
//calculate the checksum
for (uint8_t i=0; i< stackSumIndex; i++) {
sum+=receive_buffer[i];
}
return (sum == receive_buffer[stackSumIndex]);
}
bool husky_lens_protocol_receive(uint8_t data){
switch (receive_index)
{
case HEADER_0_INDEX:
if (data!=0x55) {receive_index = 0; return false;}
receive_buffer[HEADER_0_INDEX] = 0x55;
break;
case HEADER_1_INDEX:
if (data!=0xaa) {receive_index = 0; return false;}
receive_buffer[HEADER_1_INDEX] = 0xaa;
break;
case ADDRESS_INDEX:
receive_buffer[ADDRESS_INDEX] = data;
break;
case CONTENT_SIZE_INDEX:
if (data >= FRAME_BUFFER_SIZE-PROTOCOL_SIZE) {receive_index = 0; return false;}
receive_buffer[CONTENT_SIZE_INDEX] = data;
break;
default:
receive_buffer[receive_index]=data;
if (receive_index==receive_buffer[CONTENT_SIZE_INDEX]+CONTENT_INDEX) {
content_end = receive_index;
receive_index=0;
return validateCheckSum();
}
break;
}
receive_index++;
return false;
}
bool husky_lens_protocol_read_begin(uint8_t command){
if (command == receive_buffer[COMMAND_INDEX])
{
content_current = CONTENT_INDEX;
content_read_end = false;
receive_fail = false;
return true;
}
return false;
}
uint8_t husky_lens_protocol_read_uint8(){
if (content_current >= content_end || content_read_end){receive_fail = true; return 0;}
uint8_t result;
memcpy(&result, receive_buffer + content_current, sizeof(result));
content_current += sizeof(result);
return result;
}
int16_t husky_lens_protocol_read_int16(){
if (content_current >= content_end || content_read_end){receive_fail = true; return 0;}
int16_t result;
memcpy(&result, receive_buffer + content_current, sizeof(result));
if (IS_BIG_ENDIAN()){__builtin_bswap16(result);}
content_current += sizeof(result);
return result;
}
int32_t husky_lens_protocol_read_int32(){
if (content_current >= content_end || content_read_end){receive_fail = true; return 0;}
int32_t result;
memcpy(&result, receive_buffer + content_current, sizeof(result));
if (IS_BIG_ENDIAN()){__builtin_bswap32(result);}
content_current += sizeof(result);
return result;
}
float husky_lens_protocol_read_float(){
if (content_current >= content_end || content_read_end){receive_fail = true; return 0;}
float result;
memcpy(&result, receive_buffer + content_current, sizeof(result));
if (IS_BIG_ENDIAN()){__builtin_bswap32(result);}
content_current += sizeof(result);
return result;
}
bool husky_lens_protocol_read_end(){
if (receive_fail)
{
receive_fail = false;
return false;
}
return content_current == content_end;
}
uint8_t* husky_lens_protocol_write_begin(uint8_t command){
send_fail = false;
send_buffer[HEADER_0_INDEX] = 0x55;
send_buffer[HEADER_1_INDEX] = 0xAA;
send_buffer[ADDRESS_INDEX] = 0x11;
send_buffer[COMMAND_INDEX] = command;
send_index = CONTENT_INDEX;
return &send_buffer;
}
void husky_lens_protocol_write_uint8(uint8_t content){
if(send_index + sizeof(content) >= FRAME_BUFFER_SIZE) {send_fail = true; return;}
memcpy(send_buffer + send_index, &content, sizeof(content));
send_index += sizeof(content);
}
void husky_lens_protocol_write_int16(int16_t content){
if(send_index + sizeof(content) >= FRAME_BUFFER_SIZE) {send_fail = true; return;}
if (IS_BIG_ENDIAN()){__builtin_bswap16(content);}
memcpy(send_buffer + send_index, &content, sizeof(content));
send_index += sizeof(content);
}
void husky_lens_protocol_write_int32(int32_t content){
if(send_index + sizeof(content) >= FRAME_BUFFER_SIZE) {send_fail = true; return;}
if (IS_BIG_ENDIAN()){__builtin_bswap32(content);}
memcpy(send_buffer + send_index, &content, sizeof(content));
send_index += sizeof(content);
}
void husky_lens_protocol_write_float(float content){
if(send_index + sizeof(content) >= FRAME_BUFFER_SIZE) {send_fail = true; return;}
if (IS_BIG_ENDIAN()){__builtin_bswap32(content);}
memcpy(send_buffer + send_index, &content, sizeof(content));
send_index += sizeof(content);
}
int husky_lens_protocol_write_end(){
if(send_fail) {return 0;}
if(send_index + 1 >= FRAME_BUFFER_SIZE) {return 0;}
send_buffer[CONTENT_SIZE_INDEX] = send_index - CONTENT_INDEX;
uint8_t sum = 0;
for (int i = 0; i < send_index; i++)
{
sum += send_buffer[i];
}
send_buffer[send_index] = sum;
send_index ++;
return send_index;
}