Recent implementation of a MLX90614 IR temperature sensor library based on the example code of Jonathan Jones. Working with the current I2C mbed library.

Dependents:   D7_MLX_AND_BAT

Fork of MLX90614 by Jens Strümper

Revision:
1:b70477df5c75
Parent:
0:755bd47fd6be
--- a/MLX90614.cpp	Mon Sep 05 10:52:54 2016 +0000
+++ b/MLX90614.cpp	Fri Feb 17 12:51:18 2017 +0000
@@ -1,35 +1,21 @@
 #include "MLX90614.h"
 
-MLX90614::MLX90614(PinName sda, PinName scl)
-{
-    i2c_ = new I2C(sda, scl);
-    i2c_->frequency(400000);
-}
-
 MLX90614::MLX90614(I2C *i2c) : i2c_(i2c) {
-    }
-
-MLX90614::~MLX90614() {
-    delete i2c_;
-    }
-
-float MLX90614::get_temp(uint8_t reg) {
-    char cmd[3] = { 0 };
-    // read the temperature data (kelvin)
-    cmd[0] = ram_access | reg;
-    i2c_->write(default_addr,cmd,1,true); i2c_->read(default_addr,cmd,3);
-    // convert to meaningful units, still in kelvin - just normalized
-    return 0.02 * static_cast<float>((cmd[1]<<8)|cmd[0]);
 }
 
-float MLX90614::read_temp(int select) {
-    uint8_t reg_addrs[] = { T_ambient, T_obj1 };
-    float tt = 0.0;
-    if (select == 0){
-        tt = get_temp(reg_addrs[0])-273.15;
-        }
-    if (select == 1){
-        tt = get_temp(reg_addrs[1])-273.15;
-        }
-    return tt;
-    }
+float MLX90614::readTemp(uint8_t reg) {
+    char cmd[3] = { 0 };
+    cmd[0] = ram_access | reg;
+    i2c_->write(default_addr, cmd, 1, true); 
+    i2c_->read(default_addr, cmd, 3);
+    return 0.02f * static_cast<float>(((cmd[1] & 0x7F) << 8) | cmd[0]) - 273.15f;
+}
+
+
+float MLX90614::ambientTemp(){
+    return readTemp(T_ambient);
+}
+
+float MLX90614::objectTemp(){
+    return readTemp(T_obj1);
+}
\ No newline at end of file