summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/th02/th02.cxx
blob: 5e79e0688d4bd0ad439f4117e34422bedf7d051e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
 * Contributions: Jon Trulson <jtlulson@ics.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * Credits to Seeed Studeo.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdexcept>

#include "th02.hpp"

using namespace std;
using namespace upm;

TH02::TH02 (int bus, uint8_t addr) : m_i2c(bus) {
    m_addr = addr;
    m_name = "TH02";

    mraa::Result ret = m_i2c.address(m_addr);
    if (ret != mraa::SUCCESS) {
        throw std::invalid_argument(std::string(__FUNCTION__) + 
                                    ": mraa_i2c_address() failed");
    }
}

TH02::~TH02 () {
}

float
TH02::getTemperature () {
    uint16_t temperature = 0;

    /* Start a new temperature conversion */
    if (m_i2c.writeReg(TH02_REG_CONFIG, TH02_CMD_MEASURE_TEMP)) {
        throw std::runtime_error(std::string(__FUNCTION__) +
                                 ": I2c.writeReg() failed");
        return 0.0;
    }

    /* Wait until conversion is done */
    while (getStatus() == false);

    temperature = m_i2c.readReg(TH02_REG_DATA_H) << 8;
    temperature |= m_i2c.readReg(TH02_REG_DATA_L);
    temperature >>= 2;

    return ((float(temperature) / 32.0) - 50.0);
}

float
TH02::getHumidity () {
    uint16_t humidity = 0;

    /* Start a new humidity conversion */
    if (m_i2c.writeReg(TH02_REG_CONFIG, TH02_CMD_MEASURE_HUMI)) {
        throw std::runtime_error(std::string(__FUNCTION__) +
                                 ": I2c.writeReg() failed");
        return 0.0;
    }

    /* Wait until conversion is done */
    while (getStatus() == false);

    humidity = m_i2c.readReg(TH02_REG_DATA_H) << 8;
    humidity |= m_i2c.readReg(TH02_REG_DATA_L);
    humidity >>= 4;

    return ((float(humidity) / 16.0) - 24.0);
}

bool
TH02::getStatus () {
    uint8_t status = m_i2c.readReg(TH02_REG_STATUS);

    if (status & TH02_STATUS_RDY_MASK)
        return false;           // NOT ready
    else
        return true;            // ready
}