Skip to content

Commit 44e2e65

Browse files
authored
rework protocol
1 parent 81e17d8 commit 44e2e65

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/VS1/VS1.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ void VS1::loop() {
171171
case State::INIT:
172172
_init();
173173
break;
174-
case State::INIT_ACK:
175-
_initAck();
174+
case State::SYNC_ENQ:
175+
_syncEnq();
176176
break;
177-
case State::WAIT:
178-
_wait();
177+
case State::SYNC_RECV:
178+
_syncRecv();
179179
break;
180180
case State::SEND:
181181
_send();
@@ -205,10 +205,12 @@ void VS1::_setState(State state) {
205205
_state = state;
206206
}
207207

208+
// wait for ENQ or reset connection if ENQ is not coming
208209
void VS1::_init() {
209210
if (_interface->available()) {
210211
if (_interface->read() == VitoWiFiInternals::ProtocolBytes.ENQ) {
211-
_setState(State::INIT_ACK);
212+
_lastMillis = _currentMillis;
213+
_setState(State::SYNC_ENQ);
212214
}
213215
} else {
214216
if (_currentMillis - _lastMillis > 3000UL) { // reset should Vitotronic be connected with VS2
@@ -218,17 +220,22 @@ void VS1::_init() {
218220
}
219221
}
220222

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+
}
225230
} else {
226231
_setState(State::INIT);
227232
}
228233
}
229234

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) {
232239
if (_currentDatapoint) {
233240
_setState(State::SEND);
234241
}
@@ -237,6 +244,7 @@ void VS1::_wait() {
237244
}
238245
}
239246

247+
// send request and move to RECEIVE
240248
void VS1::_send() {
241249
_bytesTransferred += _interface->write(&_currentRequest[_bytesTransferred], _currentRequest.length() - _bytesTransferred);
242250
if (_bytesTransferred == _currentRequest.length()) {
@@ -246,6 +254,8 @@ void VS1::_send() {
246254
}
247255
}
248256

257+
// wait for data to receive
258+
// when done, move to SYN_RECV
249259
void VS1::_receive() {
250260
while (_interface->available()) {
251261
_responseBuffer[_bytesTransferred] = _interface->read();
@@ -254,7 +264,7 @@ void VS1::_receive() {
254264
}
255265
if (_bytesTransferred == _currentRequest.length()) {
256266
_bytesTransferred = 0;
257-
_setState(State::WAIT);
267+
_setState(State::SYNC_RECV);
258268
_tryOnResponse();
259269
}
260270
}

src/VS1/VS1.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class VS1 {
5959
private:
6060
enum class State {
6161
INIT,
62-
INIT_ACK,
63-
WAIT,
62+
SYNC_ENQ,
63+
SYNC_RECV,
6464
SEND,
6565
RECEIVE,
6666
UNDEFINED
@@ -80,8 +80,8 @@ class VS1 {
8080
inline void _setState(State state);
8181

8282
void _init();
83-
void _initAck();
84-
void _wait();
83+
void _syncEnq();
84+
void _syncRecv();
8585
void _send();
8686
void _receive();
8787

0 commit comments

Comments
 (0)