Home | History | Annotate | Download | only in am2315
      1 /*
      2  * Author: William Penner <william.penner (at) 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.h>
     28 #include <math.h>
     29 
     30 #define AM2315_NAME "am2315"
     31 #define AM2315_I2C_ADDRESS 0x5c
     32 
     33 #define AM2315_READ     0x03
     34 #define AM2315_WRITE    0x10
     35 
     36 /* AM2315 Commands */
     37 #define AM2315_HUMIDITY 0x00
     38 #define AM2315_TEMP     0x02
     39 #define AM2315_MODEL    0x08
     40 #define AM2315_VERSION  0x0A
     41 #define AM2315_ID       0x0B
     42 #define AM2315_STATUS   0x0F
     43 #define AM2315_USER_A   0x10
     44 #define AM2315_USER_B   0x12
     45 
     46 #define AM2315_SAMPLE   2
     47 
     48 #define HIGH_PRIORITY   99
     49 
     50 namespace upm {
     51 
     52 /**
     53  * @brief AM2315 Temperature & Humidity Sensor library
     54  * @defgroup am2315 libupm-am2315
     55  * @ingroup adafruit i2c temp
     56  */
     57 
     58 /**
     59  * @library am2315
     60  * @sensor am2315
     61  * @comname AM2315 Temperature & Humidity Sensor
     62  * @type temp
     63  * @man adafruit
     64  * @web http://www.adafruit.com/products/1293
     65  * @con i2c
     66  *
     67  * @brief API for the AM2315 Temperature & Humidity Sensor
     68  *
     69  * AM2315 by Measurement Specialties
     70  * (http://www.aosong.com/asp_bin/Products/en/AM2315.pdf)
     71  * is a digital humidity sensor with temperature output.
     72  * RH reports between 0 and 100%, and the temperature range is
     73  * -40 to +125 degC.
     74  * The sampling period of this sensor is 2 seconds. Reads occurring
     75  * more often than that return cached data.
     76  *
     77  * @image html am2315.jpeg
     78  * @snippet am2315.cxx Interesting
     79  */
     80 class AM2315 {
     81     public:
     82         /**
     83          * Instantiates an AM2315 object
     84          *
     85          * @param bus Number of the used bus
     86          * @param devAddr Address of the used I2C device
     87          * @param mode AM2315 oversampling
     88          */
     89         AM2315 (int bus, int devAddr=AM2315_I2C_ADDRESS);
     90 
     91         /**
     92          * AM2315 object destructor; basically, it closes the I2C connection.
     93          */
     94         ~AM2315 ();
     95 
     96         /**
     97          * Gets the current measured humidity [RH]
     98          *
     99          * Data is updated every 2 seconds - accesses occurring more often than
    100          * that return cached data
    101          */
    102         float getHumidity(void);
    103 
    104         /**
    105          * Gets the humidity cell temperature [degC]
    106          *
    107          * Data is updated every 2 seconds - accesses occurring more often than
    108          * that return cached data
    109          */
    110         float getTemperature(void);
    111 
    112         /**
    113          * Gets the humidity cell temperature [degF]
    114          *
    115          * Data is updated every 2 seconds - accesses occurring more often than
    116          * that return cached data
    117          */
    118         float getTemperatureF(void);
    119 
    120         /**
    121          * Function intended to test the device and verify it
    122          * is operating correctly.
    123          *
    124          */
    125         int testSensor(void);
    126 
    127         /**
    128          * Writes a four-byte (32b) register
    129          *
    130          * Note: these access routines are not the normal accesses to an I2C
    131          * device. AM2315 contains a microcontroller that manages the
    132          * actual readings. These handlers then make requests over I2C using
    133          * a protocol defined by AM2315.
    134          *
    135          * @param reg Address of the register
    136          * @param ival 32b value
    137          */
    138         int i2cWriteReg_32(int reg, uint32_t ival);
    139 
    140         /**
    141          * Writes a two-byte (16b) register
    142          *
    143          * @param reg Address of the register
    144          * @param ival 16b value
    145          */
    146         int i2cWriteReg_16(int reg, uint16_t ival);
    147 
    148         /**
    149          * Writes a one-byte (8b) register
    150          *
    151          * @param reg Address of the register
    152          * @param ival 8b value
    153          */
    154         int i2cWriteReg_8(int reg, uint8_t ival);
    155 
    156         /**
    157          * Reads a four-byte register
    158          *
    159          * @param reg Address of the register
    160          */
    161         uint32_t i2cReadReg_32 (int reg);
    162 
    163         /**
    164          * Reads a two-byte register
    165          *
    166          * @param reg Address of the register
    167          */
    168         uint16_t i2cReadReg_16 (int reg);
    169 
    170         /**
    171          * Reads a one-byte register
    172          *
    173          * @param reg Address of the register
    174          */
    175         uint8_t i2cReadReg_8 (int reg);
    176 
    177     private:
    178 
    179         char* m_name;
    180 
    181         int m_controlAddr;
    182         int m_bus;
    183         mraa_i2c_context m_i2ControlCtx;
    184 
    185         void update_values(void);
    186         uint8_t i2cReadReg(int reg, uint8_t* data, int ilen);
    187         int i2cWriteReg(uint8_t reg, uint8_t* data, uint8_t ilen);
    188         uint16_t crc16(uint8_t* ptr, uint8_t len);
    189 
    190         int32_t   m_temperature;
    191         int32_t   m_humidity;
    192 
    193         uint16_t  m_model;
    194         uint16_t  m_version;
    195         uint32_t  m_id;
    196 
    197         time_t    m_last_time;
    198 
    199         int       m_base_priority;
    200         pthread_t this_thread;
    201 };
    202 
    203 }
    204