1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 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 #pragma once
25 
26 #include <string>
27 #include <mraa/aio.h>
28 
29 namespace upm {
30 
31   /**
32    * @brief OTP538U IR Temperature Sensor library
33    * @defgroup otp538u libupm-otp538u
34    * @ingroup seeed analog light hak
35    */
36   /**
37    * @library otp538u
38    * @sensor otp538u
39    * @comname OTP538U IR Temperature Sensor
40    * @altname Grove IR Temperature Sensor
41    * @type light
42    * @man generic
43    * @con analog
44    * @kit hak
45    *
46    * @brief API for the OTP538U IR Temperature Sensor
47    *
48    * UPM module for the OTP538U IR temperature sensor
49    *
50    * This module was tested with the Grove IR non-contact temperature
51    * sensor.
52    *
53    * The sensor provides 2 analog outputs: one for the thermistor
54    * that measures the ambient temperature, and the other for the thermopile
55    * that measures the object temperature.
56    *
57    * Much of the code depends on analyzing Seeed Studio* examples
58    * and the circuit design. As a result, there are several 'magic'
59    * numbers derived from their circuit design. These values are used
60    * by default.
61    *
62    * The tables used came from the "538U VT
63    * Table__20_200(v1.3).pdf" and "538RT_table.pdf" datasheets.
64    *
65    * These tables assume the object to be measured is 9 cm (3.54
66    * inches) from the sensor.
67    *
68    * @image html otp538u.jpg
69    * @snippet otp538u.cxx Interesting
70    */
71   class OTP538U {
72   public:
73     /**
74      * OTP538U constructor
75      *
76      * @param pinA Analog pin to use for the ambient temperature
77      * @param pinO Analog pin to use for the object temperature
78      * @param aref Analog reference voltage; default is 5.0 V
79      */
80     OTP538U(int pinA, int pinO, float aref = 5.0);
81 
82     /**
83      * OTP538U destructor
84      */
85     ~OTP538U();
86 
87     /**
88      * Gets the ambient temperature in Celsius
89      *
90      * @return Ambient temperature
91      */
92     float ambientTemperature();
93 
94     /**
95      * Gets the object temperature in Celsius
96      *
97      * @return Object temperature
98      */
99     float objectTemperature();
100 
101     /**
102      * Sets the offset voltage
103      *
104      * The Seeed Studio wiki gives an example of calibrating the sensor
105      * and calculating the offset voltage to apply. Currently, the
106      * default value is set, but you can use the function to set one
107      * of your own.
108      *
109      * @param vOffset Desired offset voltage
110      */
setVoltageOffset(float vOffset)111     void setVoltageOffset(float vOffset) { m_offsetVoltage = vOffset; };
112 
113     /**
114      * Sets the output resistance value
115      *
116      * The Seeed Studio wiki example uses a value of 2,000,000 in one of
117      * the equations used to calculate voltage. The value is the
118      * resistance of a resistor they use in the output stage of their
119      * SIG2 output. This was 'decoded' by looking at the EAGLE* files
120      * containing their schematics for this device.
121      *
122      * @param outResistance Value of the output resistor; default is 2M Ohm
123      */
setOutputResistence(int outResistance)124     void setOutputResistence(int outResistance) {
125       m_vResistance = outResistance; };
126 
127     /**
128      * Sets the reference voltage of the internal Seeed Studio voltage
129      * regulator on the sensor board.
130      *
131      * The Seeed Studio wiki example uses a value of 2.5 in one of the
132      * equations used to calculate the resistance of the ambient
133      * thermistor. The value is the voltage of an internal voltage
134      * regulator used on the sensor board. This was 'decoded' by
135      * looking at the EAGLE files containing their schematics for this
136      * device.
137      *
138      * @param vref Reference voltage of the internal sensor; default is 2.5 V
139      */
setVRef(float vref)140     void setVRef(float vref) { m_vref = vref; };
141 
142 
143   private:
144     float m_vref;
145     float m_aref;
146     int m_vResistance;
147     float m_offsetVoltage;
148     int m_adcResolution;
149     mraa_aio_context m_aioA;
150     mraa_aio_context m_aioO;
151   };
152 }
153 
154 
155