1/*
2 * Author: William Penner <william.penner@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#pragma once
25
26#include <string>
27#include <mraa/i2c.hpp>
28#include <math.h>
29
30#define HTU21D_NAME "htu21d"
31#define HTU21D_I2C_ADDRESS 0x40
32
33/* HTU21D Commands */
34#define HTU21D_READ_TEMP_HOLD     0xE3
35#define HTU21D_READ_HUMIDITY_HOLD 0xE5
36#define HTU21D_WRITE_USER_REG     0xE6
37#define HTU21D_READ_USER_REG      0xE7
38#define HTU21D_SOFT_RESET         0xFE
39
40/* User Register Bit Definition */
41#define HTU21D_DISABLE_OTP        0x02
42#define HTU21D_HEATER_ENABLE      0x04
43#define HTU21D_END_OF_BATTERY     0x40
44#define HTU21D_RESO_RH12_T14      0x00
45#define HTU21D_RESO_RH8_T12       0x01
46#define HTU21D_RESO_RH10_T13      0x80
47#define HTU21D_RESO_RH11_T11      0x81
48
49namespace upm {
50
51/**
52 * @brief HTU21D Humidity Sensor library
53 * @defgroup htu21d libupm-htu21d
54 * @ingroup seeed adafruit sparkfun i2c temp
55 */
56
57/**
58 * @library htu21d
59 * @sensor htu21d
60 * @comname HTU21D Temperature & Humidity Sensor
61 * @type temp
62 * @man seeed adafruit sparkfun
63 * @web http://www.meas-spec.com/downloads/HTU21D.pdf
64 * @con i2c
65 *
66 * @brief API for the HTU21D Temperature & Humidity Sensor
67 *
68 * HTU21D by Measurement Specialties is a digital humidity sensor with
69 * temperature output.
70 * RH reports between 0 and 100%, and the temperature range is
71 * -40 to +125 degC. Note: getCompRH is the preferred
72 * function below (passing true to cause a measurement cycle). If
73 * actual values used for the compensated ready are necessary, use
74 * the getHumidity(false) and getTemperature(false) functions following
75 * the getCompRH call.
76 * Also note the sensor should not perform more than a couple of
77 * measurements per second to limit the heating of the sensor.
78 *
79 * @image html htu21d.jpeg
80 * @snippet htu21d.cxx Interesting
81 */
82class HTU21D {
83    public:
84        /**
85         * Instantiates an HTU21D object
86         *
87         * @param bus Number of the used bus
88         * @param devAddr Address of the used I2C device
89         * @param mode HTU21D oversampling
90         */
91        HTU21D (int bus, int devAddr=HTU21D_I2C_ADDRESS);
92
93        /**
94         * Initiates a temperature/pressure mesasurement and waits for the function
95         * to complete. The humidity and temperature registers can be read
96         * after this call.
97         */
98        int sampleData(void);
99
100        /**
101         * Gets the current measured humidity [RH]
102         */
103        float getHumidity(int bSampleData = false);
104
105        /**
106         * Gets the humidity cell temperature [degC]
107         */
108        float getTemperature(int bSampleData = false);
109
110        /**
111         * Using the current humidity and temperature, the function
112         * calculates the compensated RH using the equation from
113         * the datasheet.
114         */
115        float getCompRH(int bSampleData = true);
116
117        /**
118         * Sets the heater state. The heater is used to test
119         * the sensor functionality since the temperature should increase
120         * 0.5 to 1.5 degC, and the humidity should decrease. The
121         * testSensor() function below uses the heater.
122         *
123         * @param bEnable Sets to non-zero to turn the heater on
124         */
125         int setHeater(int bEnable = false);
126
127        /**
128         * Performs a soft reset of the MPL3115A2 device to ensure
129         * it is in a known state. This function can be used to reset
130         * the min/max temperature and pressure values.
131         */
132        void resetSensor(void);
133
134        /**
135         * Tests the device and verifies it
136         * is operating correctly.
137         *
138         */
139        int testSensor(void);
140
141        /**
142         * Writes to a one-byte register
143         *
144         * @param reg Address of the register
145         * @param value Byte to be written
146         */
147        mraa::Result i2cWriteReg (uint8_t reg, uint8_t value);
148
149        /**
150         * Reads a two-byte register
151         *
152         * @param reg Address of the register
153         */
154        uint16_t i2cReadReg_16 (int reg);
155
156        /**
157         * Reads a one-byte register
158         *
159         * @param reg Address of the register
160         */
161        uint8_t i2cReadReg_8 (int reg);
162
163    private:
164
165        /**
166         * Converts the temperature register to degC * 1000
167         */
168        int32_t convertTemp(int32_t regval);
169
170        /**
171         * Converts the RH register to %RH * 1000
172         */
173        int32_t convertRH(int32_t regval);
174
175        std::string m_name;
176
177        int m_controlAddr;
178        int m_bus;
179        mraa::I2c m_i2ControlCtx;
180
181        int32_t m_temperature;
182        int32_t m_humidity;
183};
184
185}
186