1 /* 2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha (at) intel.com> 3 * Contributions: Jon Trulson <jtlulson (at) ics.com> 4 * Copyright (c) 2014 Intel Corporation. 5 * 6 * Credits to Seeed Studeo. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining 9 * a copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sublicense, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27 28 #include <iostream> 29 #include <unistd.h> 30 #include <stdlib.h> 31 #include <stdexcept> 32 33 #include "th02.h" 34 35 using namespace std; 36 using namespace upm; 37 38 TH02::TH02 (int bus, uint8_t addr) : m_i2c(bus) { 39 m_addr = addr; 40 m_name = "TH02"; 41 42 mraa::Result ret = m_i2c.address(m_addr); 43 if (ret != mraa::SUCCESS) { 44 throw std::invalid_argument(std::string(__FUNCTION__) + 45 ": mraa_i2c_address() failed"); 46 } 47 } 48 49 TH02::~TH02 () { 50 } 51 52 float 53 TH02::getTemperature () { 54 uint16_t temperature = 0; 55 56 /* Start a new temperature conversion */ 57 if (m_i2c.writeReg(TH02_REG_CONFIG, TH02_CMD_MEASURE_TEMP)) { 58 throw std::runtime_error(std::string(__FUNCTION__) + 59 ": I2c.writeReg() failed"); 60 return 0.0; 61 } 62 63 /* Wait until conversion is done */ 64 while (getStatus() == false); 65 66 temperature = m_i2c.readReg(TH02_REG_DATA_H) << 8; 67 temperature |= m_i2c.readReg(TH02_REG_DATA_L); 68 temperature >>= 2; 69 70 return ((float(temperature) / 32.0) - 50.0); 71 } 72 73 float 74 TH02::getHumidity () { 75 uint16_t humidity = 0; 76 77 /* Start a new humidity conversion */ 78 if (m_i2c.writeReg(TH02_REG_CONFIG, TH02_CMD_MEASURE_HUMI)) { 79 throw std::runtime_error(std::string(__FUNCTION__) + 80 ": I2c.writeReg() failed"); 81 return 0.0; 82 } 83 84 /* Wait until conversion is done */ 85 while (getStatus() == false); 86 87 humidity = m_i2c.readReg(TH02_REG_DATA_H) << 8; 88 humidity |= m_i2c.readReg(TH02_REG_DATA_L); 89 humidity >>= 4; 90 91 return ((float(humidity) / 16.0) - 24.0); 92 } 93 94 bool 95 TH02::getStatus () { 96 uint8_t status = m_i2c.readReg(TH02_REG_STATUS); 97 98 if (status & TH02_STATUS_RDY_MASK) 99 return false; // NOT ready 100 else 101 return true; // ready 102 } 103 104