Skip to content

Commit 1347f23

Browse files
authored
Create packetParser.cpp
1 parent d1741d6 commit 1347f23

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include <string.h>
2+
#include <Arduino.h>
3+
#include <SPI.h>
4+
#include <SoftwareSerial.h>
5+
6+
#include "Adafruit_BLE.h"
7+
#include "Adafruit_BluefruitLE_SPI.h"
8+
#include "Adafruit_BluefruitLE_UART.h"
9+
10+
11+
#define PACKET_ACC_LEN (15)
12+
#define PACKET_GYRO_LEN (15)
13+
#define PACKET_MAG_LEN (15)
14+
#define PACKET_QUAT_LEN (19)
15+
#define PACKET_BUTTON_LEN (5)
16+
#define PACKET_COLOR_LEN (6)
17+
#define PACKET_LOCATION_LEN (15)
18+
19+
// READ_BUFSIZE Size of the read buffer for incoming packets
20+
#define READ_BUFSIZE (20)
21+
22+
23+
/* Buffer to hold incoming characters */
24+
uint8_t packetbuffer[READ_BUFSIZE+1];
25+
26+
/**************************************************************************/
27+
/*!
28+
@brief Casts the four bytes at the specified address to a float
29+
*/
30+
/**************************************************************************/
31+
float parsefloat(uint8_t *buffer)
32+
{
33+
float f = ((float *)buffer)[0];
34+
return f;
35+
}
36+
37+
/**************************************************************************/
38+
/*!
39+
@brief Prints a hexadecimal value in plain characters
40+
@param data Pointer to the byte data
41+
@param numBytes Data length in bytes
42+
*/
43+
/**************************************************************************/
44+
void printHex(const uint8_t * data, const uint32_t numBytes)
45+
{
46+
uint32_t szPos;
47+
for (szPos=0; szPos < numBytes; szPos++)
48+
{
49+
Serial.print(F("0x"));
50+
// Append leading 0 for small values
51+
if (data[szPos] <= 0xF)
52+
{
53+
Serial.print(F("0"));
54+
Serial.print(data[szPos] & 0xf, HEX);
55+
}
56+
else
57+
{
58+
Serial.print(data[szPos] & 0xff, HEX);
59+
}
60+
// Add a trailing space if appropriate
61+
if ((numBytes > 1) && (szPos != numBytes - 1))
62+
{
63+
Serial.print(F(" "));
64+
}
65+
}
66+
Serial.println();
67+
}
68+
69+
/**************************************************************************/
70+
/*!
71+
@brief Waits for incoming data and parses it
72+
*/
73+
/**************************************************************************/
74+
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout)
75+
{
76+
uint16_t origtimeout = timeout, replyidx = 0;
77+
78+
memset(packetbuffer, 0, READ_BUFSIZE);
79+
80+
while (timeout--) {
81+
if (replyidx >= 20) break;
82+
if ((packetbuffer[1] == 'A') && (replyidx == PACKET_ACC_LEN))
83+
break;
84+
if ((packetbuffer[1] == 'G') && (replyidx == PACKET_GYRO_LEN))
85+
break;
86+
if ((packetbuffer[1] == 'M') && (replyidx == PACKET_MAG_LEN))
87+
break;
88+
if ((packetbuffer[1] == 'Q') && (replyidx == PACKET_QUAT_LEN))
89+
break;
90+
if ((packetbuffer[1] == 'B') && (replyidx == PACKET_BUTTON_LEN))
91+
break;
92+
if ((packetbuffer[1] == 'C') && (replyidx == PACKET_COLOR_LEN))
93+
break;
94+
if ((packetbuffer[1] == 'L') && (replyidx == PACKET_LOCATION_LEN))
95+
break;
96+
97+
while (ble->available()) {
98+
char c = ble->read();
99+
if (c == '!') {
100+
replyidx = 0;
101+
}
102+
packetbuffer[replyidx] = c;
103+
replyidx++;
104+
timeout = origtimeout;
105+
}
106+
107+
if (timeout == 0) break;
108+
delay(1);
109+
}
110+
111+
packetbuffer[replyidx] = 0; // null term
112+
113+
if (!replyidx) // no data or timeout
114+
return 0;
115+
if (packetbuffer[0] != '!') // doesn't start with '!' packet beginning
116+
return 0;
117+
118+
// check checksum!
119+
uint8_t xsum = 0;
120+
uint8_t checksum = packetbuffer[replyidx-1];
121+
122+
for (uint8_t i=0; i<replyidx-1; i++) {
123+
xsum += packetbuffer[i];
124+
}
125+
xsum = ~xsum;
126+
127+
// Throw an error message if the checksum's don't match
128+
if (xsum != checksum)
129+
{
130+
Serial.print("Checksum mismatch in packet : ");
131+
printHex(packetbuffer, replyidx+1);
132+
return 0;
133+
}
134+
135+
// checksum passed!
136+
return replyidx;
137+
}

0 commit comments

Comments
 (0)