Browse Source

th02:: added new humidity&temperature sensor

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
ee2d398bfa
7 changed files with 352 additions and 0 deletions
  1. 3
    0
      examples/CMakeLists.txt
  2. 64
    0
      examples/th02-example.cxx
  3. 5
    0
      src/th02/CMakeLists.txt
  4. 8
    0
      src/th02/jsupm_th02.i
  5. 9
    0
      src/th02/pyupm_th02.i
  6. 160
    0
      src/th02/th02.cxx
  7. 103
    0
      src/th02/th02.h

+ 3
- 0
examples/CMakeLists.txt View File

@@ -34,6 +34,7 @@ add_executable (mq3-example mq3-example.cxx)
34 34
 add_executable (mq5-example mq5-example.cxx)
35 35
 add_executable (mq9-example mq9-example.cxx)
36 36
 add_executable (tcs3414cs-example tcs3414cs-example.cxx)
37
+add_executable (th02-example th02-example.cxx)
37 38
 
38 39
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
39 40
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -62,6 +63,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614)
62 63
 include_directories (${PROJECT_SOURCE_DIR}/src/ecs1030)
63 64
 include_directories (${PROJECT_SOURCE_DIR}/src/gas)
64 65
 include_directories (${PROJECT_SOURCE_DIR}/src/tcs3414cs)
66
+include_directories (${PROJECT_SOURCE_DIR}/src/th02)
65 67
 
66 68
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
67 69
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -99,3 +101,4 @@ target_link_libraries (mq3-example gas ${CMAKE_THREAD_LIBS_INIT})
99 101
 target_link_libraries (mq5-example gas ${CMAKE_THREAD_LIBS_INIT})
100 102
 target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT})
101 103
 target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT})
104
+target_link_libraries (th02-example th02 ${CMAKE_THREAD_LIBS_INIT})

+ 64
- 0
examples/th02-example.cxx View File

@@ -0,0 +1,64 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include "th02.h"
28
+#include <signal.h>
29
+
30
+int doWork = 0;
31
+upm::TH02 *sensor = NULL;
32
+
33
+void
34
+sig_handler(int signo)
35
+{
36
+    printf("got signal\n");
37
+    if (signo == SIGINT) {
38
+        printf("exiting application\n");
39
+        doWork = 1;
40
+    }
41
+}
42
+
43
+int
44
+main(int argc, char **argv)
45
+{
46
+    //! [Interesting]
47
+    float temperature = 0.0;
48
+    float humidity = 0.0;
49
+    sensor = new upm::TH02 ();
50
+
51
+    while (!doWork) {
52
+        temperature = sensor->getTemperature ();
53
+        // humidity = sensor->getHumidity ();
54
+        std::cout << "Temperature = " << temperature << ", Humidity = " << humidity << std::endl;
55
+        usleep (500000);
56
+    }
57
+    //! [Interesting]
58
+
59
+    std::cout << "exiting application" << std::endl;
60
+
61
+    delete sensor;
62
+
63
+    return 0;
64
+}

+ 5
- 0
src/th02/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "th02")
2
+set (libdescription "Temperature and Humidity Sensor Pro")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/th02/jsupm_th02.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_th02
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "th02.h"
6
+%}
7
+
8
+%include "th02.h"

+ 9
- 0
src/th02/pyupm_th02.i View File

@@ -0,0 +1,9 @@
1
+%module pyupm_th02
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "th02.h"
7
+%{
8
+    #include "th02.h"
9
+%}

+ 160
- 0
src/th02/th02.cxx View File

@@ -0,0 +1,160 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Credits to Seeed Studeo.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+#include <iostream>
28
+#include <unistd.h>
29
+#include <stdlib.h>
30
+
31
+#include "th02.h"
32
+
33
+using namespace upm;
34
+
35
+struct TH02Exception : public std::exception {
36
+    std::string message;
37
+    TH02Exception (std::string msg) : message (msg) { }
38
+    ~TH02Exception () throw () { }
39
+    const char* what() const throw () { return message.c_str(); }
40
+};
41
+
42
+TH02::TH02 () {
43
+    m_name = "TH02";
44
+    m_i2Ctx = mraa_i2c_init(0);
45
+
46
+    mraa_result_t ret = mraa_i2c_address(m_i2Ctx, ADDR);
47
+    if (ret != MRAA_SUCCESS) {
48
+        throw TH02Exception ("Couldn't initilize I2C.");
49
+    }
50
+}
51
+
52
+TH02::~TH02 () {
53
+    mraa_i2c_stop(m_i2Ctx);
54
+}
55
+
56
+float
57
+TH02::getTemperature () {
58
+    uint8_t buffer[3];
59
+    uint16_t temperature = 0;
60
+
61
+    /* Start a new temperature conversion */
62
+    i2cWriteReg (REG_CONFIG, CMD_MEASURE_TEMP);
63
+
64
+    /* Wait until conversion is done */
65
+    while (getStatus() == false);
66
+
67
+    if (i2cReadReg_N (REG_DATA_H, 3, buffer) > 2) {
68
+        temperature = (buffer[1] << 8) | buffer[2];
69
+        temperature >>= 2;
70
+
71
+        return (temperature / 32.0) - 50.0;
72
+    }
73
+
74
+    return 0.0;
75
+}
76
+
77
+float
78
+TH02::getHumidity () {
79
+    uint8_t buffer[3];
80
+    uint16_t humidity = 0;
81
+
82
+    /* Start a new humility conversion */
83
+    i2cWriteReg (REG_CONFIG, CMD_MEASURE_HUMI);
84
+
85
+    /* Wait until conversion is done */
86
+    while (getStatus() == false);
87
+
88
+    if (i2cReadReg_N (REG_DATA_H, 3, buffer) > 2) {
89
+        humidity = (buffer[1] << 8) | buffer[2];
90
+        humidity >>= 4;
91
+
92
+        return (humidity / 16.0) - 24.0;
93
+    }
94
+
95
+    return 0.0;
96
+}
97
+
98
+bool
99
+TH02::getStatus () {
100
+    uint8_t buffer[1];
101
+
102
+    if (i2cReadReg_N (REG_STATUS, 1, buffer) > 0) {
103
+        if (buffer[0] & STATUS_RDY_MASK) {
104
+            return true;    // ready
105
+        }
106
+    }
107
+
108
+    return false;
109
+}
110
+
111
+/*
112
+ * **************
113
+ *  private area
114
+ * **************
115
+ */
116
+uint16_t
117
+TH02::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) {
118
+    int readByte = 0;
119
+
120
+    if (m_i2Ctx == NULL) {
121
+        throw TH02Exception ("Couldn't find initilized I2C.");
122
+    }
123
+
124
+    mraa_i2c_address(m_i2Ctx, ADDR);
125
+    mraa_i2c_write_byte(m_i2Ctx, reg);
126
+
127
+    mraa_i2c_address(m_i2Ctx, ADDR);
128
+    readByte = mraa_i2c_read(m_i2Ctx, buffer, len);
129
+    return readByte;
130
+}
131
+
132
+mraa_result_t
133
+TH02::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) {
134
+    mraa_result_t error = MRAA_SUCCESS;
135
+
136
+    if (m_i2Ctx == NULL) {
137
+        throw TH02Exception ("Couldn't find initilized I2C.");
138
+    }
139
+
140
+    error = mraa_i2c_address (m_i2Ctx, ADDR);
141
+    error = mraa_i2c_write_byte (m_i2Ctx, reg);
142
+    error = mraa_i2c_write (m_i2Ctx, buffer, len);
143
+
144
+    return error;
145
+}
146
+
147
+mraa_result_t
148
+TH02::i2cWriteReg (uint8_t reg, uint8_t data) {
149
+    mraa_result_t error = MRAA_SUCCESS;
150
+
151
+    if (m_i2Ctx == NULL) {
152
+        throw TH02Exception ("Couldn't find initilized I2C.");
153
+    }
154
+
155
+    error = mraa_i2c_address (m_i2Ctx, ADDR);
156
+    error = mraa_i2c_write_byte (m_i2Ctx, reg);
157
+    error = mraa_i2c_write_byte (m_i2Ctx, data);
158
+
159
+    return error;
160
+}

+ 103
- 0
src/th02/th02.h View File

@@ -0,0 +1,103 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Credits to Seeed Studeo.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining
8
+ * a copy of this software and associated documentation files (the
9
+ * "Software"), to deal in the Software without restriction, including
10
+ * without limitation the rights to use, copy, modify, merge, publish,
11
+ * distribute, sublicense, and/or sell copies of the Software, and to
12
+ * permit persons to whom the Software is furnished to do so, subject to
13
+ * the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be
16
+ * included in all copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+#pragma once
27
+
28
+#include <string>
29
+#include <mraa/i2c.h>
30
+
31
+#define ADDR                0x40 // device address
32
+
33
+#define REG_STATUS          0x00
34
+#define REG_DATA_H          0x01
35
+#define REG_DATA_L          0x02
36
+#define REG_CONFIG          0x03
37
+#define REG_ID              0x11
38
+
39
+#define STATUS_RDY_MASK     0x01
40
+
41
+#define CMD_MEASURE_HUMI    0x01
42
+#define CMD_MEASURE_TEMP    0x11
43
+
44
+#define TH02_WR_REG_MODE    0xC0
45
+#define TH02_RD_REG_MODE    0x80
46
+
47
+#define HIGH                1
48
+#define LOW                 0
49
+
50
+namespace upm {
51
+
52
+/**
53
+ * @brief C++ API for TH02 chip (Temperature and Humidity Sensor Pro)
54
+ *
55
+ * This file defines the TH02 C++ interface for libth02
56
+ *
57
+ * @snippet th02-example.cxx Interesting
58
+ *
59
+ */
60
+class TH02 {
61
+    public:
62
+        /**
63
+         * Instanciates a TH02 object
64
+         */
65
+        TH02 ();
66
+
67
+        /**
68
+         * TH02 object destructor, basicaly it close i2c connection.
69
+         */
70
+        ~TH02 ();
71
+
72
+        /**
73
+         * Get the temperature value from sensor.
74
+         */
75
+        float getTemperature ();
76
+
77
+        /**
78
+         * Get the humidity value from sensor.
79
+         */
80
+        float getHumidity ();
81
+
82
+        /**
83
+         * Get the sensor's status.
84
+         */
85
+        bool getStatus ();
86
+
87
+        /**
88
+         * Return name of the component
89
+         */
90
+        std::string name()
91
+        {
92
+            return m_name;
93
+        }
94
+    private:
95
+        std::string m_name;
96
+        mraa_i2c_context m_i2Ctx;
97
+
98
+        uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
99
+        mraa_result_t i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer);
100
+        mraa_result_t i2cWriteReg (uint8_t reg, uint8_t data);
101
+};
102
+
103
+}