-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetoothCommunicator.cpp
136 lines (116 loc) · 2.86 KB
/
BluetoothCommunicator.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
#include "BluetoothCommunicator.h"
BluetoothCommunicator::BluetoothCommunicator(uint16_t dataRate)
{
Serial.begin(dataRate);
}
BluetoothCommunicator::~BluetoothCommunicator()
{
Serial.end();
}
void BluetoothCommunicator::CheckForCommands(void (*callback)(BluetoothPacket *packet))
{
BluetoothPacket *packet = ReadFromBluetooth();
if (packet)
{
BluetoothPacket *responsePacket = CopyPacket(packet);
if (packet->IsValid)
{
(*callback)(packet);
}
else
{
FlushExessData();
responsePacket->CommandDataSize = 0;
responsePacket->IsValid = false;
SendPacket(responsePacket, true);
}
}
}
void BluetoothCommunicator::FlushExessData()
{
while (Serial.available())
{
Serial.read();
}
}
void BluetoothCommunicator::SendPacket(BluetoothPacket *responsePacket, bool isForValidity)
{
byte dataSize = 4 + responsePacket->CommandDataSize;
byte *dataBuffer = new byte[dataSize];
dataBuffer[0] = responsePacket->BeginCommand;
dataBuffer[1] = (byte)responsePacket->Command;
dataBuffer[2] = responsePacket->CommandDataSize;
for (int i = 3; i < responsePacket->CommandDataSize; i++)
{
dataBuffer[i] = responsePacket->CommandData[(i - 3)];
}
if (isForValidity)
{
dataBuffer[dataSize - 1] = responsePacket->IsValid;
}
else
{
dataBuffer[dataSize - 1] = responsePacket->CRC;
}
Serial.write(dataBuffer, dataSize);
Serial.flush();
}
BluetoothPacket *BluetoothCommunicator::ReadFromBluetooth()
{
uint8_t availableBytes = 0;
if (Serial && (availableBytes = Serial.available()) > 0)
{
BluetoothPacket *packet = new BluetoothPacket();
byte beginStream = Serial.read();
if (packet->BeginCommand == beginStream)
{
packet->Command = (CommandTypes)Serial.read();
packet->CommandDataSize = Serial.read();
byte *dataBuffer = new byte[packet->CommandDataSize];
packet->CommandData = dataBuffer;
Serial.readBytes(dataBuffer, packet->CommandDataSize);
packet->CRC = Serial.read();
uint8_t calculatedCRC = CalculateCRC(dataBuffer, packet->CommandDataSize);
if (calculatedCRC == packet->CRC)
{
packet->IsValid = true;
}
else
{
packet->IsValid = false;
}
return packet;
}
else
{
return new BluetoothPacket(CommandTypes::INVALID);
}
}
else
{
return NULL;
}
}
BluetoothPacket *BluetoothCommunicator::CopyPacket(BluetoothPacket *packet)
{
BluetoothPacket *newPacket = new BluetoothPacket();
newPacket->BeginCommand = packet->BeginCommand;
newPacket->Command = packet->Command;
return newPacket;
}
uint8_t BluetoothCommunicator::CalculateCRC(uint8_t *dataBuff, uint8_t dataSize)
{
if (dataSize == 0)
{
return 0;
}
else
{
uint8_t crc = dataBuff[0];
for (byte i = 1; i < dataSize; i++)
{
crc = crc ^ dataBuff[i];
}
return crc;
}
}