Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
10 KiB

7 years ago
7 years ago
7 years ago
  1. // -----------------------------------------------------------------------------
  2. // Dallas OneWire Sensor
  3. // Uses OneWire library
  4. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && DALLAS_SUPPORT
  7. #pragma once
  8. #include "Arduino.h"
  9. #include "BaseSensor.h"
  10. #include <vector>
  11. #include <OneWire.h>
  12. #define DS_CHIP_DS18S20 0x10
  13. #define DS_CHIP_DS1822 0x22
  14. #define DS_CHIP_DS18B20 0x28
  15. #define DS_CHIP_DS1825 0x3B
  16. #define DS_PARASITE 1
  17. #define DS_DISCONNECTED -127
  18. #define DS_CMD_START_CONVERSION 0x44
  19. #define DS_CMD_READ_SCRATCHPAD 0xBE
  20. #define DS_ERROR_FAILED_RESET -2
  21. #define DS_ERROR_FAILED_READ -3
  22. class DallasSensor : public BaseSensor {
  23. public:
  24. // ---------------------------------------------------------------------
  25. // Public
  26. // ---------------------------------------------------------------------
  27. DallasSensor(): BaseSensor() {
  28. _sensor_id = SENSOR_DALLAS_ID;
  29. }
  30. ~DallasSensor() {
  31. if (_wire) delete _wire;
  32. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  33. }
  34. // ---------------------------------------------------------------------
  35. void setGPIO(unsigned char gpio) {
  36. if (_gpio == gpio) return;
  37. _gpio = gpio;
  38. _dirty = true;
  39. }
  40. // ---------------------------------------------------------------------
  41. unsigned char getGPIO() {
  42. return _gpio;
  43. }
  44. // ---------------------------------------------------------------------
  45. // Sensor API
  46. // ---------------------------------------------------------------------
  47. // Initialization method, must be idempotent
  48. void begin() {
  49. if (!_dirty) return;
  50. _dirty = false;
  51. // Manage GPIO lock
  52. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  53. _previous = GPIO_NONE;
  54. if (!gpioGetLock(_gpio)) {
  55. _error = SENSOR_ERROR_GPIO_USED;
  56. return;
  57. }
  58. // OneWire
  59. if (_wire) delete _wire;
  60. _wire = new OneWire(_gpio);
  61. // Search devices
  62. loadDevices();
  63. // If no devices found check again pulling up the line
  64. if (_count == 0) {
  65. pinMode(_gpio, INPUT_PULLUP);
  66. loadDevices();
  67. }
  68. // Check connection
  69. if (_count == 0) {
  70. gpioReleaseLock(_gpio);
  71. } else {
  72. _previous = _gpio;
  73. }
  74. }
  75. // Loop-like method, call it in your main loop
  76. void tick() {
  77. static unsigned long last = 0;
  78. if (millis() - last < DALLAS_READ_INTERVAL) return;
  79. last = millis();
  80. // Every second we either start a conversion or read the scratchpad
  81. static bool conversion = true;
  82. if (conversion) {
  83. // Start conversion
  84. _wire->reset();
  85. _wire->skip();
  86. _wire->write(DS_CMD_START_CONVERSION, DS_PARASITE);
  87. } else {
  88. // Read scratchpads
  89. for (unsigned char index=0; index<_devices.size(); index++) {
  90. // Read scratchpad
  91. if (_wire->reset() == 0) {
  92. _error = DS_ERROR_FAILED_RESET;
  93. return;
  94. }
  95. _wire->select(_devices[index].address);
  96. _wire->write(DS_CMD_READ_SCRATCHPAD);
  97. uint8_t data[9];
  98. for (unsigned char i = 0; i < 9; i++) {
  99. data[i] = _wire->read();
  100. }
  101. #if false
  102. Serial.printf("[DS18B20] Data = ");
  103. for (unsigned char i = 0; i < 9; i++) {
  104. Serial.printf("%02X ", data[i]);
  105. }
  106. Serial.printf(" CRC = %02X\n", OneWire::crc8(data, 8));
  107. #endif
  108. if (_wire->reset() != 1) {
  109. _error = DS_ERROR_FAILED_READ;
  110. return;
  111. }
  112. if (OneWire::crc8(data, 8) != data[8]) {
  113. _error = SENSOR_ERROR_CRC;
  114. return;
  115. }
  116. memcpy(_devices[index].data, data, 9);
  117. }
  118. }
  119. conversion = !conversion;
  120. }
  121. // Descriptive name of the sensor
  122. String description() {
  123. char buffer[20];
  124. snprintf(buffer, sizeof(buffer), "Dallas @ GPIO%d", _gpio);
  125. return String(buffer);
  126. }
  127. // Address of the device
  128. String address(unsigned char index) {
  129. char buffer[20] = {0};
  130. if (index < _count) {
  131. uint8_t * address = _devices[index].address;
  132. snprintf(buffer, sizeof(buffer), "%02X%02X%02X%02X%02X%02X%02X%02X",
  133. address[0], address[1], address[2], address[3],
  134. address[4], address[5], address[6], address[7]
  135. );
  136. }
  137. return String(buffer);
  138. }
  139. // Descriptive name of the slot # index
  140. String slot(unsigned char index) {
  141. _error = SENSOR_ERROR_OK;
  142. if (index < _count) {
  143. char buffer[40];
  144. uint8_t * address = _devices[index].address;
  145. snprintf(buffer, sizeof(buffer), "%s (%02X%02X%02X%02X%02X%02X%02X%02X) @ GPIO%d",
  146. chipAsString(index).c_str(),
  147. address[0], address[1], address[2], address[3],
  148. address[4], address[5], address[6], address[7],
  149. _gpio
  150. );
  151. return String(buffer);
  152. }
  153. _error = SENSOR_ERROR_OUT_OF_RANGE;
  154. return String();
  155. }
  156. // Type for slot # index
  157. unsigned char type(unsigned char index) {
  158. _error = SENSOR_ERROR_OK;
  159. if (index < _count) return MAGNITUDE_TEMPERATURE;
  160. _error = SENSOR_ERROR_OUT_OF_RANGE;
  161. return MAGNITUDE_NONE;
  162. }
  163. // Pre-read hook (usually to populate registers with up-to-date data)
  164. void pre() {
  165. _error = SENSOR_ERROR_OK;
  166. }
  167. // Current value for slot # index
  168. double value(unsigned char index) {
  169. if (index >= _count) {
  170. _error = SENSOR_ERROR_OUT_OF_RANGE;
  171. return 0;
  172. }
  173. uint8_t * data = _devices[index].data;
  174. // Registers
  175. // byte 0: temperature LSB
  176. // byte 1: temperature MSB
  177. // byte 2: high alarm temp
  178. // byte 3: low alarm temp
  179. // byte 4: DS18S20: store for crc
  180. // DS18B20 & DS1822: configuration register
  181. // byte 5: internal use & crc
  182. // byte 6: DS18S20: COUNT_REMAIN
  183. // DS18B20 & DS1822: store for crc
  184. // byte 7: DS18S20: COUNT_PER_C
  185. // DS18B20 & DS1822: store for crc
  186. // byte 8: SCRATCHPAD_CRC
  187. int16_t raw = (data[1] << 8) | data[0];
  188. if (chip(index) == DS_CHIP_DS18S20) {
  189. raw = raw << 3; // 9 bit resolution default
  190. if (data[7] == 0x10) {
  191. raw = (raw & 0xFFF0) + 12 - data[6]; // "count remain" gives full 12 bit resolution
  192. }
  193. } else {
  194. byte cfg = (data[4] & 0x60);
  195. if (cfg == 0x00) raw = raw & ~7; // 9 bit res, 93.75 ms
  196. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  197. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  198. // 12 bit res, 750 ms
  199. }
  200. double value = (float) raw / 16.0;
  201. if (value == DS_DISCONNECTED) {
  202. _error = SENSOR_ERROR_CRC;
  203. return 0;
  204. }
  205. _error = SENSOR_ERROR_OK;
  206. return value;
  207. }
  208. protected:
  209. // ---------------------------------------------------------------------
  210. // Protected
  211. // ---------------------------------------------------------------------
  212. bool validateID(unsigned char id) {
  213. return (id == DS_CHIP_DS18S20) || (id == DS_CHIP_DS18B20) || (id == DS_CHIP_DS1822) || (id == DS_CHIP_DS1825);
  214. }
  215. unsigned char chip(unsigned char index) {
  216. if (index < _count) return _devices[index].address[0];
  217. return 0;
  218. }
  219. String chipAsString(unsigned char index) {
  220. unsigned char chip_id = chip(index);
  221. if (chip_id == DS_CHIP_DS18S20) return String("DS18S20");
  222. if (chip_id == DS_CHIP_DS18B20) return String("DS18B20");
  223. if (chip_id == DS_CHIP_DS1822) return String("DS1822");
  224. if (chip_id == DS_CHIP_DS1825) return String("DS1825");
  225. return String("Unknown");
  226. }
  227. void loadDevices() {
  228. uint8_t address[8];
  229. _wire->reset();
  230. _wire->reset_search();
  231. while (_wire->search(address)) {
  232. // Check CRC
  233. if (_wire->crc8(address, 7) == address[7]) {
  234. // Check ID
  235. if (validateID(address[0])) {
  236. ds_device_t device;
  237. memcpy(device.address, address, 8);
  238. _devices.push_back(device);
  239. }
  240. }
  241. }
  242. _count = _devices.size();
  243. }
  244. typedef struct {
  245. uint8_t address[8];
  246. uint8_t data[9];
  247. } ds_device_t;
  248. std::vector<ds_device_t> _devices;
  249. unsigned char _gpio = GPIO_NONE;
  250. unsigned char _previous = GPIO_NONE;
  251. OneWire * _wire = NULL;
  252. };
  253. #endif // SENSOR_SUPPORT && DALLAS_SUPPORT