DallasTemperature.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #ifndef DallasTemperature_h
  2. #define DallasTemperature_h
  3. #define DALLASTEMPLIBVERSION "3.7.2"
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. // set to true to include code for new and delete operators
  9. #ifndef REQUIRESNEW
  10. #define REQUIRESNEW false
  11. #endif
  12. // set to true to include code implementing alarm search functions
  13. #ifndef REQUIRESALARMS
  14. #define REQUIRESALARMS true
  15. #endif
  16. #include <inttypes.h>
  17. #include <OneWire.h>
  18. // Model IDs
  19. #define DS18S20MODEL 0x10
  20. #define DS18B20MODEL 0x28
  21. #define DS1822MODEL 0x22
  22. #define MAX31850MODEL 0x3B
  23. // OneWire commands
  24. #define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
  25. #define COPYSCRATCH 0x48 // Copy EEPROM
  26. #define READSCRATCH 0xBE // Read EEPROM
  27. #define WRITESCRATCH 0x4E // Write to EEPROM
  28. #define RECALLSCRATCH 0xB8 // Reload from last known
  29. #define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
  30. #define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
  31. // Scratchpad locations
  32. #define TEMP_LSB 0
  33. #define TEMP_MSB 1
  34. #define HIGH_ALARM_TEMP 2
  35. #define LOW_ALARM_TEMP 3
  36. #define CONFIGURATION 4
  37. #define INTERNAL_BYTE 5
  38. #define COUNT_REMAIN 6
  39. #define COUNT_PER_C 7
  40. #define SCRATCHPAD_CRC 8
  41. // Device resolution
  42. #define TEMP_9_BIT 0x1F // 9 bit
  43. #define TEMP_10_BIT 0x3F // 10 bit
  44. #define TEMP_11_BIT 0x5F // 11 bit
  45. #define TEMP_12_BIT 0x7F // 12 bit
  46. // Error Codes
  47. #define DEVICE_DISCONNECTED -127
  48. typedef uint8_t DeviceAddress[8];
  49. class DallasTemperature
  50. {
  51. public:
  52. DallasTemperature(OneWire*);
  53. // initalise bus
  54. void begin(void);
  55. // returns the number of devices found on the bus
  56. uint8_t getDeviceCount(void);
  57. // Is a conversion complete on the wire?
  58. bool isConversionComplete(void);
  59. // returns true if address is valid
  60. bool validAddress(uint8_t*);
  61. // finds an address at a given index on the bus
  62. bool getAddress(uint8_t*, const uint8_t);
  63. // attempt to determine if the device at the given address is connected to the bus
  64. bool isConnected(uint8_t*);
  65. // attempt to determine if the device at the given address is connected to the bus
  66. // also allows for updating the read scratchpad
  67. bool isConnected(uint8_t*, uint8_t*);
  68. // read device's scratchpad
  69. void readScratchPad(uint8_t*, uint8_t*);
  70. // write device's scratchpad
  71. void writeScratchPad(uint8_t*, const uint8_t*);
  72. // read device's power requirements
  73. bool readPowerSupply(uint8_t*);
  74. // get global resolution
  75. uint8_t getResolution();
  76. // set global resolution to 9, 10, 11, or 12 bits
  77. void setResolution(uint8_t);
  78. // returns the device resolution, 9-12
  79. uint8_t getResolution(uint8_t*);
  80. // set resolution of a device to 9, 10, 11, or 12 bits
  81. bool setResolution(uint8_t*, uint8_t);
  82. // sets/gets the waitForConversion flag
  83. void setWaitForConversion(bool);
  84. bool getWaitForConversion(void);
  85. // sets/gets the checkForConversion flag
  86. void setCheckForConversion(bool);
  87. bool getCheckForConversion(void);
  88. // sends command for all devices on the bus to perform a temperature conversion
  89. void requestTemperatures(void);
  90. // sends command for one device to perform a temperature conversion by address
  91. bool requestTemperaturesByAddress(uint8_t*);
  92. // sends command for one device to perform a temperature conversion by index
  93. bool requestTemperaturesByIndex(uint8_t);
  94. // returns temperature in degrees C
  95. float getTempC(uint8_t*);
  96. // returns temperature in degrees F
  97. float getTempF(uint8_t*);
  98. // Get temperature for device index (slow)
  99. float getTempCByIndex(uint8_t);
  100. // Get temperature for device index (slow)
  101. float getTempFByIndex(uint8_t);
  102. // returns true if the bus requires parasite power
  103. bool isParasitePowerMode(void);
  104. bool isConversionAvailable(uint8_t*);
  105. #if REQUIRESALARMS
  106. typedef void AlarmHandler(uint8_t*);
  107. // sets the high alarm temperature for a device
  108. // accepts a char. valid range is -55C - 125C
  109. void setHighAlarmTemp(uint8_t*, const char);
  110. // sets the low alarm temperature for a device
  111. // accepts a char. valid range is -55C - 125C
  112. void setLowAlarmTemp(uint8_t*, const char);
  113. // returns a signed char with the current high alarm temperature for a device
  114. // in the range -55C - 125C
  115. char getHighAlarmTemp(uint8_t*);
  116. // returns a signed char with the current low alarm temperature for a device
  117. // in the range -55C - 125C
  118. char getLowAlarmTemp(uint8_t*);
  119. // resets internal variables used for the alarm search
  120. void resetAlarmSearch(void);
  121. // search the wire for devices with active alarms
  122. bool alarmSearch(uint8_t*);
  123. // returns true if ia specific device has an alarm
  124. bool hasAlarm(uint8_t*);
  125. // returns true if any device is reporting an alarm on the bus
  126. bool hasAlarm(void);
  127. // runs the alarm handler for all devices returned by alarmSearch()
  128. void processAlarms(void);
  129. // sets the alarm handler
  130. void setAlarmHandler(AlarmHandler *);
  131. // The default alarm handler
  132. static void defaultAlarmHandler(uint8_t*);
  133. #endif
  134. // convert from celcius to farenheit
  135. static float toFahrenheit(const float);
  136. // convert from farenheit to celsius
  137. static float toCelsius(const float);
  138. #if REQUIRESNEW
  139. // initalize memory area
  140. void* operator new (unsigned int);
  141. // delete memory reference
  142. void operator delete(void*);
  143. #endif
  144. private:
  145. typedef uint8_t ScratchPad[9];
  146. // parasite power on or off
  147. bool parasite;
  148. // used to determine the delay amount needed to allow for the
  149. // temperature conversion to take place
  150. uint8_t bitResolution;
  151. // used to requestTemperature with or without delay
  152. bool waitForConversion;
  153. // used to requestTemperature to dynamically check if a conversion is complete
  154. bool checkForConversion;
  155. // count of devices on the bus
  156. uint8_t devices;
  157. // Take a pointer to one wire instance
  158. OneWire* _wire;
  159. // reads scratchpad and returns the temperature in degrees C
  160. float calculateTemperature(uint8_t*, uint8_t*);
  161. void blockTillConversionComplete(uint8_t*,uint8_t*);
  162. #if REQUIRESALARMS
  163. // required for alarmSearch
  164. uint8_t alarmSearchAddress[8];
  165. char alarmSearchJunction;
  166. uint8_t alarmSearchExhausted;
  167. // the alarm handler function pointer
  168. AlarmHandler *_AlarmHandler;
  169. #endif
  170. };
  171. #endif