DallasTemperature.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef DallasTemperature_h
  2. #define DallasTemperature_h
  3. #define DALLASTEMPLIBVERSION "3.8.1" // To be deprecated -> TODO remove in 4.0.0
  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. #ifdef __STM32F1__
  18. #include <OneWireSTM.h>
  19. #else
  20. #include <OneWire.h>
  21. #endif
  22. // Model IDs
  23. #define DS18S20MODEL 0x10 // also DS1820
  24. #define DS18B20MODEL 0x28 // also MAX31820
  25. #define DS1822MODEL 0x22
  26. #define DS1825MODEL 0x3B
  27. #define DS28EA00MODEL 0x42
  28. // Error Codes
  29. #define DEVICE_DISCONNECTED_C -127
  30. #define DEVICE_DISCONNECTED_F -196.6
  31. #define DEVICE_DISCONNECTED_RAW -7040
  32. // For readPowerSupply on oneWire bus
  33. // definition of nullptr for C++ < 11, using official workaround:
  34. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf
  35. #if __cplusplus < 201103L
  36. const class
  37. {
  38. public:
  39. template <class T>
  40. operator T *() const
  41. {
  42. return 0;
  43. }
  44. template <class C, class T>
  45. operator T C::*() const
  46. {
  47. return 0;
  48. }
  49. private:
  50. void operator&() const;
  51. } nullptr = {};
  52. #endif
  53. typedef uint8_t DeviceAddress[8];
  54. class DallasTemperature {
  55. public:
  56. DallasTemperature();
  57. DallasTemperature(OneWire*);
  58. DallasTemperature(OneWire*, uint8_t);
  59. void setOneWire(OneWire*);
  60. void setPullupPin(uint8_t);
  61. // initialise bus
  62. void begin(void);
  63. // returns the number of devices found on the bus
  64. uint8_t getDeviceCount(void);
  65. // returns the number of DS18xxx Family devices on bus
  66. uint8_t getDS18Count(void);
  67. // returns true if address is valid
  68. bool validAddress(const uint8_t*);
  69. // returns true if address is of the family of sensors the lib supports.
  70. bool validFamily(const uint8_t* deviceAddress);
  71. // finds an address at a given index on the bus
  72. bool getAddress(uint8_t*, uint8_t);
  73. // attempt to determine if the device at the given address is connected to the bus
  74. bool isConnected(const uint8_t*);
  75. // attempt to determine if the device at the given address is connected to the bus
  76. // also allows for updating the read scratchpad
  77. bool isConnected(const uint8_t*, uint8_t*);
  78. // read device's scratchpad
  79. bool readScratchPad(const uint8_t*, uint8_t*);
  80. // write device's scratchpad
  81. void writeScratchPad(const uint8_t*, const uint8_t*);
  82. // read device's power requirements
  83. bool readPowerSupply(const uint8_t* deviceAddress = nullptr);
  84. // get global resolution
  85. uint8_t getResolution();
  86. // set global resolution to 9, 10, 11, or 12 bits
  87. void setResolution(uint8_t);
  88. // returns the device resolution: 9, 10, 11, or 12 bits
  89. uint8_t getResolution(const uint8_t*);
  90. // set resolution of a device to 9, 10, 11, or 12 bits
  91. bool setResolution(const uint8_t*, uint8_t,
  92. bool skipGlobalBitResolutionCalculation = false);
  93. // sets/gets the waitForConversion flag
  94. void setWaitForConversion(bool);
  95. bool getWaitForConversion(void);
  96. // sets/gets the checkForConversion flag
  97. void setCheckForConversion(bool);
  98. bool getCheckForConversion(void);
  99. // sends command for all devices on the bus to perform a temperature conversion
  100. void requestTemperatures(void);
  101. // sends command for one device to perform a temperature conversion by address
  102. bool requestTemperaturesByAddress(const uint8_t*);
  103. // sends command for one device to perform a temperature conversion by index
  104. bool requestTemperaturesByIndex(uint8_t);
  105. // returns temperature raw value (12 bit integer of 1/128 degrees C)
  106. int16_t getTemp(const uint8_t*);
  107. // returns temperature in degrees C
  108. float getTempC(const uint8_t*);
  109. // returns temperature in degrees F
  110. float getTempF(const uint8_t*);
  111. // Get temperature for device index (slow)
  112. float getTempCByIndex(uint8_t);
  113. // Get temperature for device index (slow)
  114. float getTempFByIndex(uint8_t);
  115. // returns true if the bus requires parasite power
  116. bool isParasitePowerMode(void);
  117. // Is a conversion complete on the wire? Only applies to the first sensor on the wire.
  118. bool isConversionComplete(void);
  119. int16_t millisToWaitForConversion(uint8_t);
  120. // Sends command to one device to save values from scratchpad to EEPROM by index
  121. // Returns true if no errors were encountered, false indicates failure
  122. bool saveScratchPadByIndex(uint8_t);
  123. // Sends command to one or more devices to save values from scratchpad to EEPROM
  124. // Returns true if no errors were encountered, false indicates failure
  125. bool saveScratchPad(const uint8_t* = nullptr);
  126. // Sends command to one device to recall values from EEPROM to scratchpad by index
  127. // Returns true if no errors were encountered, false indicates failure
  128. bool recallScratchPadByIndex(uint8_t);
  129. // Sends command to one or more devices to recall values from EEPROM to scratchpad
  130. // Returns true if no errors were encountered, false indicates failure
  131. bool recallScratchPad(const uint8_t* = nullptr);
  132. // Sets the autoSaveScratchPad flag
  133. void setAutoSaveScratchPad(bool);
  134. // Gets the autoSaveScratchPad flag
  135. bool getAutoSaveScratchPad(void);
  136. #if REQUIRESALARMS
  137. typedef void AlarmHandler(const uint8_t*);
  138. // sets the high alarm temperature for a device
  139. // accepts a int8_t. valid range is -55C - 125C
  140. void setHighAlarmTemp(const uint8_t*, int8_t);
  141. // sets the low alarm temperature for a device
  142. // accepts a int8_t. valid range is -55C - 125C
  143. void setLowAlarmTemp(const uint8_t*, int8_t);
  144. // returns a int8_t with the current high alarm temperature for a device
  145. // in the range -55C - 125C
  146. int8_t getHighAlarmTemp(const uint8_t*);
  147. // returns a int8_t with the current low alarm temperature for a device
  148. // in the range -55C - 125C
  149. int8_t getLowAlarmTemp(const uint8_t*);
  150. // resets internal variables used for the alarm search
  151. void resetAlarmSearch(void);
  152. // search the wire for devices with active alarms
  153. bool alarmSearch(uint8_t*);
  154. // returns true if ia specific device has an alarm
  155. bool hasAlarm(const uint8_t*);
  156. // returns true if any device is reporting an alarm on the bus
  157. bool hasAlarm(void);
  158. // runs the alarm handler for all devices returned by alarmSearch()
  159. void processAlarms(void);
  160. // sets the alarm handler
  161. void setAlarmHandler(const AlarmHandler *);
  162. // returns true if an AlarmHandler has been set
  163. bool hasAlarmHandler();
  164. #endif
  165. // if no alarm handler is used the two bytes can be used as user data
  166. // example of such usage is an ID.
  167. // note if device is not connected it will fail writing the data.
  168. // note if address cannot be found no error will be reported.
  169. // in short use carefully
  170. void setUserData(const uint8_t*, int16_t);
  171. void setUserDataByIndex(uint8_t, int16_t);
  172. int16_t getUserData(const uint8_t*);
  173. int16_t getUserDataByIndex(uint8_t);
  174. // convert from Celsius to Fahrenheit
  175. static float toFahrenheit(float);
  176. // convert from Fahrenheit to Celsius
  177. static float toCelsius(float);
  178. // convert from raw to Celsius
  179. static float rawToCelsius(int16_t);
  180. // convert from raw to Fahrenheit
  181. static float rawToFahrenheit(int16_t);
  182. #if REQUIRESNEW
  183. // initialize memory area
  184. void* operator new (unsigned int);
  185. // delete memory reference
  186. void operator delete(void*);
  187. #endif
  188. private:
  189. typedef uint8_t ScratchPad[9];
  190. // parasite power on or off
  191. bool parasite;
  192. // external pullup
  193. bool useExternalPullup;
  194. uint8_t pullupPin;
  195. // used to determine the delay amount needed to allow for the
  196. // temperature conversion to take place
  197. uint8_t bitResolution;
  198. // used to requestTemperature with or without delay
  199. bool waitForConversion;
  200. // used to requestTemperature to dynamically check if a conversion is complete
  201. bool checkForConversion;
  202. // used to determine if values will be saved from scratchpad to EEPROM on every scratchpad write
  203. bool autoSaveScratchPad;
  204. // count of devices on the bus
  205. uint8_t devices;
  206. // count of DS18xxx Family devices on bus
  207. uint8_t ds18Count;
  208. // Take a pointer to one wire instance
  209. OneWire* _wire;
  210. // reads scratchpad and returns the raw temperature
  211. int16_t calculateTemperature(const uint8_t*, uint8_t*);
  212. void blockTillConversionComplete(uint8_t);
  213. // Returns true if all bytes of scratchPad are '\0'
  214. bool isAllZeros(const uint8_t* const scratchPad, const size_t length = 9);
  215. // External pullup control
  216. void activateExternalPullup(void);
  217. void deactivateExternalPullup(void);
  218. #if REQUIRESALARMS
  219. // required for alarmSearch
  220. uint8_t alarmSearchAddress[8];
  221. int8_t alarmSearchJunction;
  222. uint8_t alarmSearchExhausted;
  223. // the alarm handler function pointer
  224. AlarmHandler *_AlarmHandler;
  225. #endif
  226. };
  227. #endif