From 3443edf23337fb5d9ce63075ead80c4779cf823a Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 27 Mar 2022 12:48:36 +0100 Subject: break code into logical components --- co2.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 co2.h (limited to 'co2.h') diff --git a/co2.h b/co2.h new file mode 100644 index 0000000..ac963f4 --- /dev/null +++ b/co2.h @@ -0,0 +1,62 @@ +#pragma once + +#include + +#include +#include + +#include "data.h" + +class CO2 { +private: + SensirionI2CScd4x scd4x; + +public: + CO2() : scd4x() {} + + void start() { + uint16_t error; + char errorMessage[256]; + + scd4x.begin(Wire); + + // stop potentially previously started measurement + error = scd4x.stopPeriodicMeasurement(); + if (error) { + Serial.print("CO2 stopPeriodicMeasurement() error: "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } + + error = scd4x.setAutomaticSelfCalibration(1); + if (error) { + Serial.print("CO2 setAutomaticSelfCalibration() error: "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } + + // Start Measurement + error = scd4x.startPeriodicMeasurement(); + if (error) { + Serial.print("CO2 startPeriodicMeasurement() error: "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } + } + + bool dataReady() { + return true; + } + + void read(SensorData *data) { + uint16_t error; + char errorMessage[256]; + + error = scd4x.readMeasurement(data->co2, data->temperature, data->humidity); + if (error) { + Serial.print("CO2 readMeasurement() error: "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } + } +}; -- cgit v1.2.3