2222
2323void 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
3536void 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
112117void 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
0 commit comments