Updated i2c interface to work correctly with mBed and Hexiwear

Dependents:   Hexi_Click_IRThermo_Example

Fork of MLX90614 by Hikaru Sugiura

Revision:
5:8f8aedd25609
Parent:
2:01d333d06727
--- a/mlx90614.cpp	Thu Jun 09 13:50:26 2011 +0000
+++ b/mlx90614.cpp	Thu Oct 27 20:06:15 2016 +0000
@@ -1,10 +1,16 @@
- 
+//Melexis Infrared Thermometer MLX90614 Library
+
+//*****************************************************************
+//  Build : 27/10/16 Dave Clarke
+//          Only read thermo data.
+//
+//  This program does not check CRC.
+//  If you want to check CRC, please do it your self :)
+//****************************************************************// 
+
 #include "mlx90614.h"
 
-
-
-
-MLX90614::MLX90614(I2C* i2c,int addr){
+MLX90614::MLX90614(I2C* i2c, uint8_t addr){
 
     this->i2caddress = addr;
     this->i2c = i2c; 
@@ -14,35 +20,24 @@
 
 bool MLX90614::getTemp(float* temp_val){
 
-    char p1,p2,p3;
-    float temp_thermo;
-    bool ch;
+    bool ch;                    // Check flag for ack
+    char data[3];               // Raw data storage
+    char ram[1] = {0x07};       // RAM address what temperature is stored
+    char readaddr = 0xB5;       // Read Data address
+    uint16_t  temp_thermo;
+       
+    ch = i2c->write(i2caddress, ram, 1, true); // ping i2c with address and RAM location
+    if (ch) return false;                      // check for ack
+    
+    ch = i2c->read(readaddr, data , 3); // read raw temperature value
+    if (ch) return false;               // check for ack
 
-    i2c->stop();                            //stop i2c if not ack
-    wait(0.01);
-    i2c->start();                           //start I2C                   
-    ch=i2c->write(i2caddress);              //device address with write condition
-    
-    if(!ch)return false;                    //No Ack, return False
-    
-    ch=i2c->write(0x07);                    //device ram address where Tobj value is present
+    temp_thermo |=  data[1] << 8;   // make MSB
+    temp_thermo |=  data[0];        // make LSB
 
-    if(!ch)return false;                    //No Ack, return False
+    *temp_val = ((float)temp_thermo * 0.02) - 273;  //Convert kelvin to degree Celsius
+   
+    return true;                            //load data successfully, return true 
+}
 
 
-    i2c->start();                           //repeat start
-    ch=i2c->write(i2caddress|0x01);         //device address with read condition 
-    if(!ch)return false;                    //No Ack, return False
-
-    p1=i2c->read(1);     //Tobj low byte
-    p2=i2c->read(1);     //Tobj heigh byte
-    p3=i2c->read(0);     //PEC
-    
-    i2c->stop();                            //stop condition
-     
-    
-    temp_thermo=((((p2&0x007f)<<8)+p1)*0.02)-0.01;      //degree centigrate conversion
-    *temp_val=temp_thermo-273;                          //Convert kelvin to degree Celsius
-    
-    return true;                            //load data successfully, return true 
-}
\ No newline at end of file