@@ -171,11 +171,11 @@ void VS1::loop() {
171
171
case State::INIT:
172
172
_init ();
173
173
break ;
174
- case State::INIT_ACK :
175
- _initAck ();
174
+ case State::SYNC_ENQ :
175
+ _syncEnq ();
176
176
break ;
177
- case State::WAIT :
178
- _wait ();
177
+ case State::SYNC_RECV :
178
+ _syncRecv ();
179
179
break ;
180
180
case State::SEND:
181
181
_send ();
@@ -205,10 +205,12 @@ void VS1::_setState(State state) {
205
205
_state = state;
206
206
}
207
207
208
+ // wait for ENQ or reset connection if ENQ is not coming
208
209
void VS1::_init () {
209
210
if (_interface->available ()) {
210
211
if (_interface->read () == VitoWiFiInternals::ProtocolBytes.ENQ ) {
211
- _setState (State::INIT_ACK);
212
+ _lastMillis = _currentMillis;
213
+ _setState (State::SYNC_ENQ);
212
214
}
213
215
} else {
214
216
if (_currentMillis - _lastMillis > 3000UL ) { // reset should Vitotronic be connected with VS2
@@ -218,17 +220,22 @@ void VS1::_init() {
218
220
}
219
221
}
220
222
221
- void VS1::_initAck () {
222
- if (_interface->write (&VitoWiFiInternals::ProtocolBytes.ENQ_ACK , 1 ) == 1 ) {
223
- _setState (State::WAIT);
224
- _lastMillis = _currentMillis;
223
+ // if we want to send something within 50msec of receiving the ENQ, send ENQ_ACK and move to SEND
224
+ // if > 50msec, return to INIT
225
+ void VS1::_syncEnq () {
226
+ if (_currentMillis - _lastMillis < 50 ) {
227
+ if (_currentDatapoint && _interface->write (&VitoWiFiInternals::ProtocolBytes.ENQ_ACK , 1 ) == 1 ) {
228
+ _setState (State::SEND);
229
+ }
225
230
} else {
226
231
_setState (State::INIT);
227
232
}
228
233
}
229
234
230
- void VS1::_wait () {
231
- if (_currentMillis - _lastMillis < 50 ) { // don't reinitialize if within 50 msec
235
+ // if we want to send something within 50msec of previous SEND, send again
236
+ // if > 50msec, return to INIT
237
+ void VS1::_syncRecv () {
238
+ if (_currentMillis - _lastMillis < 50 ) {
232
239
if (_currentDatapoint) {
233
240
_setState (State::SEND);
234
241
}
@@ -237,6 +244,7 @@ void VS1::_wait() {
237
244
}
238
245
}
239
246
247
+ // send request and move to RECEIVE
240
248
void VS1::_send () {
241
249
_bytesTransferred += _interface->write (&_currentRequest[_bytesTransferred], _currentRequest.length () - _bytesTransferred);
242
250
if (_bytesTransferred == _currentRequest.length ()) {
@@ -246,6 +254,8 @@ void VS1::_send() {
246
254
}
247
255
}
248
256
257
+ // wait for data to receive
258
+ // when done, move to SYN_RECV
249
259
void VS1::_receive () {
250
260
while (_interface->available ()) {
251
261
_responseBuffer[_bytesTransferred] = _interface->read ();
@@ -254,7 +264,7 @@ void VS1::_receive() {
254
264
}
255
265
if (_bytesTransferred == _currentRequest.length ()) {
256
266
_bytesTransferred = 0 ;
257
- _setState (State::WAIT );
267
+ _setState (State::SYNC_RECV );
258
268
_tryOnResponse ();
259
269
}
260
270
}
0 commit comments