Loguino 0.1
Standalone data logger for MegaSquirt and other devices.
Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes
DallasTemperature Class Reference

#include "DallasTemperature.h"

List of all members.

Public Types

typedef void AlarmHandler (uint8_t *)

Public Member Functions

 DallasTemperature (OneWire *)
void begin (void)
uint8_t getDeviceCount (void)
bool isConversionComplete (void)
bool validAddress (uint8_t *)
bool getAddress (uint8_t *, const uint8_t)
bool isConnected (uint8_t *)
bool isConnected (uint8_t *, uint8_t *)
void readScratchPad (uint8_t *, uint8_t *)
void writeScratchPad (uint8_t *, const uint8_t *)
bool readPowerSupply (uint8_t *)
uint8_t getResolution ()
void setResolution (uint8_t)
uint8_t getResolution (uint8_t *)
bool setResolution (uint8_t *, uint8_t)
void setWaitForConversion (bool)
bool getWaitForConversion (void)
void setCheckForConversion (bool)
bool getCheckForConversion (void)
void requestTemperatures (void)
bool requestTemperaturesByAddress (uint8_t *)
bool requestTemperaturesByIndex (uint8_t)
float getTempC (uint8_t *)
float getTempF (uint8_t *)
float getTempCByIndex (uint8_t)
float getTempFByIndex (uint8_t)
bool isParasitePowerMode (void)
bool isConversionAvailable (uint8_t *)
void setHighAlarmTemp (uint8_t *, const char)
void setLowAlarmTemp (uint8_t *, const char)
char getHighAlarmTemp (uint8_t *)
char getLowAlarmTemp (uint8_t *)
void resetAlarmSearch (void)
bool alarmSearch (uint8_t *)
bool hasAlarm (uint8_t *)
bool hasAlarm (void)
void processAlarms (void)
void setAlarmHandler (AlarmHandler *)
void * operator new (unsigned int)
void operator delete (void *)

Static Public Member Functions

static void defaultAlarmHandler (uint8_t *)
static float toFahrenheit (const float)
static float toCelsius (const float)

Private Types

typedef uint8_t ScratchPad [9]

Private Member Functions

float calculateTemperature (uint8_t *, uint8_t *)
void blockTillConversionComplete (uint8_t *, uint8_t *)

Private Attributes

bool parasite
uint8_t bitResolution
bool waitForConversion
bool checkForConversion
uint8_t devices
OneWire_wire
uint8_t alarmSearchAddress [8]
char alarmSearchJunction
uint8_t alarmSearchExhausted
AlarmHandler_AlarmHandler

Detailed Description

Definition at line 60 of file DallasTemperature.h.


Member Typedef Documentation

typedef void DallasTemperature::AlarmHandler(uint8_t *)

Definition at line 145 of file DallasTemperature.h.

typedef uint8_t DallasTemperature::ScratchPad[9] [private]

Definition at line 203 of file DallasTemperature.h.


Constructor & Destructor Documentation

DallasTemperature::DallasTemperature ( OneWire _oneWire)

Definition at line 20 of file DallasTemperature.cpp.

  : _AlarmHandler(&defaultAlarmHandler)
  #endif
{
  _wire = _oneWire;
  devices = 0;
  parasite = false;
  bitResolution = 9;
  waitForConversion = true;
  checkForConversion = true;
}

Member Function Documentation

bool DallasTemperature::alarmSearch ( uint8_t *  newAddr)

Definition at line 583 of file DallasTemperature.cpp.

{
  uint8_t i;
  char lastJunction = -1;
  uint8_t done = 1;

  if (alarmSearchExhausted) return false;
  if (!_wire->reset()) return false;

  // send the alarm search command
  _wire->write(0xEC, 0);

  for(i = 0; i < 64; i++)
  {
    uint8_t a = _wire->read_bit( );
    uint8_t nota = _wire->read_bit( );
    uint8_t ibyte = i / 8;
    uint8_t ibit = 1 << (i & 7);

    // I don't think this should happen, this means nothing responded, but maybe if
    // something vanishes during the search it will come up.
    if (a && nota) return false;

    if (!a && !nota)
    {
      if (i == alarmSearchJunction)
      {
        // this is our time to decide differently, we went zero last time, go one.
        a = 1;
        alarmSearchJunction = lastJunction;
      }
      else if (i < alarmSearchJunction)
      {
        // take whatever we took last time, look in address
        if (alarmSearchAddress[ibyte] & ibit) a = 1;
        else
        {
          // Only 0s count as pending junctions, we've already exhasuted the 0 side of 1s
          a = 0;
          done = 0;
          lastJunction = i;
        }
      }
      else
      {
        // we are blazing new tree, take the 0
        a = 0;
        alarmSearchJunction = i;
        done = 0;
      }
      // OneWire search fix
      // See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
    }

    if (a) alarmSearchAddress[ibyte] |= ibit;
    else alarmSearchAddress[ibyte] &= ~ibit;

    _wire->write_bit(a);
  }

  if (done) alarmSearchExhausted = 1;
  for (i = 0; i < 8; i++) newAddr[i] = alarmSearchAddress[i];
  return true;
}
void DallasTemperature::begin ( void  )

Definition at line 34 of file DallasTemperature.cpp.

{
  DeviceAddress deviceAddress;

  _wire->reset_search();
  devices = 0; // Reset the number of devices when we enumerate wire devices

  while (_wire->search(deviceAddress))
  {
    if (validAddress(deviceAddress))
    {
      if (!parasite && readPowerSupply(deviceAddress)) parasite = true;

      ScratchPad scratchPad;

      readScratchPad(deviceAddress, scratchPad);

      bitResolution = max(bitResolution, getResolution(deviceAddress));

      devices++;
    }
  }
}
void DallasTemperature::blockTillConversionComplete ( uint8_t *  bitResolution,
uint8_t *  deviceAddress 
) [private]

Definition at line 359 of file DallasTemperature.cpp.

{
    if(deviceAddress != 0 && checkForConversion && !parasite)
    {
        // Continue to check if the IC has responded with a temperature
        // NB: Could cause issues with multiple devices (one device may respond faster)
        unsigned long start = millis();
        while(!isConversionAvailable(0) && ((millis() - start) < 750)); 
    }
    
    // Wait a fix number of cycles till conversion is complete (based on IC datasheet)
      switch (*bitResolution)
      {
        case 9:
          delay(94);
          break;
        case 10:
          delay(188);
          break;
        case 11:
          delay(375);
          break;
        case 12:
        default:
          delay(750);
          break;
      }

}
float DallasTemperature::calculateTemperature ( uint8_t *  deviceAddress,
uint8_t *  scratchPad 
) [private]

Definition at line 412 of file DallasTemperature.cpp.

{
  int16_t rawTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 8) | scratchPad[TEMP_LSB];

  switch (deviceAddress[0])
  {
    case DS18B20MODEL:
    case DS1822MODEL:
      switch (scratchPad[CONFIGURATION])
      {
        case TEMP_12_BIT:
          return (float)rawTemperature * 0.0625;
          break;
        case TEMP_11_BIT:
          return (float)(rawTemperature >> 1) * 0.125;
          break;
        case TEMP_10_BIT:
          return (float)(rawTemperature >> 2) * 0.25;
          break;
        case TEMP_9_BIT:
          return (float)(rawTemperature >> 3) * 0.5;
          break;
      }
      break;
    case DS18S20MODEL:
      /*

      Resolutions greater than 9 bits can be calculated using the data from
      the temperature, COUNT REMAIN and COUNT PER �C registers in the
      scratchpad. Note that the COUNT PER �C register is hard-wired to 16
      (10h). After reading the scratchpad, the TEMP_READ value is obtained
      by truncating the 0.5�C bit (bit 0) from the temperature data. The
      extended resolution temperature can then be calculated using the
      following equation:

                                       COUNT_PER_C - COUNT_REMAIN
      TEMPERATURE = TEMP_READ - 0.25 + --------------------------
                                               COUNT_PER_C
      */

      // Good spot. Thanks Nic Johns for your contribution
      return (float)(rawTemperature >> 1) - 0.25 +((float)(scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) / (float)scratchPad[COUNT_PER_C] );
      break;
  }
}
void DallasTemperature::defaultAlarmHandler ( uint8_t *  deviceAddress) [static]

Definition at line 698 of file DallasTemperature.cpp.

{
}
bool DallasTemperature::getAddress ( uint8_t *  deviceAddress,
const uint8_t  index 
)

Definition at line 72 of file DallasTemperature.cpp.

{
  uint8_t depth = 0;

  _wire->reset_search();

  while (depth <= index && _wire->search(deviceAddress))
  {
    if (depth == index && validAddress(deviceAddress)) return true;
    depth++;
  }

  return false;
}
bool DallasTemperature::getCheckForConversion ( void  )

Definition at line 307 of file DallasTemperature.cpp.

{
    return checkForConversion;
}
uint8_t DallasTemperature::getDeviceCount ( void  )

Definition at line 59 of file DallasTemperature.cpp.

{
  return devices;
}
char DallasTemperature::getHighAlarmTemp ( uint8_t *  deviceAddress)

Definition at line 546 of file DallasTemperature.cpp.

{
  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[HIGH_ALARM_TEMP];
  return DEVICE_DISCONNECTED;
}
char DallasTemperature::getLowAlarmTemp ( uint8_t *  deviceAddress)

Definition at line 555 of file DallasTemperature.cpp.

{
  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[LOW_ALARM_TEMP];
  return DEVICE_DISCONNECTED;
}
uint8_t DallasTemperature::getResolution ( )

Definition at line 247 of file DallasTemperature.cpp.

{
    return bitResolution;
}
uint8_t DallasTemperature::getResolution ( uint8_t *  deviceAddress)

Definition at line 254 of file DallasTemperature.cpp.

{
  if (deviceAddress[0] == DS18S20MODEL) return 9; // this model has a fixed resolution

  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad))
  {
    switch (scratchPad[CONFIGURATION])
    {
      case TEMP_12_BIT:
        return 12;
        
      case TEMP_11_BIT:
        return 11;
        
      case TEMP_10_BIT:
        return 10;
        
      case TEMP_9_BIT:
        return 9;
        
    }
  }
  return 0;
}
float DallasTemperature::getTempC ( uint8_t *  deviceAddress)

Definition at line 463 of file DallasTemperature.cpp.

{
  // TODO: Multiple devices (up to 64) on the same bus may take 
  //       some time to negotiate a response
  // What happens in case of collision?

  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad)) return calculateTemperature(deviceAddress, scratchPad);
  return DEVICE_DISCONNECTED;
}
float DallasTemperature::getTempCByIndex ( uint8_t  deviceIndex)

Definition at line 398 of file DallasTemperature.cpp.

{
  DeviceAddress deviceAddress;
  getAddress(deviceAddress, deviceIndex);
  return getTempC((uint8_t*)deviceAddress);
}
float DallasTemperature::getTempF ( uint8_t *  deviceAddress)

Definition at line 477 of file DallasTemperature.cpp.

{
  return toFahrenheit(getTempC(deviceAddress));
}
float DallasTemperature::getTempFByIndex ( uint8_t  deviceIndex)

Definition at line 406 of file DallasTemperature.cpp.

{
  return toFahrenheit(getTempCByIndex(deviceIndex));
}
bool DallasTemperature::getWaitForConversion ( void  )

Definition at line 292 of file DallasTemperature.cpp.

{
    return waitForConversion;
}
bool DallasTemperature::hasAlarm ( uint8_t *  deviceAddress)

Definition at line 652 of file DallasTemperature.cpp.

{
  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad))
  {
    float temp = calculateTemperature(deviceAddress, scratchPad);

    // check low alarm
    if ((char)temp <= (char)scratchPad[LOW_ALARM_TEMP]) return true;

    // check high alarm
    if ((char)temp >= (char)scratchPad[HIGH_ALARM_TEMP]) return true;
  }

  // no alarm
  return false;
}
bool DallasTemperature::hasAlarm ( void  )

Definition at line 671 of file DallasTemperature.cpp.

{
  DeviceAddress deviceAddress;
  resetAlarmSearch();
  return alarmSearch(deviceAddress);
}
bool DallasTemperature::isConnected ( uint8_t *  deviceAddress)

Definition at line 88 of file DallasTemperature.cpp.

{
  ScratchPad scratchPad;
  return isConnected(deviceAddress, scratchPad);
}
bool DallasTemperature::isConnected ( uint8_t *  deviceAddress,
uint8_t *  scratchPad 
)

Definition at line 96 of file DallasTemperature.cpp.

{
  readScratchPad(deviceAddress, scratchPad);
  return (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
}
bool DallasTemperature::isConversionAvailable ( uint8_t *  deviceAddress)

Definition at line 312 of file DallasTemperature.cpp.

{
    // Check if the clock has been raised indicating the conversion is complete
    ScratchPad scratchPad;
    readScratchPad(deviceAddress, scratchPad);
    return scratchPad[0];
}   
bool DallasTemperature::isConversionComplete ( void  )
bool DallasTemperature::isParasitePowerMode ( void  )

Definition at line 483 of file DallasTemperature.cpp.

{
  return parasite;
}
void DallasTemperature::operator delete ( void *  p)

Definition at line 730 of file DallasTemperature.cpp.

{
  DallasTemperature* pNss =  (DallasTemperature*) p; // Cast to NSS pointer
  pNss->~DallasTemperature(); // Destruct the object

  free(p); // Free the memory
}
void * DallasTemperature::operator new ( unsigned int  size)

!! CANT EXPLICITLY CALL CONSTRUCTOR - workaround by using an init() methodR - workaround by using an init() method

Definition at line 719 of file DallasTemperature.cpp.

{
  void * p; // void pointer
  p = malloc(size); // Allocate memory
  memset((DallasTemperature*)p,0,size); // Initalise memory

  return (DallasTemperature*) p; // Cast blank region to NSS pointer
}
void DallasTemperature::processAlarms ( void  )

Definition at line 679 of file DallasTemperature.cpp.

{
  resetAlarmSearch();
  DeviceAddress alarmAddr;

  while (alarmSearch(alarmAddr))
  {
    if (validAddress(alarmAddr))
      _AlarmHandler(alarmAddr);
  }
}
bool DallasTemperature::readPowerSupply ( uint8_t *  deviceAddress)

Definition at line 188 of file DallasTemperature.cpp.

{
  bool ret = false;
  _wire->reset();
  _wire->select(deviceAddress);
  _wire->write(READPOWERSUPPLY);
  if (_wire->read_bit() == 0) ret = true;
  _wire->reset();
  return ret;
}
void DallasTemperature::readScratchPad ( uint8_t *  deviceAddress,
uint8_t *  scratchPad 
)

Definition at line 103 of file DallasTemperature.cpp.

{
  // send the command
  _wire->reset();
  _wire->select(deviceAddress);
  _wire->write(READSCRATCH);

  // TODO => collect all comments &  use simple loop
  // byte 0: temperature LSB  
  // byte 1: temperature MSB
  // byte 2: high alarm temp
  // byte 3: low alarm temp
  // byte 4: DS18S20: store for crc
  //         DS18B20 & DS1822: configuration register
  // byte 5: internal use & crc
  // byte 6: DS18S20: COUNT_REMAIN
  //         DS18B20 & DS1822: store for crc
  // byte 7: DS18S20: COUNT_PER_C
  //         DS18B20 & DS1822: store for crc
  // byte 8: SCRATCHPAD_CRC
  //
  // for(int i=0; i<9; i++)
  // {
  //   scratchPad[i] = _wire->read();
  // }

  
  // read the response

  // byte 0: temperature LSB
  scratchPad[TEMP_LSB] = _wire->read();

  // byte 1: temperature MSB
  scratchPad[TEMP_MSB] = _wire->read();

  // byte 2: high alarm temp
  scratchPad[HIGH_ALARM_TEMP] = _wire->read();

  // byte 3: low alarm temp
  scratchPad[LOW_ALARM_TEMP] = _wire->read();

  // byte 4:
  // DS18S20: store for crc
  // DS18B20 & DS1822: configuration register
  scratchPad[CONFIGURATION] = _wire->read();

  // byte 5:
  // internal use & crc
  scratchPad[INTERNAL_BYTE] = _wire->read();

  // byte 6:
  // DS18S20: COUNT_REMAIN
  // DS18B20 & DS1822: store for crc
  scratchPad[COUNT_REMAIN] = _wire->read();

  // byte 7:
  // DS18S20: COUNT_PER_C
  // DS18B20 & DS1822: store for crc
  scratchPad[COUNT_PER_C] = _wire->read();

  // byte 8:
  // SCTRACHPAD_CRC
  scratchPad[SCRATCHPAD_CRC] = _wire->read();

  _wire->reset();
}
void DallasTemperature::requestTemperatures ( void  )

Definition at line 322 of file DallasTemperature.cpp.

{
  _wire->reset();
  _wire->skip();
  _wire->write(STARTCONVO, parasite);

  // ASYNC mode?
  if (!waitForConversion) return; 
  blockTillConversionComplete(&bitResolution, 0);

  return;
}
bool DallasTemperature::requestTemperaturesByAddress ( uint8_t *  deviceAddress)

Definition at line 338 of file DallasTemperature.cpp.

{

  _wire->reset();
  _wire->select(deviceAddress);
  _wire->write(STARTCONVO, parasite);
  
    // check device
  ScratchPad scratchPad;
  if (!isConnected(deviceAddress, scratchPad)) return false;
  
  
  // ASYNC mode?
  if (!waitForConversion) return true;   
  uint8_t bitResolution = getResolution(deviceAddress);
  blockTillConversionComplete(&bitResolution, deviceAddress);
  
  return true;
}
bool DallasTemperature::requestTemperaturesByIndex ( uint8_t  deviceIndex)

Definition at line 390 of file DallasTemperature.cpp.

{
  DeviceAddress deviceAddress;
  getAddress(deviceAddress, deviceIndex);
  return requestTemperaturesByAddress(deviceAddress);
}
void DallasTemperature::resetAlarmSearch ( void  )

Definition at line 563 of file DallasTemperature.cpp.

{
  alarmSearchJunction = -1;
  alarmSearchExhausted = 0;
  for(uint8_t i = 0; i < 7; i++)
    alarmSearchAddress[i] = 0;
}
void DallasTemperature::setAlarmHandler ( AlarmHandler handler)

Definition at line 692 of file DallasTemperature.cpp.

{
  _AlarmHandler = handler;
}
void DallasTemperature::setCheckForConversion ( bool  flag)

Definition at line 301 of file DallasTemperature.cpp.

{
    checkForConversion = flag;
}
void DallasTemperature::setHighAlarmTemp ( uint8_t *  deviceAddress,
const char  celsius 
)

Definition at line 513 of file DallasTemperature.cpp.

{
  // make sure the alarm temperature is within the device's range
  if (celsius > 125) celsius = 125;
  else if (celsius < -55) celsius = -55;

  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad))
  {
    scratchPad[HIGH_ALARM_TEMP] = (uint8_t)celsius;
    writeScratchPad(deviceAddress, scratchPad);
  }
}
void DallasTemperature::setLowAlarmTemp ( uint8_t *  deviceAddress,
const char  celsius 
)

Definition at line 530 of file DallasTemperature.cpp.

{
  // make sure the alarm temperature is within the device's range
  if (celsius > 125) celsius = 125;
  else if (celsius < -55) celsius = -55;

  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad))
  {
    scratchPad[LOW_ALARM_TEMP] = (uint8_t)celsius;
    writeScratchPad(deviceAddress, scratchPad);
  }
}
bool DallasTemperature::setResolution ( uint8_t *  deviceAddress,
uint8_t  newResolution 
)

Definition at line 215 of file DallasTemperature.cpp.

{
  ScratchPad scratchPad;
  if (isConnected(deviceAddress, scratchPad))
  {
    // DS18S20 has a fixed 9-bit resolution
    if (deviceAddress[0] != DS18S20MODEL)
    {
      switch (newResolution)
      {
        case 12:
          scratchPad[CONFIGURATION] = TEMP_12_BIT;
          break;
        case 11:
          scratchPad[CONFIGURATION] = TEMP_11_BIT;
          break;
        case 10:
          scratchPad[CONFIGURATION] = TEMP_10_BIT;
          break;
        case 9:
        default:
          scratchPad[CONFIGURATION] = TEMP_9_BIT;
          break;
      }
      writeScratchPad(deviceAddress, scratchPad);
    }
    return true;  // new value set
  }
  return false;
}
void DallasTemperature::setResolution ( uint8_t  newResolution)

Definition at line 202 of file DallasTemperature.cpp.

{
  bitResolution = constrain(newResolution, 9, 12);
  DeviceAddress deviceAddress;
  for (int i=0; i<devices; i++)
  {
    getAddress(deviceAddress, i);
    setResolution(deviceAddress, bitResolution);
  }
}
void DallasTemperature::setWaitForConversion ( bool  flag)

Definition at line 286 of file DallasTemperature.cpp.

{
    waitForConversion = flag;
}
float DallasTemperature::toCelsius ( const float  fahrenheit) [static]

Definition at line 711 of file DallasTemperature.cpp.

{
  return (fahrenheit - 32) / 1.8;
}
float DallasTemperature::toFahrenheit ( const float  celsius) [static]

Definition at line 705 of file DallasTemperature.cpp.

{
  return (celsius * 1.8) + 32;
}
bool DallasTemperature::validAddress ( uint8_t *  deviceAddress)

Definition at line 65 of file DallasTemperature.cpp.

{
  return (_wire->crc8(deviceAddress, 7) == deviceAddress[7]);
}
void DallasTemperature::writeScratchPad ( uint8_t *  deviceAddress,
const uint8_t *  scratchPad 
)

Definition at line 171 of file DallasTemperature.cpp.

{
  _wire->reset();
  _wire->select(deviceAddress);
  _wire->write(WRITESCRATCH);
  _wire->write(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp
  _wire->write(scratchPad[LOW_ALARM_TEMP]); // low alarm temp
  // DS18S20 does not use the configuration register
  if (deviceAddress[0] != DS18S20MODEL) _wire->write(scratchPad[CONFIGURATION]); // configuration
  _wire->reset();
  // save the newly written values to eeprom
  _wire->write(COPYSCRATCH, parasite);
  if (parasite) delay(10); // 10ms delay
  _wire->reset();
}

Member Data Documentation

Definition at line 237 of file DallasTemperature.h.

Definition at line 222 of file DallasTemperature.h.

Definition at line 232 of file DallasTemperature.h.

Definition at line 234 of file DallasTemperature.h.

Definition at line 233 of file DallasTemperature.h.

Definition at line 210 of file DallasTemperature.h.

Definition at line 216 of file DallasTemperature.h.

uint8_t DallasTemperature::devices [private]

Definition at line 219 of file DallasTemperature.h.

Definition at line 206 of file DallasTemperature.h.

Definition at line 213 of file DallasTemperature.h.


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs