diff --git a/MHZ.cpp b/MHZ.cpp index b4af86a..44f005e 100644 --- a/MHZ.cpp +++ b/MHZ.cpp @@ -5,6 +5,16 @@ #include "MHZ.h" +const int MHZ14A = 14; +const int MHZ19B = 19; + +const int STATUS_NO_RESPONSE = -2; +const int STATUS_CHECKSUM_MISMATCH = -3; +const int STATUS_INCOMPLETE = -4; +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; @@ -15,6 +25,9 @@ MHZ::MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type) co2Serial.begin(9600); } +/** + * Enables or disables the debug mode (more logging). + */ void MHZ::setDebug(boolean enable) { debug = enable; if (debug) { @@ -24,15 +37,40 @@ void MHZ::setDebug(boolean enable) { } } -int retryCount = 0; +boolean MHZ::isPreHeating() { + if (_type == MHZ14A) { + return millis() < (3 * 60 * 1000); + } else if (_type == MHZ19B) { + return millis() < (3 * 60 * 1000); + } else { + Serial.println("MHZ::isPreHeating() => UNKNOWN SENSOR"); + return false; + } +} + +boolean MHZ::isReady() { + if (isPreHeating()) return false; + if (_type == MHZ14A) + return lastRequest < millis() - 90 * 1000; + else if (_type == MHZ19B) + return lastRequest < millis() - 60 * 1000; + else { + Serial.print("MHZ::isReady() => UNKNOWN SENSOR \""); + Serial.print(_type); + Serial.println("\""); + return true; + } +} int MHZ::readCO2UART() { + if (!isReady()) return STATUS_NOT_READY; if (debug) Serial.println("-- read CO2 uart ---"); byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; byte response[9]; // for answer if (debug) Serial.print(" >> Sending CO2 request"); co2Serial.write(cmd, 9); // request PPM CO2 + lastRequest = millis(); // clear the buffer memset(response, 0, 9); @@ -124,9 +162,12 @@ int MHZ::readCO2UART() { return ppm_uart; } -uint8_t MHZ::getLastTemperature() { return temperature; } +uint8_t MHZ::getLastTemperature() { + if (!isReady()) return STATUS_NOT_READY; + return temperature; +} -byte MHZ::getCheckSum(byte *packet) { +byte MHZ::getCheckSum(byte* packet) { if (debug) Serial.println(" getCheckSum()"); byte i; unsigned char checksum = 0; @@ -139,6 +180,7 @@ byte MHZ::getCheckSum(byte *packet) { } int MHZ::readCO2PWM() { + // if (!isReady()) return STATUS_NOT_READY; not needed? if (debug) Serial.print("-- reading CO2 from pwm "); unsigned long th, tl, ppm_pwm = 0; do { diff --git a/MHZ.h b/MHZ.h index b022aca..2af5e29 100644 --- a/MHZ.h +++ b/MHZ.h @@ -11,16 +11,18 @@ #include "WProgram.h" #endif -// Define types of sensors. -#define MHZ14A 14 -#define MHZ19B 19 - -#define STATUS_NO_RESPONSE -2 -#define STATUS_CHECKSUM_MISMATCH -3 -#define STATUS_INCOMPLETE -4 - #include +// types of sensors. +extern const int MHZ14A; +extern const int MHZ19B; + +// status codes +extern const int STATUS_NO_RESPONSE; +extern const int STATUS_CHECKSUM_MISMATCH; +extern const int STATUS_INCOMPLETE; +extern const int STATUS_NOT_READY; + class MHZ { public: MHZ(uint8_t rxpin, uint8_t txpin, uint8_t pwmpin, uint8_t type); diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..35f4450 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,41 @@ +####################################### +# Syntax Coloring Map For MHZ +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +MHZ KEYWORD1 + + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +setDebug KEYWORD2 +isPreHeating KEYWORD2 +isReady KEYWORD2 +readCO2UART KEYWORD2 +readCO2PWM KEYWORD2 +getLastTemperature KEYWORD2 + + +####################################### +# Instances (KEYWORD2) +####################################### + +co2 KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### + +MHZ14A LITERAL1 +MHZ19B LITERAL1 + +STATUS_NO_RESPONSE LITERAL1 +STATUS_CHECKSUM_MISMATCH LITERAL1 +STATUS_INCOMPLETE LITERAL1 +STATUS_NOT_READY LITERAL1 \ No newline at end of file