1/*
2 * Author: Jon Trulson <jtrulson@ics.com>
3 * Copyright (c) 2015 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#pragma once
25
26#include <iostream>
27#include <string>
28#include <mraa/aio.hpp>
29#include <mraa/gpio.hpp>
30
31namespace upm {
32  /**
33   * @brief DFRobot CO2 Sensor
34   * @defgroup mg811 libupm-mg811
35   * @ingroup dfrobot analog gas
36   */
37
38  /**
39   * @library mg811
40   * @sensor mg811
41   * @comname DFRobot CO2 Sensor
42   * @altname MG811
43   * @type gas
44   * @man dfrobot
45   * @web http://www.dfrobot.com/index.php?route=product/product&product_id=1023
46   * @con analog
47   *
48   * @brief API for the DFRobot CO2 Sensor
49   *
50   * This sensor returns an an analog voltage that falls as the
51   * concentration of CO2 increases.  It contains a heater that must
52   * be allowed to 'warm' up the sensor before measurements are stable
53   * (hours to days is the recommendation).  It requires that the MCU
54   * be powered from an external power supply (not USB) at 5v, since
55   * the heater will consume significant current.
56   *
57   * The sensor should be allowed to 'heat' up for some time before
58   * beginning use, typically a couple of hours minimum.  It also
59   * needs fairly precise calibration at 400ppm and 1000ppm to return
60   * meaningful results.
61   *
62   * The sensor also incorporates a potentiometer that can be adjusted
63   * to specific threshold.  Once that threshold is reached, an LED
64   * on the sensor will light, and the digital pin will be driven
65   * high.
66   *
67   * @snippet mg811.cxx Interesting
68   */
69
70  class MG811 {
71  public:
72
73    /**
74     * MG811 constructor
75     *
76     * @param pin Analog pin to use
77     * @param dpin Digital pin that indicates threshold
78     * @param aref Analog reference voltage; default is 5.0 V
79     */
80    MG811(int pin, int dpin, float aref=5.0);
81
82    /**
83     * MG811 destructor
84     */
85    ~MG811();
86
87    /**
88     * Return a cumputed reference voltage to be used in calibration.
89     * @return Computed reference voltage
90     */
91    float getReferenceVoltage();
92
93    /**
94     * Set calibration parameters.  You should measure the reference
95     * voltage you get when at CO2 concentrations of 400ppm (ambient)
96     * and 1000ppm using the getReferenceVoltage() method.  Then
97     * specify those voltages here for more accurate results.
98     *
99     * @param ppm400 The measured reference voltage at 400 ppm
100     * @param ppm40000 The measured reference voltage at 1000 ppm
101     */
102    void setCalibration(float ppm400, float ppm1000);
103
104    /**
105     * Returns the voltage detected on the analog pin
106     *
107     * @return The detected voltage
108     */
109    float volts();
110
111    /**
112     * Returns the computed CO2 concentration in ppm (Parts Per
113     * Million).  This method will return 0.0 if the reference voltage
114     * is greater than the ppm400 value.  Essentially, ppm values
115     * below 400 will be reported as 0.
116     *
117     * @return The computed CO2 concentration in ppm
118     */
119    float ppm();
120
121    /**
122     * Read the digital pin and return true if the set threshold has
123     * been reached or exceeded.  This threshold is set by adjusting
124     * the potentiometer on the sensor.
125     *
126     * @return true if the threshold has been reached, false otherwise
127     */
128    bool thresholdReached();
129
130  protected:
131    mraa::Aio m_aio;
132    mraa::Gpio m_gpio;
133
134    // calibration values
135    float m_zeroPointValue;
136    float m_reactionValue;
137
138    // ADC resolution
139    int m_aRes;
140
141  private:
142    float m_aref;
143  };
144}
145
146
147