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 <string>
27 #include <iostream>
28 
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <termios.h>
36 #include <sys/time.h>
37 #include <sys/select.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 
41 #include <mraa/uart.h>
42 
43 const int MHZ16_DEFAULT_UART = 0;
44 
45 // protocol start and end codes
46 const uint8_t MHZ16_START = 0x7e;
47 const uint8_t MHZ16_END   = 0x7e;
48 
49 namespace upm {
50     /**
51      * @brief MHZ16 Serial CO2 Sensor library
52      * @defgroup mhz16 libupm-mhz16
53      * @ingroup seeed uart gaseous
54      */
55 
56     /**
57      * @library mhz16
58      * @sensor mhz16
59      * @comname Grove CO2 Sensor
60      * @altname MHZ16 Serial CO2 Sensor
61      * @type gaseous
62      * @man seeed
63      * @con uart
64      *
65      * @brief API support for the Grove CO2 sensor
66      *
67      * This class implements support for the Grove CO2 sensor.
68      *
69      * Its CO2 detection range is 0-2,000 ppm. It requires a
70      * 2-3 minute warm-up time before reporting valid data.
71      *
72      * @image html mhz16.jpg
73      * @snippet mhz16.cxx Interesting
74      */
75   class MHZ16 {
76   public:
77 
78 
79     /**
80      * MHZ16 constructor
81      *
82      * @param uart Default UART to use (0 or 1)
83      */
84     MHZ16(int uart);
85 
86     /**
87      * MHZ16 destructor
88      */
89     ~MHZ16();
90 
91     /**
92      * Checks to see if there is data available for reading
93      *
94      * @param millis Number of milliseconds to wait; 0 means no waiting.
95      * @return True if there is data available for reading
96      */
97     bool dataAvailable(unsigned int millis);
98 
99     /**
100      * Reads any available data in a user-supplied buffer. Note: the
101      * call blocks until data is available to be read. Use
102      * dataAvailable() to determine whether there is data available
103      * beforehand, to avoid blocking.
104      *
105      * @param buffer Buffer to hold the data read
106      * @param len Length of the buffer
107      * @return Number of bytes read
108      */
109     int readData(char *buffer, int len);
110 
111     /**
112      * Writes the data in the buffer to the device
113      *
114      * @param buffer Buffer to hold the data read
115      * @param len Length of the buffer
116      * @return Number of bytes written
117      */
118     int writeData(char *buffer, int len);
119 
120     /**
121      * Sets up proper tty I/O modes and the baud rate. The default
122      * baud rate is 9,600 (B9600).
123      *
124      * @param baud Desired baud rate.
125      * @return True if successful
126      */
127     bool setupTty(speed_t baud=B9600);
128 
129     /**
130      * Verifies the packet header and indicates its validity
131      *
132      * @param pkt Packet to check
133      * @return True if the checksum is valid, false otherwise
134      */
135     bool verifyPacket(uint8_t *pkt, int len);
136 
137     /**
138      * Queries the sensor and returns gas (CO2) concentration and
139      * temperature data.
140      *
141      * @param gas Returned gas concentration
142      * @param temp Returned temperature in Celsius
143      * @return True if successful
144      */
145     bool getData(int *gas, int *temp);
146 
147     /**
148      * Sets the zero point of the sensor
149      *
150      */
151     void calibrateZeroPoint();
152 
153   protected:
ttyFd()154     int ttyFd() { return m_ttyFd; };
155 
156   private:
157     mraa_uart_context m_uart;
158     int m_ttyFd;
159   };
160 }
161 
162 
163