Skip to content

Commit 9bd326d

Browse files
committed
1.5.4
Backwards compatability added for CalibrateZero(); Desync correction added on data mismatch.
1 parent 678c63c commit 9bd326d

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/MHZ19.cpp

+20-10
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,8 @@ byte MHZ19::read(byte inBytes[MHZ19_DATA_LEN], Command_Type commandnumber)
583583
this->errorCode = RESULT_TIMEOUT;
584584

585585
/* clear incomplete 9 byte values, limit is finite */
586-
inBytes[1] = mySerial->available();
587-
for(uint8_t x = 0; x < inBytes[1]; x++)
588-
{
589-
inBytes[0] = mySerial->read();
590-
#if defined (ESP32) && (MHZ19_ERRORS)
591-
ESP_LOGW(TAG_MHZ19, "Clearing Byte: %d", inBytes[0]);
592-
#elif MHZ19_ERRORS
593-
Serial.print("!Warning: Clearing Byte: "); Serial.println(inBytes[0]);
594-
#endif
595-
}
586+
cleanUp(mySerial->available());
587+
596588
//return error condition
597589
return RESULT_TIMEOUT;
598590
}
@@ -612,7 +604,11 @@ byte MHZ19::read(byte inBytes[MHZ19_DATA_LEN], Command_Type commandnumber)
612604

613605
/* construct error code */
614606
if (inBytes[0] != this->storage.constructedCommand[0] || inBytes[1] != this->storage.constructedCommand[2])
607+
{
608+
/* clear rx buffer for deysnc correction */
609+
cleanUp(mySerial->available());
615610
this->errorCode = RESULT_MATCH;
611+
}
616612

617613
/* if error has been assigned */
618614
if (this->errorCode == RESULT_NULL)
@@ -625,6 +621,20 @@ byte MHZ19::read(byte inBytes[MHZ19_DATA_LEN], Command_Type commandnumber)
625621
return this->errorCode;
626622
}
627623

624+
void MHZ19::cleanUp(uint8_t cnt)
625+
{
626+
uint8_t eject = 0;
627+
for(uint8_t x = 0; x < cnt; x++)
628+
{
629+
eject = mySerial->read();
630+
#if defined (ESP32) && (MHZ19_ERRORS)
631+
ESP_LOGW(TAG_MHZ19, "Clearing Byte: %d", eject);
632+
#elif MHZ19_ERRORS
633+
Serial.print("!Warning: Clearing Byte: "); Serial.println(eject);
634+
#endif
635+
}
636+
}
637+
628638
void MHZ19::handleResponse(Command_Type commandtype)
629639
{
630640
if (this->storage.constructedCommand[2] == Commands[RAWCO2]) // compare commands byte

src/MHZ19.h

+5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class MHZ19
110110
/* Calibrates "Zero" (Note: Zero refers to 400ppm for this sensor)*/
111111
void calibrate();
112112

113+
/* Calibrate Backwards compatability */
114+
void inline calibrateZero(){ calibrate(); };
115+
113116
/* requests a reset */
114117
void recoveryReset();
115118

@@ -199,5 +202,7 @@ class MHZ19
199202

200203
/* converts bytes to integers according to *256 and + value */
201204
unsigned int makeInt(byte high, byte low);
205+
206+
void cleanUp(uint8_t cnt);
202207
};
203208
#endif

0 commit comments

Comments
 (0)