-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialplayback.cpp
163 lines (139 loc) · 3.4 KB
/
serialplayback.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* 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, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "mainwindow.h"
#include "serialplayback.h"
SerialPlayback::SerialPlayback(MainWindow *mainWindow) :
logPlaybackPeriod(10),
fsmStatus(FSM_SYNC0)
{
fileName = mainWindow->getFileName();
fileName = "/Users/kenz/Desktop/1212.ser";
if (fileName != "") {
loadFile(fileName);
if (file.isOpen()) {
// Set start time
logTime = 0;
logHeartbeat.start(logPlaybackPeriod);
// Connect signals
connect(&logHeartbeat, SIGNAL(timeout()), this, SLOT(on_timerTimeout()));
connect(mainWindow, SIGNAL(playbackSpeedChanged(int)), this, SLOT(on_playbackSpeedChanged(int)));
connect(this, SIGNAL(newDataReady(QByteArray, quint64)), mainWindow, SLOT(receivedData(QByteArray, quint64)));
}
}
}
SerialPlayback::~SerialPlayback()
{
file.close();
}
void SerialPlayback::loadFile(QString fileName)
{
// Open file
if (fileName != "") {
file.setFileName(fileName);
int ret = file.open(QIODevice::ReadOnly);
if (!ret) {
qDebug() << "File failed to open";
Q_ASSERT(0);
}
}
}
void SerialPlayback::parseByte(quint8 c)
{
switch (fsmStatus) {
case FSM_SYNC0:
if (c == SYNC0) {
fsmStatus = FSM_SYNC1;
} else {
fsmStatus = FSM_LOST_SYNC;
}
break;
case FSM_SYNC1:
if (c == SYNC1) {
fsmStatus = FSM_TIMESTAMP;
idx = 0;
timeStamp=0;
} else {
fsmStatus = FSM_LOST_SYNC;
}
break;
case FSM_TIMESTAMP:
//
timeStamp += (c) * (1<< 8*idx);
idx++;
if (idx == 4) {
fsmStatus = FSM_DATALENGTH;
}
break;
case FSM_DATALENGTH:
dataLength = c;
fsmStatus = FSM_DATA;
idx = 0;
break;
case FSM_DATA:
data.append(c);
idx++;
if (idx == dataLength) {
fsmStatus = FSM_END_MARKER;
}
break;
case FSM_END_MARKER:
if (c == END_MARKER) {
fsmStatus = FSM_VALID_MSG;
} else {
fsmStatus = FSM_LOST_SYNC;
}
break;
case FSM_VALID_MSG:
break;
case FSM_LOST_SYNC:
fsmStatus = FSM_SYNC0;
break;
}
}
void SerialPlayback::on_timerTimeout()
{
logTime += 1;
while (fsmStatus != FSM_VALID_MSG && !file.atEnd()) {
QByteArray fileData;
// Try to read the packet intelligently. This is less
// efficient than reading lots of data at once, but
// easier to program.
if (fsmStatus != FSM_DATA) {
fileData = file.read(1);
}
else {
fileData = file.read(dataLength);
}
foreach (quint8 byte, fileData) {
parseByte(byte);
}
}
// Check if the packet needs to be emitted
if (logTime > timeStamp && fsmStatus == FSM_VALID_MSG) {
emit newDataReady(data, timeStamp);
data.clear();
fsmStatus = FSM_SYNC0;
}
// If the file has been read, end playback
if (file.atEnd()) {
emit playbackCompleted();
}
}
void SerialPlayback::on_playbackSpeedChanged(int newPeriod)
{
logPlaybackPeriod = newPeriod;
logHeartbeat.start(logPlaybackPeriod);
}