diff --git a/include/attsensor.h b/include/attsensor.h index dc46e72..76055af 100644 --- a/include/attsensor.h +++ b/include/attsensor.h @@ -40,6 +40,9 @@ class AttSensor { // Return the number of Bytes added to the Payload virtual uint8_t numBytes(void) = 0; + // Calibrate a Sensor. Needs to be Implemented in the Child Class if supported + void calibrate(void) {}; + // Helper Functions to Put Values Into the Payload Array static void int32ToPayload(int32_t value, char *payload, uint8_t startbyte) { payload[startbyte] = (value) & 0XFF; diff --git a/lib/MHZ19C/MHZ19C.cpp b/lib/MHZ19C/MHZ19C.cpp index 00801ac..50d15b6 100644 --- a/lib/MHZ19C/MHZ19C.cpp +++ b/lib/MHZ19C/MHZ19C.cpp @@ -29,8 +29,15 @@ // Constructor - Inititalize Hardware UART MHZ19C::MHZ19C(void) { - Serial.begin(9600); - Serial.setTimeout(MHZ19C_READ_TIMEOUT); + Serial.begin(9600); + Serial.setTimeout(MHZ19C_READ_TIMEOUT); +} + +MHZ19C::MHZ19C(pin_size_t calpin) { + this->calpin = calpin; + + Serial.begin(9600); + Serial.setTimeout(MHZ19C_READ_TIMEOUT); } void MHZ19C::initialize(void) { @@ -41,6 +48,14 @@ void MHZ19C::initialize(void) { #endif } +void MHZ19C::calibrate(void) { + pinMode(calpin, OUTPUT); + digitalWrite(calpin, LOW); + delay(7500); + digitalWrite(calpin, HIGH); + pinMode(PIN_PB4, INPUT_PULLUP); +} + uint8_t MHZ19C::getSensorData(char * payload, uint8_t startbyte) { write(MHZ19C_CMD_GET_PPM, 0x00); delay(50); diff --git a/lib/MHZ19C/MHZ19C.h b/lib/MHZ19C/MHZ19C.h index 2250445..55c12b6 100644 --- a/lib/MHZ19C/MHZ19C.h +++ b/lib/MHZ19C/MHZ19C.h @@ -38,6 +38,7 @@ class MHZ19C : public AttSensor { private: uint8_t buffer[MHZ19C_SER_BUF_LEN]; + pin_size_t calpin = PIN_PB4; // PB4 is the Calibration Pin on the Addon PCB void write(byte cmd, byte arg); uint8_t read(); @@ -47,7 +48,9 @@ class MHZ19C : public AttSensor { public: MHZ19C(void); + MHZ19C(pin_size_t calpin); void initialize(void); + void calibrate(void); uint8_t numBytes(void) {return 2;}; uint8_t getSensorData(char *payload, uint8_t startbyte); void setSelfCalibration(bool state); diff --git a/src/main.cpp b/src/main.cpp index a2ca9d3..76dcc83 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -364,17 +364,14 @@ void setup() void loop() { // Handle long Button Press for Calibration with MH-Z19C Sensor - #if defined HAS_MHZ19C && defined BTN_PIN + #ifdef BTN_PIN if (digitalRead(BTN_PIN) == LOW) { - // Press Button longer than 4 Seconds -> Start MH-Z19C Calibration Routine + // Press Button longer than 4 Seconds -> Start Sensor Calibration Routine (if applicable) unsigned long loop_millis = millis(); if ((unsigned long)(loop_millis - btn_millis) >= 4000) { WS2812B_SETLED(1,153,0,153); - pinMode(PIN_PB4, OUTPUT); - digitalWrite(PIN_PB4, LOW); - delay(7500); - digitalWrite(PIN_PB4, HIGH); - pinMode(PIN_PB4, INPUT_PULLUP); + for (uint8_t i=0; icalibrate(); WS2812B_SETLED(1,0,0,0); } else { delay(500); @@ -383,7 +380,7 @@ void loop() #endif // Only Run the LMIC loop here. Actual Sending Code is in do_send() os_runloop_once(); - #if defined HAS_MHZ19C && defined BTN_PIN + #ifdef BTN_PIN } #endif } \ No newline at end of file