Skip to content

Commit 298af58

Browse files
committed
Restore windows compatibility
1 parent 85dc46f commit 298af58

File tree

11 files changed

+477
-52
lines changed

11 files changed

+477
-52
lines changed

eeprom_programmer_PC/eeprom-programmer.pro

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ DEFINES += QT_DEPRECATED_WARNINGS
2020

2121
# Disable debug messages for release builds
2222
# evaluate only when "release" is defined of the two options "debug" and "release"
23-
CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT
24-
DEFINES += QT_NO_DEBUG_OUTPUT
23+
CONFIG(release, debug|release): {
24+
DEFINES += QT_NO_DEBUG_OUTPUT NDEBUG
25+
}
2526

2627
# You can also make your code fail to compile if you use deprecated APIs.
2728
# In order to do so, uncomment the following line.
@@ -39,18 +40,27 @@ SOURCES += \
3940
eeprom.cpp \
4041
serialportreader.cpp \
4142
serialportwriter.cpp \
42-
memorycomm.cpp \
43-
sigwatch.cpp
43+
memorycomm.cpp
4444

4545
HEADERS += \
4646
app.h \
4747
crc16.h \
4848
eeprom.h \
4949
serialportreader.h \
5050
serialportwriter.h \
51-
memorycomm.h \
52-
sigwatch.h
51+
memorycomm.h
5352

53+
unix {
54+
SOURCES += sigwatch.cpp
55+
HEADERS += sigwatch.h
56+
CONFIG(release, debug|release): \
57+
CONFIG += staticlib
58+
}
59+
60+
win32 {
61+
SOURCES += signalhandler.cpp
62+
HEADERS += signalhandler.h
63+
}
5464

5565
# Default rules for deployment.
5666
qnx: target.path = /tmp/$${TARGET}/bin

eeprom_programmer_PC/eeprom-programmer.pro.user

Lines changed: 172 additions & 9 deletions
Large diffs are not rendered by default.

eeprom_programmer_PC/src/app.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ App::~App() {
151151

152152
}
153153

154+
154155
const QString &App::getOutputFilename() const
155156
{
156157
return m_filename_out;

eeprom_programmer_PC/src/app.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ class App: public MemoryComm
2929
ST_INIT,
3030
ST_IDLE,
3131
ST_MEMID,
32-
ST_PING,
32+
ST_WAIT_PING,
3333
ST_WAIT_READMEM,
3434
ST_WAIT_WRITEMEM
3535
};
3636

37+
3738
private slots:
3839
void handleTimeout(void);
3940
void pingTimerLoop(void);
@@ -66,6 +67,7 @@ private slots:
6667
void setSignals();
6768
bool doSomething();
6869
void retryConnection();
70+
void retryOperation(operations_e);
6971
};
7072

7173
// m_ = member

eeprom_programmer_PC/src/app_.cpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
void App::reconnect()
2424
{
25+
qDebug() << "App::reconnect()";
2526
m_xferState = ST_DISCONNECTED;
2627
m_connected = false;
2728
handleXfer(nullptr);
@@ -33,12 +34,9 @@ void App::handleTimeout(void) {
3334
}
3435

3536
void App::pingTimerLoop(void) {
36-
/* if(m_xferState == ST_IDLE && m_currentOperation == OP_NONE)
37-
{
38-
sendCommand_ping();
39-
m_xferState = ST_PING;
40-
}*/
41-
setNextOperation(OP_PING);
37+
if(m_currentOperation == OP_NONE && m_nextOperation == OP_NONE) {
38+
setNextOperation(OP_PING);
39+
}
4240
handleXfer(nullptr);
4341
}
4442

@@ -48,7 +46,7 @@ void App::printError(pkgdata_t *pkg)
4846
m_standardOutput << "Unknown error from uC" << Qt::endl;
4947
else {
5048
char c_err = pkg->data[0];
51-
errorcode_e err = errorcode_e(c_err);
49+
errorcode_e err = static_cast<errorcode_e>(c_err);
5250
m_standardOutput << "uC ERROR " << EEPROM::getErrorMsg(err) << Qt::endl;
5351
}
5452
}
@@ -65,14 +63,14 @@ bool App::doSomething()
6563
switch(m_nextOperation)
6664
{
6765
case OP_RX:
68-
m_currentOperation = m_nextOperation;
6966
m_xferState = ST_WAIT_READMEM;
67+
m_currentOperation = OP_RX;
7068
readMem();
7169
break;
7270

7371
case OP_TX:
74-
m_currentOperation = m_nextOperation;
7572
m_xferState = ST_WAIT_WRITEMEM;
73+
m_currentOperation = OP_TX;
7674
if(!App::writeMem()) {
7775
m_currentOperation = OP_NONE;
7876
m_xferState = ST_IDLE;
@@ -81,22 +79,22 @@ bool App::doSomething()
8179
break;
8280

8381
case OP_PING:
84-
m_xferState = ST_PING;
85-
sendCommand_ping();
86-
break;
87-
8882
case OP_NONE:
83+
m_xferState = ST_WAIT_PING;
84+
m_currentOperation = OP_PING;
85+
sendCommand_ping();
86+
// m_pingTimer.start();
8987
break;
9088

9189
default:
9290
m_standardOutput << "Invalid operation." << Qt::endl;
91+
m_currentOperation = OP_NONE;
9392
break;
9493
}
9594

9695
// clean "Next" flag
9796
m_nextOperation = OP_NONE;
9897

99-
10098
// return true if we're gonna do something
10199
return m_currentOperation != OP_NONE;
102100
}
@@ -106,11 +104,23 @@ void App::retryConnection()
106104
clearBuffers();
107105
m_xferState = ST_DISCONNECTED;
108106
m_connected = false;
109-
QTimer::singleShot(1000, this, &App::reconnect);
107+
// QTimer::singleShot(1000, this, &App::reconnect);
108+
}
109+
110+
void App::retryOperation(operations_e op)
111+
{
112+
m_currentOperation = OP_NONE;
113+
setNextOperation(op);
114+
doSomething();
110115
}
111116

112117
void App::handleXfer(pkgdata_t *pkg) {
113-
// TODO: handle error packages
118+
// TODO: improve handle of error packages?
119+
120+
static bool busy = false;
121+
if(busy) // make sure no stupid shit happen
122+
return;
123+
busy = true;
114124

115125
switch(m_xferState)
116126
{
@@ -143,6 +153,7 @@ void App::handleXfer(pkgdata_t *pkg) {
143153

144154
if(pkg->cmd == CMD_OK) {
145155
m_xferState = ST_IDLE;
156+
m_currentOperation = OP_NONE;
146157
doSomething();
147158
}
148159
else {
@@ -155,13 +166,14 @@ void App::handleXfer(pkgdata_t *pkg) {
155166
doSomething();
156167
break;
157168

158-
case ST_PING: // waiting for ping answer
169+
case ST_WAIT_PING: // waiting for ping answer
159170

160171
if(!pkg)
161172
break;
162173

163174
if(pkg->cmd == CMD_TXRX_ACK) {
164175
m_standardOutput << "Ping." << Qt::endl;
176+
m_currentOperation = OP_NONE;
165177
m_xferState = ST_IDLE;
166178
}
167179
else {
@@ -175,45 +187,42 @@ void App::handleXfer(pkgdata_t *pkg) {
175187
if(!pkg)
176188
break;
177189

178-
m_currentOperation = OP_NONE;
179190
if(pkg->cmd == CMD_MEMDATA) {
180191
// finished receiving memory data
181192
m_memBuffer = pkg->data;
182193
printData();
183194
saveData();
184195
m_xferState = ST_IDLE;
196+
m_currentOperation = OP_NONE;
185197
QTimer::singleShot(50, qApp, SLOT(quit()));
186198
}
187199
else {
188200
printError(pkg);
189-
setNextOperation(OP_RX);
190-
doSomething();
201+
retryOperation(OP_RX);
191202
}
192-
// clearBuffers();
193203
break;
194204

195205
case ST_WAIT_WRITEMEM: // requested eeprom full write - waiting confirmation
196206

197207
if(!pkg)
198208
break;
199209

200-
m_currentOperation = OP_NONE;
201210
if(pkg->cmd == CMD_TXRX_DONE) {
202211
m_standardOutput << "Memory write SUCCESSFULLY" << Qt::endl;
203212
m_xferState = ST_IDLE;
213+
m_currentOperation = OP_NONE;
204214
QTimer::singleShot(50, qApp, SLOT(quit()));
205215
}
206216
else {
207217
printError(pkg);
208-
setNextOperation(OP_TX);
209-
doSomething();
218+
retryOperation(OP_TX);
210219
}
211-
// clearBuffers();
212220
break;
213221

214222
default:
215223
while(1); // catch the bug :-)
216224
}
225+
busy = false;
217226
}
218227
// TODO: split into simple methods
219228

eeprom_programmer_PC/src/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "app.h"
2+
#ifdef __unix
23
#include "sigwatch.h"
4+
#endif
5+
36

47
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
58
{
@@ -27,7 +30,9 @@ void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QS
2730

2831
int main(int argc, char *argv[])
2932
{
30-
qInstallMessageHandler(myMessageOutput);
33+
#ifndef NDEBUG
34+
// qInstallMessageHandler(myMessageOutput);
35+
#endif
3136

3237
App app(argc, argv);
3338

@@ -36,9 +41,8 @@ int main(int argc, char *argv[])
3641
UnixSignalWatcher sigwatch;
3742
sigwatch.watchForSignal(SIGINT);
3843
sigwatch.watchForSignal(SIGTERM);
39-
QObject::connect(&sigwatch, SIGNAL(unixSignal(int)), &app, SLOT(quit()));
44+
QObject::connect(&sigwatch, SIGNAL(unixSignal(int)), &app, SLOT(quit()));
4045
#endif
41-
4246
return app.exec();
4347
}
4448

eeprom_programmer_PC/src/memorycomm.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ void MemoryComm::setSignals()
4444

4545
connect(&m_serialPortWriter, &SerialPortWriter::packageSent,
4646
this, &MemoryComm::handlePackageSent);
47+
48+
connect( qApp, &QCoreApplication::aboutToQuit,
49+
this, &MemoryComm::handleAppQuit);
4750
}
4851

4952

@@ -59,23 +62,53 @@ void MemoryComm::setSerialPortOptions(SerialPortOptions& op)
5962

6063
MemoryComm::~MemoryComm()
6164
{
65+
qDebug() << "MemoryComm destructor.";
66+
67+
if(m_serialPort.isOpen()) {
68+
qDebug() << "SerialPort is still open.";
69+
m_serialPort.close();
70+
}
71+
}
72+
73+
void MemoryComm::handleAppQuit()
74+
{
75+
qDebug() << "MemoryComm::HandleAppQuit()";
6276
if(m_serialPort.isOpen()) {
63-
qDebug() << "SerialPort connected. Sending CMD_DISCONNECT...";
77+
qDebug() << "SerialPort connected. Sending CMD_DISCONNECT...";
6478
sendCommand(CMD_DISCONNECT);
65-
m_serialPort.waitForBytesWritten(500);
79+
while( m_serialPortWriter.busy() == true
80+
&& m_serialPort.waitForBytesWritten(5000) != false);
81+
// TODO: this doesn't seems to return on time on linux.
82+
// Nor does the signal bytesWritten get emmited.
6683
m_serialPort.close();
6784
}
6885
else {
6986
qDebug() << "SerialPort not connected.";
7087
}
88+
// MemoryComm::quit();
7189
}
7290

91+
#ifdef _WIN32
92+
bool MemoryComm::handleSignal(int signal)
93+
{
94+
qDebug() << "Handling signal " << signal;
95+
if(signal & DEFAULT_SIGNALS) {
96+
QTimer::singleShot(0, qApp, SLOT(quit()));
97+
// The thread is going to stop soon, so don't propagate this signal further
98+
return true;
99+
}
100+
else {
101+
// Let the signal propagate as though we had not been there
102+
return false;
103+
}
104+
}
105+
#endif
106+
73107
void MemoryComm::clearBuffers() {
74108
m_serialPortReader.clearBuffer();
75109
m_buffer.clear();
76110
}
77111

78-
79112
bool MemoryComm::sendCommand(commands_e cmd, uint8_t data) {
80113
return sendCommand(cmd, QByteArray(1, char(data)));
81114
}
@@ -221,7 +254,7 @@ void MemoryComm::handlePackageReceived(package_t *pkg)
221254
if(pkg->cmd == CMD_MEMDATA)
222255
{
223256
m_buffer.append((char*)(pkg->data), pkg->datalen);
224-
qDebug("Received %d bytes out of %d", m_buffer.size(), m_memsize);
257+
qDebug("Received %lld bytes out of %d", m_buffer.size(), m_memsize);
225258
if(m_buffer.size() < m_memsize) {
226259
sendCommand(CMD_TXRX_ACK);
227260
m_pending.append(CMD_READNEXT);
@@ -291,7 +324,6 @@ void MemoryComm::setPackageError(package_t *pkg, errorcode_e err)
291324

292325
void MemoryComm::packageReady(package_t *pkg)
293326
{
294-
// hasta acá el paquete llega bien.
295327
m_pkg.cmd = pkg->cmd;
296328
QByteArray data((char*)(pkg->data), pkg->datalen);
297329
m_pkg.data = data;

0 commit comments

Comments
 (0)