99
1010#include " DallasTemperature.h"
1111
12+
1213#if ARDUINO >= 100
1314#include " Arduino.h"
1415#else
@@ -107,7 +108,7 @@ bool DallasTemperature::isConnected(const uint8_t* deviceAddress, uint8_t* scrat
107108 return b && (_wire->crc8 (scratchPad, 8 ) == scratchPad[SCRATCHPAD_CRC]);
108109}
109110
110- void DallasTemperature::readScratchPad (const uint8_t * deviceAddress, uint8_t * scratchPad){
111+ bool DallasTemperature::readScratchPad (const uint8_t * deviceAddress, uint8_t * scratchPad){
111112
112113 // send the reset command and fail fast
113114 int b = _wire->reset ();
@@ -334,31 +335,16 @@ bool DallasTemperature::requestTemperaturesByAddress(const uint8_t* deviceAddres
334335}
335336
336337
337- void DallasTemperature::blockTillConversionComplete (uint8_t bitResolution, uint8_t * deviceAddress)
338- {
339- if (deviceAddress != 0 && checkForConversion && !parasite){
340- // Continue to check if the IC has responded with a temperature
341- // NB: Could cause issues with multiple devices (one device may respond faster)
342- unsigned long start = millis ();
343- while (!isConversionAvailable (0 ) && ((millis () - start) < 750 ));
344- }
338+ // Continue to check if the IC has responded with a temperature
339+ void DallasTemperature::blockTillConversionComplete (uint8_t bitResolution, const uint8_t * deviceAddress){
345340
346- // Wait a fix number of cycles till conversion is complete (based on IC datasheet)
347- switch (*bitResolution){
348- case 9 :
349- delay (94 );
350- break ;
351- case 10 :
352- delay (188 );
353- break ;
354- case 11 :
355- delay (375 );
356- break ;
357- case 12 :
358- default :
359- delay (750 );
360- break ;
361- }
341+ int delms = millisToWaitForConversion (bitResolution);
342+ if (deviceAddress != NULL && checkForConversion && !parasite){
343+ unsigned long timend = millis () + delms;
344+ while (!isConversionAvailable (deviceAddress) && (millis () < timend));
345+ }else {
346+ delay (delms);
347+ }
362348
363349}
364350
@@ -378,18 +364,6 @@ int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution){
378364
379365}
380366
381- // Continue to check if the IC has responded with a temperature
382- void DallasTemperature::blockTillConversionComplete (uint8_t bitResolution, const uint8_t * deviceAddress){
383-
384- int delms = millisToWaitForConversion (bitResolution);
385- if (deviceAddress != NULL && checkForConversion && !parasite){
386- unsigned long timend = millis () + delms;
387- while (!isConversionAvailable (deviceAddress) && (millis () < timend));
388- }else {
389- delay (delms);
390- }
391-
392- }
393367
394368// sends command for one device to perform a temp conversion by index
395369bool DallasTemperature::requestTemperaturesByIndex (uint8_t deviceIndex){
@@ -506,6 +480,82 @@ bool DallasTemperature::isParasitePowerMode(void){
506480 return parasite;
507481}
508482
483+
484+ // IF alarm is not used one can store a 16 bit int of userdata in the alarm
485+ // registers. E.g. an ID of the sensor.
486+ // See github issue #29
487+
488+ // note if device is not connected it will fail writing the data.
489+ void DallasTemperature::setUserData (const uint8_t * deviceAddress, int16_t data)
490+ {
491+ ScratchPad scratchPad;
492+ if (isConnected (deviceAddress, scratchPad))
493+ {
494+ scratchPad[HIGH_ALARM_TEMP] = data >> 8 ;
495+ scratchPad[LOW_ALARM_TEMP] = data & 255 ;
496+ writeScratchPad (deviceAddress, scratchPad);
497+ }
498+ }
499+
500+ int16_t DallasTemperature::getUserData (const uint8_t * deviceAddress)
501+ {
502+ int16_t data = 0 ;
503+ ScratchPad scratchPad;
504+ if (isConnected (deviceAddress, scratchPad))
505+ {
506+ data = scratchPad[HIGH_ALARM_TEMP] << 8 ;
507+ data += scratchPad[LOW_ALARM_TEMP];
508+ }
509+ return data;
510+ }
511+
512+ // note If address cannot be found no error will be reported.
513+ int16_t DallasTemperature::getUserDataByIndex (uint8_t deviceIndex)
514+ {
515+ DeviceAddress deviceAddress;
516+ getAddress (deviceAddress, deviceIndex);
517+ return getUserData ((uint8_t *) deviceAddress);
518+ }
519+
520+ void DallasTemperature::setUserDataByIndex (uint8_t deviceIndex, int16_t data)
521+ {
522+ DeviceAddress deviceAddress;
523+ getAddress (deviceAddress, deviceIndex);
524+ setUserData ((uint8_t *) deviceAddress, data);
525+ }
526+
527+
528+ // Convert float Celsius to Fahrenheit
529+ float DallasTemperature::toFahrenheit (float celsius){
530+ return (celsius * 1.8 ) + 32 ;
531+ }
532+
533+ // Convert float Fahrenheit to Celsius
534+ float DallasTemperature::toCelsius (float fahrenheit){
535+ return (fahrenheit - 32 ) * 0.555555556 ;
536+ }
537+
538+ // convert from raw to Celsius
539+ float DallasTemperature::rawToCelsius (int16_t raw){
540+
541+ if (raw <= DEVICE_DISCONNECTED_RAW)
542+ return DEVICE_DISCONNECTED_C;
543+ // C = RAW/128
544+ return (float )raw * 0.0078125 ;
545+
546+ }
547+
548+ // convert from raw to Fahrenheit
549+ float DallasTemperature::rawToFahrenheit (int16_t raw){
550+
551+ if (raw <= DEVICE_DISCONNECTED_RAW)
552+ return DEVICE_DISCONNECTED_F;
553+ // C = RAW/128
554+ // F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
555+ return ((float )raw * 0.0140625 ) + 32 ;
556+
557+ }
558+
509559#if REQUIRESALARMS
510560
511561/*
@@ -720,81 +770,6 @@ void DallasTemperature::defaultAlarmHandler(const uint8_t* deviceAddress){}
720770
721771#endif
722772
723- // IF alarm is not used one can store a 16 bit int of userdata in the alarm
724- // registers. E.g. an ID of the sensor.
725- // See github issue #29
726-
727- // note if device is not connected it will fail writing the data.
728- void DallasTemperature::setUserData (const uint8_t * deviceAddress, int16_t data)
729- {
730- ScratchPad scratchPad;
731- if (isConnected (deviceAddress, scratchPad))
732- {
733- scratchPad[HIGH_ALARM_TEMP] = data >> 8 ;
734- scratchPad[LOW_ALARM_TEMP] = data & 255 ;
735- writeScratchPad (deviceAddress, scratchPad);
736- }
737- }
738-
739- int16_t DallasTemperature::getUserData (const uint8_t * deviceAddress)
740- {
741- int16_t data = 0 ;
742- ScratchPad scratchPad;
743- if (isConnected (deviceAddress, scratchPad))
744- {
745- data = scratchPad[HIGH_ALARM_TEMP] << 8 ;
746- data += scratchPad[LOW_ALARM_TEMP];
747- }
748- return data;
749- }
750-
751- // note If address cannot be found no error will be reported.
752- int16_t DallasTemperature::getUserDataByIndex (uint8_t deviceIndex)
753- {
754- DeviceAddress deviceAddress;
755- getAddress (deviceAddress, deviceIndex);
756- return getUserData ((uint8_t *) deviceAddress);
757- }
758-
759- void DallasTemperature::setUserDataByIndex (uint8_t deviceIndex, int16_t data)
760- {
761- DeviceAddress deviceAddress;
762- getAddress (deviceAddress, deviceIndex);
763- setUserData ((uint8_t *) deviceAddress, data);
764- }
765-
766-
767- // Convert float Celsius to Fahrenheit
768- float DallasTemperature::toFahrenheit (float celsius){
769- return (celsius * 1.8 ) + 32 ;
770- }
771-
772- // Convert float Fahrenheit to Celsius
773- float DallasTemperature::toCelsius (float fahrenheit){
774- return (fahrenheit - 32 ) * 0.555555556 ;
775- }
776-
777- // convert from raw to Celsius
778- float DallasTemperature::rawToCelsius (int16_t raw){
779-
780- if (raw <= DEVICE_DISCONNECTED_RAW)
781- return DEVICE_DISCONNECTED_C;
782- // C = RAW/128
783- return (float )raw * 0.0078125 ;
784-
785- }
786-
787- // convert from raw to Fahrenheit
788- float DallasTemperature::rawToFahrenheit (int16_t raw){
789-
790- if (raw <= DEVICE_DISCONNECTED_RAW)
791- return DEVICE_DISCONNECTED_F;
792- // C = RAW/128
793- // F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
794- return ((float )raw * 0.0140625 ) + 32 ;
795-
796- }
797-
798773#if REQUIRESNEW
799774
800775// MnetCS - Allocates memory for DallasTemperature. Allows us to instance a new object
0 commit comments