th02:: added new humidity&temperature sensor
[contrib/upm.git] / src / th02 / th02.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Credits to Seeed Studeo.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 #pragma once
27
28 #include <string>
29 #include <mraa/i2c.h>
30
31 #define ADDR                0x40 // device address
32
33 #define REG_STATUS          0x00
34 #define REG_DATA_H          0x01
35 #define REG_DATA_L          0x02
36 #define REG_CONFIG          0x03
37 #define REG_ID              0x11
38
39 #define STATUS_RDY_MASK     0x01
40
41 #define CMD_MEASURE_HUMI    0x01
42 #define CMD_MEASURE_TEMP    0x11
43
44 #define TH02_WR_REG_MODE    0xC0
45 #define TH02_RD_REG_MODE    0x80
46
47 #define HIGH                1
48 #define LOW                 0
49
50 namespace upm {
51
52 /**
53  * @brief C++ API for TH02 chip (Temperature and Humidity Sensor Pro)
54  *
55  * This file defines the TH02 C++ interface for libth02
56  *
57  * @snippet th02-example.cxx Interesting
58  *
59  */
60 class TH02 {
61     public:
62         /**
63          * Instanciates a TH02 object
64          */
65         TH02 ();
66
67         /**
68          * TH02 object destructor, basicaly it close i2c connection.
69          */
70         ~TH02 ();
71
72         /**
73          * Get the temperature value from sensor.
74          */
75         float getTemperature ();
76
77         /**
78          * Get the humidity value from sensor.
79          */
80         float getHumidity ();
81
82         /**
83          * Get the sensor's status.
84          */
85         bool getStatus ();
86
87         /**
88          * Return name of the component
89          */
90         std::string name()
91         {
92             return m_name;
93         }
94     private:
95         std::string m_name;
96         mraa_i2c_context m_i2Ctx;
97
98         uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
99         mraa_result_t i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer);
100         mraa_result_t i2cWriteReg (uint8_t reg, uint8_t data);
101 };
102
103 }