|
@@ -0,0 +1,129 @@
|
|
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 <iostream>
|
|
26
|
+#include <unistd.h>
|
|
27
|
+#include <stdlib.h>
|
|
28
|
+
|
|
29
|
+#include "mlx90614.h"
|
|
30
|
+
|
|
31
|
+using namespace upm;
|
|
32
|
+
|
|
33
|
+struct MLX90614Exception : public std::exception {
|
|
34
|
+ std::string message;
|
|
35
|
+ MLX90614Exception (std::string msg) : message (msg) { }
|
|
36
|
+ ~MLX90614Exception () throw () { }
|
|
37
|
+ const char* what() const throw () { return message.c_str(); }
|
|
38
|
+};
|
|
39
|
+
|
|
40
|
+MLX90614::MLX90614 (int bus, int devAddr) {
|
|
41
|
+ m_name = "MLX90614";
|
|
42
|
+
|
|
43
|
+ m_i2cAddr = devAddr;
|
|
44
|
+ m_bus = bus;
|
|
45
|
+
|
|
46
|
+ m_i2Ctx = mraa_i2c_init(m_bus);
|
|
47
|
+
|
|
48
|
+ mraa_result_t ret = mraa_i2c_address(m_i2Ctx, m_i2cAddr);
|
|
49
|
+ if (ret != MRAA_SUCCESS) {
|
|
50
|
+ throw MLX90614Exception ("Couldn't initilize I2C.");
|
|
51
|
+ }
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+MLX90614::~MLX90614() {
|
|
55
|
+ mraa_i2c_stop(m_i2Ctx);
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+float
|
|
59
|
+MLX90614::readObjectTempF(void) {
|
|
60
|
+ return (readTemperature (MLX90614_TOBJ1) * 9 / 5) + 32;
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+float
|
|
64
|
+MLX90614::readAmbientTempF(void) {
|
|
65
|
+ return (readTemperature (MLX90614_TA) * 9 / 5) + 32;
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+float
|
|
69
|
+MLX90614::readObjectTempC(void) {
|
|
70
|
+ return readTemperature (MLX90614_TOBJ1);
|
|
71
|
+}
|
|
72
|
+
|
|
73
|
+float
|
|
74
|
+MLX90614::readAmbientTempC(void) {
|
|
75
|
+ return readTemperature (MLX90614_TA);
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+/*
|
|
79
|
+ * **************
|
|
80
|
+ * private area
|
|
81
|
+ * **************
|
|
82
|
+ */
|
|
83
|
+uint16_t
|
|
84
|
+MLX90614::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) {
|
|
85
|
+ int readByte = 0;
|
|
86
|
+
|
|
87
|
+ if (m_i2Ctx == NULL) {
|
|
88
|
+ throw MLX90614Exception ("Couldn't find initilized I2C.");
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ mraa_i2c_address(m_i2Ctx, m_i2cAddr);
|
|
92
|
+ mraa_i2c_write_byte(m_i2Ctx, reg);
|
|
93
|
+
|
|
94
|
+ readByte = mraa_i2c_read(m_i2Ctx, buffer, len);
|
|
95
|
+ return readByte;
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+mraa_result_t
|
|
99
|
+MLX90614::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) {
|
|
100
|
+ mraa_result_t error = MRAA_SUCCESS;
|
|
101
|
+
|
|
102
|
+ if (m_i2Ctx == NULL) {
|
|
103
|
+ throw MLX90614Exception ("Couldn't find initilized I2C.");
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ error = mraa_i2c_address (m_i2Ctx, m_i2cAddr);
|
|
107
|
+ error = mraa_i2c_write (m_i2Ctx, buffer, len);
|
|
108
|
+
|
|
109
|
+ return error;
|
|
110
|
+}
|
|
111
|
+
|
|
112
|
+float
|
|
113
|
+MLX90614::readTemperature (uint8_t address) {
|
|
114
|
+ uint8_t buffer[3];
|
|
115
|
+ float temperature = 0;
|
|
116
|
+
|
|
117
|
+ /* Reading temperature from sensor.
|
|
118
|
+ Answer contained of 3 bytes (TEMP_LSB | TEMP_MSB | PEC)
|
|
119
|
+ */
|
|
120
|
+ if (i2cReadReg_N (address, 3, buffer) > 2) {
|
|
121
|
+ temperature = buffer[0];
|
|
122
|
+ temperature = buffer[1] << 8;
|
|
123
|
+
|
|
124
|
+ temperature *= .02;
|
|
125
|
+ temperature -= 273.15;
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ return temperature;
|
|
129
|
+}
|