This repository was archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandler.cpp
More file actions
85 lines (75 loc) · 1.29 KB
/
MessageHandler.cpp
File metadata and controls
85 lines (75 loc) · 1.29 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
#include "MessageHandler.h"
MessageHandler::MessageHandler(uint8_t msgBegin)
{
_data = NULL;
_msg = 0;
_len = 0;
_handled = true;
_msgBegin = msgBegin;
}
bool MessageHandler::Update()
{
if(_handled)
{
if(_msg == 0) // Waiting for the message
{
if(Serial.available() >= 3)
{
if(Serial.read() == _msgBegin)
{
_msg = Serial.read();
_len = Serial.read();
if(_data)
{
delete[] _data;
_data = NULL;
}
if(_len > 0)
{
_data = new uint8_t[_len];
}
else
{
_handled = false;
}
}
}
}
else // Message received, retrieving data
{
if(Serial.available() >= _len && _data != NULL)
{
Serial.readBytes(_data, _len);
_handled = false;
}
}
}
return (_handled == false);
}
uint8_t MessageHandler::GetMessageDataLength() const
{
return _len;
}
void MessageHandler::GetMessage(uint8_t* msg, uint8_t* data)
{
*msg = _msg;
if(data && _data)
memcpy(data, _data, _len);
if(_data)
{
delete[] _data;
_data = NULL;
}
_msg = 0;
_len = 0;
_handled = true;
}
void MessageHandler::SendMessage(uint8_t msg, uint8_t len, uint8_t* data)
{
uint8_t query[] = {_msgBegin, msg, len};
Serial.write(query, sizeof(query) / sizeof(uint8_t));
if(len > 0 && data)
{
Serial.write(data, len);
}
}