Skip to content

Commit 421b077

Browse files
committed
fix VS1
1 parent c917b43 commit 421b077

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

src/Constants.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ constexpr struct {
6464
uint8_t ENQ = 0x05;
6565
uint8_t EOT = 0x04;
6666
uint8_t SYNC[3] = {0x16, 0x00, 0x00};
67-
uint8_t PROBE[4] = {0xF7, 0x00, 0xF8, 0x02};
6867
} ProtocolBytes;
6968

7069
enum class ParserResult {

src/VS1/VS1.cpp

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,14 @@ bool VS1::write(const Datapoint& datapoint, const uint8_t* data, uint8_t length)
158158
}
159159

160160
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;
163169
}
164170

165171
void VS1::loop() {
@@ -168,14 +174,11 @@ void VS1::loop() {
168174
case State::INIT:
169175
_init();
170176
break;
171-
case State::INIT_ACK:
172-
_initAck();
177+
case State::SYNC_ENQ:
178+
_syncEnq();
173179
break;
174-
case State::IDLE:
175-
_idle();
176-
break;
177-
case State::PROBE_ACK:
178-
_probeAck();
180+
case State::SYNC_RECV:
181+
_syncRecv();
179182
break;
180183
case State::SEND:
181184
_send();
@@ -189,6 +192,7 @@ void VS1::loop() {
189192
}
190193
// double timeout to accomodate for connection initialization
191194
if (_currentDatapoint && _currentMillis - _requestTime > 4000UL) {
195+
_bytesTransferred = 0;
192196
_setState(State::INIT);
193197
_tryOnError(OptolinkResult::TIMEOUT);
194198
}
@@ -205,10 +209,12 @@ void VS1::_setState(State state) {
205209
_state = state;
206210
}
207211

212+
// wait for ENQ or reset connection if ENQ is not coming
208213
void VS1::_init() {
209214
if (_interface->available()) {
210215
if (_interface->read() == VitoWiFiInternals::ProtocolBytes.ENQ) {
211-
_setState(State::IDLE);
216+
_lastMillis = _currentMillis;
217+
_setState(State::SYNC_ENQ);
212218
}
213219
} else {
214220
if (_currentMillis - _lastMillis > 3000UL) { // reset should Vitotronic be connected with VS2
@@ -218,38 +224,32 @@ void VS1::_init() {
218224
}
219225
}
220226

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+
}
225235
} else {
226236
_setState(State::INIT);
227237
}
228238
}
229239

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);
239246
}
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 {
249248
_setState(State::INIT);
250249
}
251250
}
252251

252+
// send request and move to RECEIVE
253253
void VS1::_send() {
254254
_bytesTransferred += _interface->write(&_currentRequest[_bytesTransferred], _currentRequest.length() - _bytesTransferred);
255255
if (_bytesTransferred == _currentRequest.length()) {
@@ -259,22 +259,24 @@ void VS1::_send() {
259259
}
260260
}
261261

262+
// wait for data to receive
263+
// when done, move to SYN_RECV
262264
void VS1::_receive() {
263265
while (_interface->available()) {
264266
_responseBuffer[_bytesTransferred] = _interface->read();
265267
++_bytesTransferred;
266268
_lastMillis = _currentMillis;
267269
}
268-
if (_bytesTransferred == _currentRequest.length()) {
270+
if (_bytesTransferred == _currentDatapoint.length()) {
269271
_bytesTransferred = 0;
270-
_setState(State::IDLE);
272+
_setState(State::SYNC_RECV);
271273
_tryOnResponse();
272274
}
273275
}
274276

275277
void VS1::_tryOnResponse() {
276278
if (_onResponseCallback) {
277-
_onResponseCallback(_responseBuffer, _currentRequest.length(), _currentDatapoint);
279+
_onResponseCallback(_responseBuffer, _currentDatapoint.length(), _currentDatapoint);
278280
}
279281
_currentDatapoint = Datapoint(nullptr, 0, 0, noconv);
280282
}

src/VS1/VS1.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ class VS1 {
5959
private:
6060
enum class State {
6161
INIT,
62-
INIT_ACK,
63-
IDLE,
64-
PROBE_ACK,
62+
SYNC_ENQ,
63+
SYNC_RECV,
6564
SEND,
6665
RECEIVE,
6766
UNDEFINED
@@ -81,10 +80,8 @@ class VS1 {
8180
inline void _setState(State state);
8281

8382
void _init();
84-
void _initAck();
85-
void _idle();
86-
void _probeAck();
87-
void _sync();
83+
void _syncEnq();
84+
void _syncRecv();
8885
void _send();
8986
void _receive();
9087

0 commit comments

Comments
 (0)