1 /* 2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha (at) 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 #include <stdexcept> 29 30 #include "mlx90614.h" 31 32 using namespace upm; 33 34 MLX90614::MLX90614 (int bus, int devAddr) : m_i2Ctx(bus) { 35 m_name = "MLX90614"; 36 37 m_i2cAddr = devAddr; 38 m_bus = bus; 39 40 mraa::Result ret = m_i2Ctx.address(m_i2cAddr); 41 if (ret != mraa::SUCCESS) { 42 throw std::invalid_argument(std::string(__FUNCTION__) + 43 ": address() failed"); 44 } 45 } 46 47 float 48 MLX90614::readObjectTempF(void) { 49 return (readTemperature(MLX90614_TOBJ1) * 9 / 5) + 32; 50 } 51 52 float 53 MLX90614::readAmbientTempF(void) { 54 return (readTemperature(MLX90614_TA) * 9 / 5) + 32; 55 } 56 57 float 58 MLX90614::readObjectTempC(void) { 59 return readTemperature(MLX90614_TOBJ1); 60 } 61 62 float 63 MLX90614::readAmbientTempC(void) { 64 return readTemperature(MLX90614_TA); 65 } 66 67 /* 68 * ************** 69 * private area 70 * ************** 71 */ 72 uint16_t 73 MLX90614::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) { 74 int readByte = 0; 75 76 m_i2Ctx.address(m_i2cAddr); 77 m_i2Ctx.writeByte(reg); 78 79 readByte = m_i2Ctx.read(buffer, len); 80 return readByte; 81 } 82 83 mraa::Result 84 MLX90614::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) { 85 mraa::Result error = mraa::SUCCESS; 86 87 error = m_i2Ctx.address(m_i2cAddr); 88 error = m_i2Ctx.write(buffer, len); 89 90 return error; 91 } 92 93 float 94 MLX90614::readTemperature (uint8_t address) { 95 uint8_t buffer[3]; 96 float temperature = 0; 97 98 /* Reading temperature from sensor. 99 Answer contained of 3 bytes (TEMP_LSB | TEMP_MSB | PEC) 100 */ 101 if (i2cReadReg_N(address, 3, buffer) > 2) { 102 temperature = buffer[0]; 103 temperature = buffer[1] << 8; 104 105 temperature *= .02; 106 temperature -= 273.15; 107 } 108 109 return temperature; 110 } 111