Victor Karthik / Mbed 2 deprecated STM32Disco_Melexis

Dependencies:   mbed

main.cpp

Committer:
vctkarthik
Date:
2020-12-03
Revision:
3:3023306be214
Parent:
2:5e957dcc94bc

File content as of revision 3:3023306be214:

#include "mbed.h"
//#include "Mlx90615.h"
// this is working one for Disco-F413Zh board as of Dec 3, 2020


/*  for the button press device should start reading, then it should stop the loop for button press  */

AnalogIn analog_value(A0);
InterruptIn  button(PA_0);
DigitalOut myled(LED1);
bool workState= false;


I2C i2c(PB_11,PB_10);   //sda,scl
//SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd"); //// mosi, miso, sclk, cs, the pinout on the mbed Cool Components workshop board  stm32F4

//////////////MLX90615////////////////////////////////////////////////////

const int dev_add_W                  = 0xB6 ; // address with write(0) bit at last////Address must be bit shifted left already shifted,
const int  reg_To_address            = 0x27 ;//address of temperature of the object pointed
const int  reg_Ta_address            = 0x26 ;//address of temperature of the object pointed
//const int  reg_emis_address          = 0x13 ;//address of the emissivity register
//const int  emis_human                 = 16384 ;/// emis_val=0.98*16384+.49;
/*

uint8_t crc8(uint8_t *addr, uint8_t len)
{
  uint8_t crc = 0;
  while (len--) {
    uint8_t inbyte = *addr++;
    int i;
    for (i = 8; i; i--)
    {
      uint8_t carry = (crc ^ inbyte) & 0x80;
      crc <<= 1;
      if (carry)
        crc ^= 0x7;
      inbyte <<= 1;
    }
  }
  return crc;
}  */

class Mlx90615 {
public:
 /*   Mlx90615():  {  // _pin(pin) means pass pin to the DigitalOut constructor
                                               // default the output to 0
    }*/

   float readTemp(int regAddress)
    {
       char cmd[2];
       char reg[1];
       int ack=1;
       reg[0] = regAddress;

    /// write to the  address of the device
       i2c.write(dev_add_W);
       i2c.read(ack);
       // write to the reg address
       i2c.write(reg_To_address);
       i2c.read(ack);

     //  write (int address, const char *data, int length, bool repeated=false)
       i2c.write(dev_add_W,reg,1,true ); // write with repeated start
        i2c.read(ack);

     //   read (int address, char *data, int length, bool repeated=false)
       i2c.read(dev_add_W, cmd,2,false);
       int temp = (cmd[1]<<8)|cmd[0]; // raw temp value
       float tempC = (0.02*temp) -273.15; // temp in Celcius
       float tempF= (tempC*9/5) +31;

       return tempF;


        }


//private:;

};


/*float mlx90615_readTemp(int regAddress)
{
       char cmd[2];
       char reg[1];
       int ack=1;
       reg[0] = regAddress;

    /// write to the  address of the device
       i2c.write(dev_add_W);
       i2c.read(ack);
       // write to the reg address
       i2c.write(reg_To_address);
       i2c.read(ack);

     //  write (int address, const char *data, int length, bool repeated=false)
       i2c.write(dev_add_W,reg,1,true ); // write with repeated start
        i2c.read(ack);

     //   read (int address, char *data, int length, bool repeated=false)
       i2c.read(dev_add_W, cmd,2,false);
       int temp = (cmd[1]<<8)|cmd[0]; // raw temp value
       float tempC = (0.02*temp) -273.15; // temp in Celcius
       float tempF= temp;//(tempC*9/5) +31;

       return tempF;


}*/
/*

void mlx90615_emisWrite(int emis_val)
{


    //uint8_t data_low = 0;
//    uint8_t data_high = 0;
//    int ack=1;
//
//
//
//    data_low=(uint8_t)(emis_val & 0x00ff);;
//    data_high= (uint8_t)((emis_val & 0xff00) >> 8);
//
//     uint8_t *emisData=0x00;
//    uint8_t emisData_A[4];// arrary to keep it in pointer
//    emisData_A[0]=dev_add_W;
//    emisData_A[1]=reg_emis_address;
//    emisData_A[2]=data_low;
//    emisData_A[3]=data_high;
//    emisData=emisData_A;


    char cmd[4] = { 0 };
    uint8_t crcbuf[4] = { 0 };



    crcbuf[0] = dev_add_W;       //write Address
    crcbuf[1] = cmd[0] = reg_emis_address;
    crcbuf[2] = cmd[1] = (uint8_t)(emis_val & 0xFF);
    crcbuf[3] = cmd[2] = (uint8_t)((emis_val) >> 8);
                cmd[3] = crc8(crcbuf, 4);

    i2c.write(dev_add_W, cmd, 4, true);
    wait(0.1);






   // /// write to the  address of the device
//   i2c.write(dev_add_W);
//   i2c.read(ack);
//   // write to the reg address
//   i2c.write(reg_To_address);
//   i2c.read(ack);
//
//   //write the low 8 bits
//   i2c.write(data_low);
//   i2c.read(ack);
//
//    //write to the high  bits
//   i2c.write(data_high);
//   i2c.read(ack);
//
//   //write pec
//   i2c.write(crc8(emisData,4));
//   i2c.read(ack);




}*/

//////////////MLX90615///////////////////////////////////////////////////////////////////////

void read_ecg(bool work)
{
    float meas_r;
    float meas_v;
    int temp;
    float tempC;
    int ack=1;
    float bodyTempF;
    float envTempF;
    Mlx90615 mlx;



    if(work){
           meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
           meas_v = meas_r * 3300; // Converts value in the 0V-3.3V range

            char cmd[2];
            char reg[1];
            reg[0] = 0x27;
      //     i2c.write(dev_add_W, 1, true);
        //   i2c.read(ack);

           bodyTempF = mlx.readTemp(reg_To_address);
           envTempF = mlx.readTemp(reg_Ta_address);

           i2c.write(dev_add_W);
           i2c.read(ack);
           i2c.write(reg_To_address);
           i2c.read(ack);

         //  write (int address, const char *data, int length, bool repeated=false)
           i2c.write(dev_add_W,reg,1,true );
            i2c.read(ack);

         //   read (int address, char *data, int length, bool repeated=false)
           i2c.read(dev_add_W, cmd,2,false);
           temp = (cmd[1]<<8)|cmd[0];
           tempC = (0.02*temp) -273.15;




          // Display values
           printf("measure = %f = %.0f mV\n", meas_r, meas_v);

           //temp =15143;
          printf("Temperature : %f \r\n",tempC);
          printf("Body Temperature : %f \r\n",bodyTempF);


          printf("Env Temperature : %f \r\n",envTempF);

        }




}


 // toggleing  the button ////////////// +


void toggle()
{
       myled = !myled;
       workState = !workState;




}



int main() {

    button.rise(&toggle) ;

    //mlx90615_emisWrite(emis_human);
     //mlx90615_emisWrite(emis_human);
           wait(0.5);
    while(1)
    {

      button.rise(&toggle) ;
      read_ecg(workState);

      wait(1);
        
     }



}