@@ -158,8 +158,14 @@ bool VS1::write(const Datapoint& datapoint, const uint8_t* data, uint8_t length)
158
158
}
159
159
160
160
bool VS1::begin () {
161
- _setState (State::INIT);
162
- return _interface->begin ();
161
+ if (_interface->begin ()) {
162
+ while (_interface->available ()) {
163
+ _interface->read (); // clear rx buffer
164
+ }
165
+ _setState (State::INIT);
166
+ return true ;
167
+ }
168
+ return false ;
163
169
}
164
170
165
171
void VS1::loop () {
@@ -168,14 +174,11 @@ void VS1::loop() {
168
174
case State::INIT:
169
175
_init ();
170
176
break ;
171
- case State::INIT_ACK :
172
- _initAck ();
177
+ case State::SYNC_ENQ :
178
+ _syncEnq ();
173
179
break ;
174
- case State::IDLE:
175
- _idle ();
176
- break ;
177
- case State::PROBE_ACK:
178
- _probeAck ();
180
+ case State::SYNC_RECV:
181
+ _syncRecv ();
179
182
break ;
180
183
case State::SEND:
181
184
_send ();
@@ -189,6 +192,7 @@ void VS1::loop() {
189
192
}
190
193
// double timeout to accomodate for connection initialization
191
194
if (_currentDatapoint && _currentMillis - _requestTime > 4000UL ) {
195
+ _bytesTransferred = 0 ;
192
196
_setState (State::INIT);
193
197
_tryOnError (OptolinkResult::TIMEOUT);
194
198
}
@@ -205,10 +209,12 @@ void VS1::_setState(State state) {
205
209
_state = state;
206
210
}
207
211
212
+ // wait for ENQ or reset connection if ENQ is not coming
208
213
void VS1::_init () {
209
214
if (_interface->available ()) {
210
215
if (_interface->read () == VitoWiFiInternals::ProtocolBytes.ENQ ) {
211
- _setState (State::IDLE);
216
+ _lastMillis = _currentMillis;
217
+ _setState (State::SYNC_ENQ);
212
218
}
213
219
} else {
214
220
if (_currentMillis - _lastMillis > 3000UL ) { // reset should Vitotronic be connected with VS2
@@ -218,38 +224,32 @@ void VS1::_init() {
218
224
}
219
225
}
220
226
221
- void VS1::_initAck () {
222
- if (_interface->write (&VitoWiFiInternals::ProtocolBytes.ENQ_ACK , 1 ) == 1 ) {
223
- _setState (State::IDLE);
224
- _lastMillis = _currentMillis;
227
+ // if we want to send something within 50msec of receiving the ENQ, send ENQ_ACK and move to SEND
228
+ // if > 50msec, return to INIT
229
+ void VS1::_syncEnq () {
230
+ if (_currentMillis - _lastMillis < 50 ) {
231
+ if (_currentDatapoint && _interface->write (&VitoWiFiInternals::ProtocolBytes.ENQ_ACK , 1 ) == 1 ) {
232
+ _setState (State::SEND);
233
+ _send (); // speed up things
234
+ }
225
235
} else {
226
236
_setState (State::INIT);
227
237
}
228
238
}
229
239
230
- void VS1::_idle () {
231
- if (_currentDatapoint) {
232
- _setState (State::SEND);
233
- } else if (_currentMillis - _lastMillis > 500 ) {
234
- if (_interface->write (&VitoWiFiInternals::ProtocolBytes.PROBE [0 ], 4 ) == 4 ) {
235
- _lastMillis = _currentMillis;
236
- _setState (State::PROBE_ACK);
237
- } else {
238
- _setState (State::INIT);
240
+ // if we want to send something within 50msec of previous SEND, send again
241
+ // if > 50msec, return to INIT
242
+ void VS1::_syncRecv () {
243
+ if (_currentMillis - _lastMillis < 50 ) {
244
+ if (_currentDatapoint) {
245
+ _setState (State::SEND);
239
246
}
240
- }
241
- }
242
-
243
- void VS1::_probeAck () {
244
- if (_interface->available () == 2 ) {
245
- _interface->read ();
246
- _interface->read ();
247
- _setState (State::IDLE);
248
- } else if (_currentMillis - _lastMillis > 1000UL ) {
247
+ } else {
249
248
_setState (State::INIT);
250
249
}
251
250
}
252
251
252
+ // send request and move to RECEIVE
253
253
void VS1::_send () {
254
254
_bytesTransferred += _interface->write (&_currentRequest[_bytesTransferred], _currentRequest.length () - _bytesTransferred);
255
255
if (_bytesTransferred == _currentRequest.length ()) {
@@ -259,22 +259,24 @@ void VS1::_send() {
259
259
}
260
260
}
261
261
262
+ // wait for data to receive
263
+ // when done, move to SYN_RECV
262
264
void VS1::_receive () {
263
265
while (_interface->available ()) {
264
266
_responseBuffer[_bytesTransferred] = _interface->read ();
265
267
++_bytesTransferred;
266
268
_lastMillis = _currentMillis;
267
269
}
268
- if (_bytesTransferred == _currentRequest .length ()) {
270
+ if (_bytesTransferred == _currentDatapoint .length ()) {
269
271
_bytesTransferred = 0 ;
270
- _setState (State::IDLE );
272
+ _setState (State::SYNC_RECV );
271
273
_tryOnResponse ();
272
274
}
273
275
}
274
276
275
277
void VS1::_tryOnResponse () {
276
278
if (_onResponseCallback) {
277
- _onResponseCallback (_responseBuffer, _currentRequest .length (), _currentDatapoint);
279
+ _onResponseCallback (_responseBuffer, _currentDatapoint .length (), _currentDatapoint);
278
280
}
279
281
_currentDatapoint = Datapoint (nullptr , 0 , 0 , noconv);
280
282
}
0 commit comments