//*****************************************************************************
//
// TEMP.c - Knihovna pro ovladani snimace teploty
//
//*****************************************************************************


#include <common.h>

#include <I2Cdriver.h>
#include <TEMPdriver.h>

//Promenne------------------
unsigned char data[2];


//Inicializace snimace teploty / presnost, ukazatel nastaven na teplotu---------
void TEMPInit(void)
{
  
  //nastavit presnost
    data[0]=POINTER_TO_CONF;                          //budu zapisovat do konfig.registru
    data[1]=FACTORY_SETTINGS | RESOLUTION_11B;        //zmenim pouze rozliseni na 11bitu
    I2C_Write(ADDRESS_TEMP_SENS, &data[0], 2);      //zapisu nastaveni
  
  //nastavit pointer na teplotu
    data[0]=POINTER_TO_TEMP;                          //dale se bude jen merit teplota
    I2C_Write(ADDRESS_TEMP_SENS, &data[0], 1);      //nastavim tedy pointer na registr s teplotou
    SysCtlDelay(10000);
} 

//Vycteni hodnoty ze snimace a jeji prevod--------------------------------------
float TEMPGetValue()
{  
    float result;
    
    I2C_Read(POINTER_TO_TEMP, &data[0], (TEMP_SENS | 0x02));       //ze snimace teploty budu cist 2Byty z adresy udane ukazatelem
    //result=(float)(data[0]&0x7f);
    //result+=(float)((data[1]>>4)*0.0625);
    result=(float)((data[0]&0x7f) + (data[1]>>4)*0.0625);
    if(data[0]&0x80)
      result*=-1.0;
    
    return result;
}
