upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
am2315.h
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.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 
80 class AM2315 {
81  public:
89  AM2315 (int bus, int devAddr=AM2315_I2C_ADDRESS);
90 
94  ~AM2315 ();
95 
101  float getHumidity(void);
102 
108  float getTemperature(void);
109 
115  float getTemperatureF(void);
116 
122  int testSensor(void);
123 
135  int i2cWriteReg_32(int reg, uint32_t ival);
136 
143  int i2cWriteReg_16(int reg, uint16_t ival);
144 
151  int i2cWriteReg_8(int reg, uint8_t ival);
152 
158  uint32_t i2cReadReg_32 (int reg);
159 
165  uint16_t i2cReadReg_16 (int reg);
166 
172  uint8_t i2cReadReg_8 (int reg);
173 
174  private:
175 
176  char* m_name;
177 
178  int m_controlAddr;
179  int m_bus;
180  mraa_i2c_context m_i2ControlCtx;
181 
182  void update_values(void);
183  uint8_t i2cReadReg(int reg, uint8_t* data, int ilen);
184  int i2cWriteReg(uint8_t reg, uint8_t* data, uint8_t ilen);
185  uint16_t crc16(uint8_t* ptr, uint8_t len);
186 
187  int32_t m_temperature;
188  int32_t m_humidity;
189 
190  uint16_t m_model;
191  uint16_t m_version;
192  uint32_t m_id;
193 
194  time_t m_last_time;
195 
196  int m_base_priority;
197  pthread_t this_thread;
198 };
199 
200 }
int i2cWriteReg_32(int reg, uint32_t ival)
Definition: am2315.cpp:264
AM2315(int bus, int devAddr=AM2315_I2C_ADDRESS)
Definition: am2315.cpp:37
~AM2315()
Definition: am2315.cpp:63
float getHumidity(void)
Definition: am2315.cpp:98
Definition: a110x.h:29
C++ API for AM2315 chip (Temperature & Humidity Sensor)
Definition: am2315.h:80
uint8_t i2cReadReg_8(int reg)
Definition: am2315.cpp:305
float getTemperatureF(void)
Definition: am2315.cpp:92
int testSensor(void)
Definition: am2315.cpp:111
float getTemperature(void)
Definition: am2315.cpp:85
int i2cWriteReg_8(int reg, uint8_t ival)
Definition: am2315.cpp:282
uint32_t i2cReadReg_32(int reg)
Definition: am2315.cpp:290
uint16_t i2cReadReg_16(int reg)
Definition: am2315.cpp:298
int i2cWriteReg_16(int reg, uint16_t ival)
Definition: am2315.cpp:274