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

Files at this revision

API Documentation at this revision

Comitter:
shawe
Date:
Fri Feb 17 12:51:18 2017 +0000
Parent:
0:755bd47fd6be
Commit message:
refactored;

Changed in this revision

MLX90614.cpp Show annotated file Show diff for this revision Revisions of this file
MLX90614.h Show annotated file Show diff for this revision Revisions of this file
diff -r 755bd47fd6be -r b70477df5c75 MLX90614.cpp
--- 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
diff -r 755bd47fd6be -r b70477df5c75 MLX90614.h
--- a/MLX90614.h	Mon Sep 05 10:52:54 2016 +0000
+++ b/MLX90614.h	Fri Feb 17 12:51:18 2017 +0000
@@ -1,93 +1,28 @@
-/*
-    Copyright (c) 2016 Jens Strümper based on the work of Jonathan Jones. 
- 
-    Permission is hereby granted, free of charge, to any person obtaining a copy
-    of this software and associated documentation files (the "Software"), to deal
-    in the Software without restriction, including without limitation the rights
-    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-    copies of the Software, and to permit persons to whom the Software is
-    furnished to do so, subject to the following conditions:
- 
-    The above copyright notice and this permission notice shall be included in
-    all copies or substantial portions of the Software.
- 
-    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-    THE SOFTWARE.
-*/
-
 #ifndef MLX90614_H
 #define MLX90614_H
-
-#ifndef MBED_H
 #include "mbed.h"
-#endif
 
-namespace js {
-    
-/** Due to changes in mbed's I2C stack older implementation of the MLX9064 library do currently not work. 
- * This recent implementation wraps the example code of Jonathan Jones into a library.  
- *
- * The float read_temp(int select) memeber function will take one argument:
- *  <ul>
- *  <li> "0" to return ambient temperature </li>
- *  <li> "1" to retunr object temperature </li> 
- *  </ul>
- *
- * Example:
- * @code
- * #include "mbed.h"
- * #include "MLX90614.h"
- *
- * I2C i2c(p16, p17); //I2C_SDA, I2C_SCL 
- * MLX90614 mlx90614(&i2c);
- *
- * float temp;
- * int select; // 0 = object temp, 1 = ambient temp
- *
- * int main(){
-    while(true){
-      temp=mlx90614.read_temp(1);
-      printf("%4.2f Celcius\r\n", temp);
-      wait(1);
-    }
-}
- * @endcode
- */
 
 class MLX90614 {
 
 public:
-
-    MLX90614(PinName sda, PinName scl);
-
     MLX90614(I2C *i2c);
-
    ~MLX90614();
 
-    float read_temp(int select);
+    float ambientTemp();
+    float objectTemp();
 
 protected:
 
-    float get_temp(uint8_t reg);
+    float readTemp(uint8_t reg);
 
 private:
 
     I2C *i2c_;
-
     static const int default_addr = 0x00;
     static const int T_ambient = 0x06;
     static const int T_obj1 = 0x07;
     static const int ram_access = 0x00;
 
 };
-
-} //namespace js
-
-using namespace js;
-
 #endif
\ No newline at end of file