-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageprocessor.h
132 lines (121 loc) · 4.24 KB
/
messageprocessor.h
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
#ifndef MESSAGEPROCESSOR_H
#define MESSAGEPROCESSOR_H
/*
* Processes messages sent by the platform, extracts data, and performs commands
*/
/*
* Message Format
*
* CMD d0 d1 d2 d3...dx
*
* CMD: Command code. See command code defines for a list of commands. This is a single byte
* d0,d1,dx: Data bytes. Each data point (i.e. d0) is four bytes wide. Byte streaming ends when a newline is received after four bytes.
*
* Errors:
* Framing errors:
* To shorten messages, data is sent as raw bytes rather than ASCII characters(i.e. '0', '1'). Because of this,
* messages cannot be framed with ascii characters, like comma separated values. If a user wishes to send the
* byte 0x2C, it would be interpreted as a ','.
*
* Framing errors will occur when:
* -Data isn't padded to be exactly four bytes. (i.e. you send 0xFF 0xFF 0xFF instead of 0x00 0xFF 0xFF 0xFF)
*
* Prevent framing errors by ensuring your data is padded correctly.
*
* To prevent software from locking up due to framing errors, if the incorrect sequence of bytes is received, the
* software will cancel the framing and dump all previous data.
*/
/*
* Example commands
*
* Assume no spaces between bytes
* 0x## is a number in hex
* Any character surrounded with '' is an ASCII character.
*
*
*
* Add Series
*
* Format:
* CMD ID NAME NEWLINE
*
* CMD: The command ID for adding a series.
* ID: The unique ID to be associated with the data series
* NAME: The name to be given to the data series. Does not need to be unique, but recommended
* NEWLINE: An ASCII newline (
*
* Example:
* 0x01 0x00 x00 0x00 0xFF 0x00 0x00 0x00 'T' 0x00 0x00 0x00 'E' 0x00 0x00 0x00 'S' 0x00 0x00 0x00 'T' 0x0A
*
* Creates a data series with the name "TEST" and ID 255
*
*
*
* Add Single Data Point
*
* Format:
* CMD ID DATA NEWLINE
*
* CMD: The command ID for adding a single data point
* ID: The unique ID representing the series
* DATA: The value of the data point. NOTE. This is a 32-bit floating point number!
*
* Example:
* 0x02 0x00 0x00 0x00 0xFF 0xBF 0x00 0x00 0xFF 0x0A
*
* Adds a data point to the series with the ID '255' with the
* value -0.500015.
*
* Add Series of Data Points
*
* Format:
* CMD ID DATA1 DATA2 ...DATAX NEWLINE
*
* CMD: The command ID for adding a single data point
* ID: The unique ID representing the series
* DATAX: A stream of data, every four bytes being a new data point. NOTE. These bytes are 32-bit floating point numbers!
*
* Example:
* 0x03 0x00 0x00 0x00 0xFF 0xBF 0x00 0x00 0xFF 0xC0 0x00 0x00 0x00 0xC1 0x00 0x00 0x00 0x0A
*
* Adds to the data series with ID 255 the following data points:
* -0.500015
* -2
* -8
*
*
*/
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QList>
#include <QDebug>
//Command bytes
#define COMMAND_ADD_SERIES 0x01
#define COMMAND_ADD_SINGLE_DATA 0x02
#define COMMAND_ADD_SERIES_DATA 0x03
class MessageProcessor : public QObject
{
Q_OBJECT
public:
explicit MessageProcessor(QObject *parent = nullptr);
void AddBytesToBuffer(QByteArray Data);
signals:
void NewSeriesReceived(int Series, QString Seriesname);
void DataPointsReceived(int SeriesID, QList<float> DataPoints);
public slots:
void ProcessMessage(uint8_t ControlWord, QList<uint32_t> Packets);
void ProcessAddSeriesCommand(QList<uint32_t> MessageData);
void ProcessAddDataCommand(QList<uint32_t> MessageData);
void ProcessAddSeriesDataCommand(QList<uint32_t> MessageData);
QByteArray TrimMessage(QByteArray Message);
QList<uint32_t> GetMessageData(QByteArray Message);
QList<float> MessageDataToFloats(QList<uint32_t> Data);
QString MessageDataToString(QList<uint32_t> MessageData);
private:
void ProcessSerialData();
float IntToFloat(uint32_t Data);
bool AddByteToPacket(uint8_t Byte, uint32_t &Packet, uint8_t &ByteIndex);
QByteArray SerialDataBuffer;
};
#endif // MESSAGEPROCESSOR_H