atMETEO
An ATmega based weather station
mlx90614.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
36 #include "lib/utils.h"
37 
38 #include "lib/i2c.h"
39 
40 namespace Avr
41 {
42 
53 class Mlx90614
54 {
55 public:
62  bool isValid() const { return m_valid; }
63 
71  float ambientTemperature() const {
72  return m_ambientTemperature;
73  }
74 
82  float objectTemperature() const {
83  return m_objectTemperature;
84  }
85 
92  bool read()
93  {
94  bool status;
95  m_valid = true;
96 
97  m_ambientTemperature = read(c_ambientTemperatureAddress, &status)
98  * c_resolution - c_zeroCinK;
99  if (!status)
100  m_valid = false;
101 
102  m_objectTemperature = read(c_objectTemperatureAddress, &status)
103  * c_resolution - c_zeroCinK;
104  if (!status)
105  m_valid = false;
106 
107  return m_valid;
108  }
109 
110 private:
111  static constexpr float c_resolution = 0.02F;
112  static constexpr float c_zeroCinK = 273.15F;
113  static const uint8_t c_deviceAddress = 0x5A;
114  static const uint8_t c_ambientTemperatureAddress = 0x06;
115  static const uint8_t c_objectTemperatureAddress = 0x07;
116 
117  bool m_valid = false;
118  float m_ambientTemperature = 0.0F;
119  float m_objectTemperature = 0.0F;
120 
121  uint16_t read(uint8_t reg, bool *status)
122  {
123  auto i2c = Avr::I2c::instance();
124  i2c.beginTransmission(c_deviceAddress);
125  i2c.write(reg);
126  i2c.endTransmission(false);
127 
128  uint8_t bytesRead = i2c.requestFrom(c_deviceAddress, 3);
129  uint16_t value = 0;
130  if (bytesRead == 3) {
131  uint8_t lsb = i2c.read();
132  uint8_t msb = i2c.read();
133 
134  // Bit 7 set indicates error
135  if (!Sensors::bitRead(msb, 7)) {
136  value = Sensors::word(msb & 0x7F, lsb);
137  if (status) *status = true;
138  } else {
139  if (status) *status = false;
140  }
141  } else {
142  if (status) *status = false;
143  }
144  return value;
145  }
146 };
147  // \addtogroup libtarget_mlx90614
149 
150 } // namespace Avr
float objectTemperature() const
Retrieves the cached object temperature value.
Definition: mlx90614.h:82
bool read()
Reads the ambient and object temperature from the sensor and updates the cached values.
Definition: mlx90614.h:92
static I2c & instance()
Returns the I2c::I2c instance.
Definition: i2c.h:86
bool isValid() const
Determines if the last sensor access was valid.
Definition: mlx90614.h:62
float ambientTemperature() const
Retrieves the cached ambient temperature value.
Definition: mlx90614.h:71
Bit manipulation utilities.
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
bool bitRead(T &value, uint8_t bit)
Returns the bit in the given value.
Definition: utils.h:92
Decodes data from Melexis MLX90614 Infrared thermometer sensors.
Definition: mlx90614.h:53
uint16_t word(uint8_t highByte, uint8_t lowByte)
Converts two bytes to a word.
Definition: utils.h:162
Wrapper for accessing built-in I2C (TWI) communication interfaces.