-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoSerialPacketConn.cpp
More file actions
102 lines (86 loc) · 2.73 KB
/
ArduinoSerialPacketConn.cpp
File metadata and controls
102 lines (86 loc) · 2.73 KB
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
/*
Library for Packet-Based serial communications.
Copyright (C) 2017 Scott McKittrick
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Created By: Scott McKittrick Dec. 31st 2016
*/
#include "ArduinoSerialPacketConn.h"
ArduinoSerialPacketConn::ArduinoSerialPacketConn(long speed)
{
baudRate = speed;
processBlock = false;
recvCount = 0;
myReceiver = nullptr;
}
//Sets the callback function
void ArduinoSerialPacketConn::setPacketReceiver(PacketReceiver receiver)
{
myReceiver = receiver;
}
int ArduinoSerialPacketConn::connect()
{
Serial.begin(baudRate);
return 0;
}
int ArduinoSerialPacketConn::disconnect()
{
Serial.end();
return 0;
}
void ArduinoSerialPacketConn::readPacket()
{
//std::cout << "Process called\n";
//Read in as much data as is available until we reach a max size, or a frame end character
bool packetComplete = false;
uint8_t buffer;
while(Serial.available() && (recvCount < MAXCOBSPACKETLEN) && !packetComplete)
{
//std::cout << "Received data. ";
buffer = Serial.read();
packet[recvCount] = buffer;
//printf("%02X\n", buffer);
recvCount++;
//Serial.write(0xF1);
if(buffer == COBS_FRAMEEND)
{
packetComplete = true;
//Serial.write(0xF2);
}
//recvCount++;
}
if(packetComplete)
{
//Serial.write(packet, recvCount);
if((myReceiver != nullptr) ) //If someone is listening, notify them of the new packet
{
//Allocate memory for the data
uint8_t *data = new uint8_t[dataLenFromPacketLen(recvCount)];
int processResult = processPacket(packet, recvCount, data, dataLenFromPacketLen(recvCount));
if(processResult > 0)
{
//Serial.write(0xF4);
(*myReceiver)(data, processResult);
}
delete[] data;
}
recvCount = 0;
}
//If we receive a packet longer than the maximum cobs length, drop it.
if(recvCount > MAXCOBSPACKETLEN)
recvCount = 0;
}
int ArduinoSerialPacketConn::sendMessage(const uint8_t *data, int dataLength)
{
uint8_t sPacket[packetLenFromDataLen(dataLength)];
int packetLen = buildPacket(data, dataLength, sPacket, packetLenFromDataLen(dataLength));
Serial.write(sPacket, packetLen);
}