Skip to content

Commit

Permalink
Merge pull request #8 from vladkozlov69/master
Browse files Browse the repository at this point in the history
Added support of generic streams instead of hardcoded software serial.
  • Loading branch information
tobiasschuerg authored Mar 2, 2019
2 parents 4c7de25 + 03d8500 commit db2472a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
39 changes: 22 additions & 17 deletions MHZ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ const int STATUS_NOT_READY = -5;

unsigned long lastRequest = 0;

MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type)
: co2Serial(rxpin, txpin) {
_rxpin = rxpin;
_txpin = txpin;
MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type) {
SoftwareSerial * ss = new SoftwareSerial(rxpin, txpin);
_pwmpin = pwmpin;
_type = type;

co2Serial.begin(9600);
ss->begin(9600);
_serial = ss;
}

MHZ::MHZ(Stream * serial, uint8_t pwmpin, uint8_t type) {
_serial = serial;
_pwmpin = pwmpin;
_type = type;
}

/**
Expand Down Expand Up @@ -72,19 +77,19 @@ int MHZ::readCO2UART() {
byte response[9]; // for answer

if (debug) Serial.print(F(" >> Sending CO2 request"));
co2Serial.write(cmd, 9); // request PPM CO2
_serial->write(cmd, 9); // request PPM CO2
lastRequest = millis();

// clear the buffer
memset(response, 0, 9);

int waited = 0;
while (co2Serial.available() == 0) {
while (_serial->available() == 0) {
if (debug) Serial.print(".");
delay(100); // wait a short moment to avoid false reading
if (waited++ > 10) {
if (debug) Serial.println(F("No response after 10 seconds"));
co2Serial.flush();
_serial->flush();
return STATUS_NO_RESPONSE;
}
}
Expand All @@ -94,25 +99,25 @@ int MHZ::readCO2UART() {
// to resync.
// TODO: I think this might be wrong any only happens during initialization?
boolean skip = false;
while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF) {
while (_serial->available() > 0 && (unsigned char)_serial->peek() != 0xFF) {
if (!skip) {
Serial.print(F("MHZ: - skipping unexpected readings:"));
skip = true;
}
Serial.print(" ");
Serial.print(co2Serial.peek(), HEX);
co2Serial.read();
Serial.print(_serial->peek(), HEX);
_serial->read();
}
if (skip) Serial.println();

if (co2Serial.available() > 0) {
int count = co2Serial.readBytes(response, 9);
if (_serial->available() > 0) {
int count = _serial->readBytes(response, 9);
if (count < 9) {
co2Serial.flush();
_serial->flush();
return STATUS_INCOMPLETE;
}
} else {
co2Serial.flush();
_serial->flush();
return STATUS_INCOMPLETE;
}

Expand All @@ -135,7 +140,7 @@ int MHZ::readCO2UART() {
Serial.print(F("MHZ: Should be: "));
Serial.println(check, HEX);
temperature = STATUS_CHECKSUM_MISMATCH;
co2Serial.flush();
_serial->flush();
return STATUS_CHECKSUM_MISMATCH;
}

Expand All @@ -161,7 +166,7 @@ int MHZ::readCO2UART() {
Serial.println(status, HEX);
}

co2Serial.flush();
_serial->flush();
return ppm_uart;
}

Expand Down
5 changes: 3 additions & 2 deletions MHZ.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern const int STATUS_NOT_READY;
class MHZ {
public:
MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type);
MHZ(Stream * serial, uint8_t pwmpin, uint8_t type);

void setDebug(boolean enable);

Expand All @@ -37,10 +38,10 @@ class MHZ {
uint8_t getLastTemperature();

private:
uint8_t _rxpin, _txpin, _pwmpin, _type, temperature;
uint8_t _pwmpin, _type, temperature;
boolean debug = false;

SoftwareSerial co2Serial;
Stream * _serial;
byte getCheckSum(byte *packet);
};

Expand Down

0 comments on commit db2472a

Please sign in to comment.