aboutsummaryrefslogtreecommitdiff
path: root/co2.h
blob: ee4f6f21f722d8c53db809296b685e497f579195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
 
#include <Arduino.h>
 
#include <SensirionI2CScd4x.h>
#include <Wire.h>
 
#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.startLowPowerPeriodicMeasurement();
    if (error) {
        Serial.print("CO2 startLowPowerPeriodicMeasurement() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
    }
  }
 
  bool dataReady() {
    uint16_t ready;
    uint16_t error;
    char errorMessage[256];
 
    error = scd4x.getDataReadyStatus(ready);
    if (error) {
        Serial.print("CO2 getDataReadyStatus() error: ");
        errorToString(error, errorMessage, 256);
        Serial.println(errorMessage);
        return false;
    }
 
    // "If last 11 bits are 0 data not ready, else data ready" 
    return (ready & 0x7FFF) != 0;
  }
  
  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);
    }
  }
};