This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduinocomm.cpp
235 lines (216 loc) · 6.29 KB
/
arduinocomm.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "arduinocomm.h"
using namespace std;
//int s_debug = 1;
Arduinocomm::Arduinocomm()
{
}
void Arduinocomm::update()
{
/*if(input_position >= this->input_size){
input_position = 0;
this->input_size = 0;*/
//Serial.println("Available?");
while(Serial.available() > 0)
{
//Serial.println("Available");
//sendcustombyte((uint8_t)Serial.available());
//byte ret = Serial.readBytes(input_buffer,min(Serial.available(),MAX_BUFFER));
//sendcustombyte(ret);
//serial_count++;
//if(ret>0){this->input_size = ret; process();}
int val = Serial.read();
//Serial.println(val);
if(val>=0)
{ input_buffer[0] = val;}
this->input_size = 1;
process();
}
/*}
else if(this->input_size > 0){
process();
}*/
}
void Arduinocomm::process()
{
uint8_t val = 0;
uint8_t crc = 0;
uint8_t pb_pos = 0;
//sendcustombyte(this->input_size);
//while(input_position < this->input_size)
{
val = input_buffer[0];
//sendcustombyte(val);
if(val > 127)//Command
{
switch(val)
{
case START_DATA:
packet_ready = false;
packet_position = 0;
reading_packet = true;
packet_size = 0;
//input_position++;
//sendcustombyte(input_position);
break;
case END_DATA:
pb_pos = 0;
for(int i=0;i<packet_position;i+=2)
{
packet_buffer[pb_pos++] = readbyte(i);
/*Serial.println("Packet buffer");
Serial.print((int)packet_buffer[pb_pos-1]);
Serial.println();*/
//sendcustombyte(packet_buffer[i/2]);
}
//Serial.println();
crc = CRC8((const uint8_t*)&temp_buffer,packet_position-2,0);
if(crc == packet_buffer[pb_pos-1] && reading_packet)
{
packet_ready = true;
packet_size = packet_position;
}
else
{
//Serial.println("Fail");
}
reading_packet = false;
//input_position++;
return;
default:
//sendcustombyte(22);
packet_position = 0;
packet_ready = false;
reading_packet = false;
//input_position++;
break;
}
}
else
{
if(reading_packet)
{
//if(val == 2){sendcustombyte(val);}
temp_buffer[packet_position] = val;
//if(val == 2){temp_buffer[packet_position];}
packet_position++;
//input_position++;
}
/*else
{
input_position++;
}*/
}
}
}
//Reads 2 bytes into 1 byte, converting from 7-bit packing to 8-bits.
uint8_t Arduinocomm::readbyte(unsigned int position)
{
return temp_buffer[position] + (temp_buffer[position+1] << 7);
}
/*uint16_t Arduinocomm::read_uint16(uint8_t (&packet)[MAX_BUFFER], unsigned int position)
{
return packet[position] + (packet[position+1] << 8);
}
uint32_t Arduinocomm::read_uint32(uint8_t (&packet)[MAX_BUFFER], unsigned int position)
{
return packet[position] + (packet[position+1] << 8) + (packet[position+2] << 16) + (packet[position+3] << 24);
}*/
void Arduinocomm::writecommand(uint8_t byte)
{
//Serial.write(byte);
bytes[byte_position++] = byte;
}
//Writes 1 byte as 2 bytes, converting from 8-bit to 7-bit packing.
void Arduinocomm::writebyte(uint8_t byte)
{
//Serial.write(byte & 0x7F);
bytes[byte_position++] = byte & 0x7F;
//Serial.write((byte & 0x80) >> 7);
bytes[byte_position++] =(byte & 0x80) >> 7;
}
void Arduinocomm::writeuint32(uint32_t value)
{
writebyte(value & 0x000000FF);
writebyte((value >> 8) & 0x000000FF);
writebyte((value >> 16) & 0x000000FF);
writebyte((value >> 24) & 0x000000FF);
}
void Arduinocomm::writeuint16(uint16_t value)
{
writebyte(value & 0x00FF);
writebyte((value >> 8) & 0x00FF);
}
void Arduinocomm::sendcustombyte(uint8_t byte)
{
int len = 8;
bytes = (uint8_t *) malloc(len);
writecommand(START_DATA);
writebyte(DEBUG);
writebyte(byte);
writebyte(CRC8((const uint8_t*)bytes,len-4,1));
writecommand(END_DATA);
Serial.write(bytes,len);
Serial.flush();
free(bytes);
byte_position = 0;
}
void Arduinocomm::write_ok()
{
int len = 6;
bytes = (uint8_t *) malloc(len);
writecommand(START_DATA);
writebyte(OK);
writebyte(CRC8((const uint8_t*)bytes,len-4,1));
writecommand(END_DATA);
Serial.write(bytes,len);
Serial.flush();
free(bytes);
byte_position = 0;
}
void Arduinocomm::write_line_position(uint16_t position)
{
int len = 10;
bytes = (uint8_t *) malloc(len);
writecommand(START_DATA);
writebyte(LINE_POSITION);
writeuint16(position);
writebyte(CRC8((const uint8_t*)bytes,len-4,1));
writecommand(END_DATA);
if(debug)
{
for(int i =0;i<len;i++)
{
Serial.print(bytes[i]);
Serial.print(" ");
}
Serial.println();
}
else
{
Serial.write(bytes,len);
Serial.flush();
}
free(bytes);
byte_position = 0;
}
///CRC-8 - based on the CRC8 formulas by Dallas/Maxim
//code released under the therms of the GNU GPL 3.0 license
uint8_t Arduinocomm::CRC8(const uint8_t *data, uint8_t len, uint8_t start) {
//Serial.println("crc");
//Serial.println(len);
data+=start;
uint8_t crc = 0x00;
while (len--) {
uint8_t extract = *data++;
//Serial.println(extract);
for (byte tempI = 8; tempI; tempI--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}