|
@@ -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
|
+}
|