DallasTemperature.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 -16128
  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. static uint16_t millisToWaitForConversion(uint8_t);
  120. uint16_t millisToWaitForConversion();
  121. // Sends command to one device to save values from scratchpad to EEPROM by index
  122. // Returns true if no errors were encountered, false indicates failure
  123. bool saveScratchPadByIndex(uint8_t);
  124. // Sends command to one or more devices to save values from scratchpad to EEPROM
  125. // Returns true if no errors were encountered, false indicates failure
  126. bool saveScratchPad(const uint8_t* = nullptr);
  127. // Sends command to one device to recall values from EEPROM to scratchpad by index
  128. // Returns true if no errors were encountered, false indicates failure
  129. bool recallScratchPadByIndex(uint8_t);
  130. // Sends command to one or more devices to recall values from EEPROM to scratchpad
  131. // Returns true if no errors were encountered, false indicates failure
  132. bool recallScratchPad(const uint8_t* = nullptr);
  133. // Sets the autoSaveScratchPad flag
  134. void setAutoSaveScratchPad(bool);
  135. // Gets the autoSaveScratchPad flag
  136. bool getAutoSaveScratchPad(void);
  137. #if REQUIRESALARMS
  138. typedef void AlarmHandler(const uint8_t*);
  139. // sets the high alarm temperature for a device
  140. // accepts a int8_t. valid range is -55C - 125C
  141. void setHighAlarmTemp(const uint8_t*, int8_t);
  142. // sets the low alarm temperature for a device
  143. // accepts a int8_t. valid range is -55C - 125C
  144. void setLowAlarmTemp(const uint8_t*, int8_t);
  145. // returns a int8_t with the current high alarm temperature for a device
  146. // in the range -55C - 125C
  147. int8_t getHighAlarmTemp(const uint8_t*);
  148. // returns a int8_t with the current low alarm temperature for a device
  149. // in the range -55C - 125C
  150. int8_t getLowAlarmTemp(const uint8_t*);
  151. // resets internal variables used for the alarm search
  152. void resetAlarmSearch(void);
  153. // search the wire for devices with active alarms
  154. bool alarmSearch(uint8_t*);
  155. // returns true if ia specific device has an alarm
  156. bool hasAlarm(const uint8_t*);
  157. // returns true if any device is reporting an alarm on the bus
  158. bool hasAlarm(void);
  159. // runs the alarm handler for all devices returned by alarmSearch()
  160. void processAlarms(void);
  161. // sets the alarm handler
  162. void setAlarmHandler(const AlarmHandler *);
  163. // returns true if an AlarmHandler has been set
  164. bool hasAlarmHandler();
  165. #endif
  166. // if no alarm handler is used the two bytes can be used as user data
  167. // example of such usage is an ID.
  168. // note if device is not connected it will fail writing the data.
  169. // note if address cannot be found no error will be reported.
  170. // in short use carefully
  171. void setUserData(const uint8_t*, int16_t);
  172. void setUserDataByIndex(uint8_t, int16_t);
  173. int16_t getUserData(const uint8_t*);
  174. int16_t getUserDataByIndex(uint8_t);
  175. // convert from Celsius to Fahrenheit
  176. static float toFahrenheit(float);
  177. // convert from Fahrenheit to Celsius
  178. static float toCelsius(float);
  179. // convert from raw to Celsius
  180. static float rawToCelsius(int16_t);
  181. // convert from Celsius to raw
  182. static int16_t celsiusToRaw(float);
  183. // convert from raw to Fahrenheit
  184. static float rawToFahrenheit(int16_t);
  185. #if REQUIRESNEW
  186. // initialize memory area
  187. void* operator new (unsigned int);
  188. // delete memory reference
  189. void operator delete(void*);
  190. #endif
  191. void blockTillConversionComplete(uint8_t);
  192. private:
  193. typedef uint8_t ScratchPad[9];
  194. // parasite power on or off
  195. bool parasite;
  196. // external pullup
  197. bool useExternalPullup;
  198. uint8_t pullupPin;
  199. // used to determine the delay amount needed to allow for the
  200. // temperature conversion to take place
  201. uint8_t bitResolution;
  202. // used to requestTemperature with or without delay
  203. bool waitForConversion;
  204. // used to requestTemperature to dynamically check if a conversion is complete
  205. bool checkForConversion;
  206. // used to determine if values will be saved from scratchpad to EEPROM on every scratchpad write
  207. bool autoSaveScratchPad;
  208. // count of devices on the bus
  209. uint8_t devices;
  210. // count of DS18xxx Family devices on bus
  211. uint8_t ds18Count;
  212. // Take a pointer to one wire instance
  213. OneWire* _wire;
  214. // reads scratchpad and returns the raw temperature
  215. int16_t calculateTemperature(const uint8_t*, uint8_t*);
  216. // Returns true if all bytes of scratchPad are '\0'
  217. bool isAllZeros(const uint8_t* const scratchPad, const size_t length = 9);
  218. // External pullup control
  219. void activateExternalPullup(void);
  220. void deactivateExternalPullup(void);
  221. #if REQUIRESALARMS
  222. // required for alarmSearch
  223. uint8_t alarmSearchAddress[8];
  224. int8_t alarmSearchJunction;
  225. uint8_t alarmSearchExhausted;
  226. // the alarm handler function pointer
  227. AlarmHandler *_AlarmHandler;
  228. #endif
  229. };
  230. #endif