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.

320 lines
10 KiB

6 years ago
6 years ago
6 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. // Current value for slot # index
  164. double value(unsigned char index) {
  165. if (index >= _count) {
  166. _error = SENSOR_ERROR_OUT_OF_RANGE;
  167. return 0;
  168. }
  169. uint8_t * data = _devices[index].data;
  170. // Registers
  171. // byte 0: temperature LSB
  172. // byte 1: temperature MSB
  173. // byte 2: high alarm temp
  174. // byte 3: low alarm temp
  175. // byte 4: DS18S20: store for crc
  176. // DS18B20 & DS1822: configuration register
  177. // byte 5: internal use & crc
  178. // byte 6: DS18S20: COUNT_REMAIN
  179. // DS18B20 & DS1822: store for crc
  180. // byte 7: DS18S20: COUNT_PER_C
  181. // DS18B20 & DS1822: store for crc
  182. // byte 8: SCRATCHPAD_CRC
  183. int16_t raw = (data[1] << 8) | data[0];
  184. if (chip(index) == DS_CHIP_DS18S20) {
  185. raw = raw << 3; // 9 bit resolution default
  186. if (data[7] == 0x10) {
  187. raw = (raw & 0xFFF0) + 12 - data[6]; // "count remain" gives full 12 bit resolution
  188. }
  189. } else {
  190. byte cfg = (data[4] & 0x60);
  191. if (cfg == 0x00) raw = raw & ~7; // 9 bit res, 93.75 ms
  192. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  193. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  194. // 12 bit res, 750 ms
  195. }
  196. double value = (float) raw / 16.0;
  197. if (value == DS_DISCONNECTED) {
  198. _error = SENSOR_ERROR_CRC;
  199. return 0;
  200. }
  201. _error = SENSOR_ERROR_OK;
  202. return value;
  203. }
  204. protected:
  205. // ---------------------------------------------------------------------
  206. // Protected
  207. // ---------------------------------------------------------------------
  208. bool validateID(unsigned char id) {
  209. return (id == DS_CHIP_DS18S20) || (id == DS_CHIP_DS18B20) || (id == DS_CHIP_DS1822) || (id == DS_CHIP_DS1825);
  210. }
  211. unsigned char chip(unsigned char index) {
  212. if (index < _count) return _devices[index].address[0];
  213. return 0;
  214. }
  215. String chipAsString(unsigned char index) {
  216. unsigned char chip_id = chip(index);
  217. if (chip_id == DS_CHIP_DS18S20) return String("DS18S20");
  218. if (chip_id == DS_CHIP_DS18B20) return String("DS18B20");
  219. if (chip_id == DS_CHIP_DS1822) return String("DS1822");
  220. if (chip_id == DS_CHIP_DS1825) return String("DS1825");
  221. return String("Unknown");
  222. }
  223. void loadDevices() {
  224. uint8_t address[8];
  225. _wire->reset();
  226. _wire->reset_search();
  227. while (_wire->search(address)) {
  228. // Check CRC
  229. if (_wire->crc8(address, 7) == address[7]) {
  230. // Check ID
  231. if (validateID(address[0])) {
  232. ds_device_t device;
  233. memcpy(device.address, address, 8);
  234. _devices.push_back(device);
  235. }
  236. }
  237. }
  238. _count = _devices.size();
  239. }
  240. typedef struct {
  241. uint8_t address[8];
  242. uint8_t data[9];
  243. } ds_device_t;
  244. std::vector<ds_device_t> _devices;
  245. unsigned char _gpio = GPIO_NONE;
  246. unsigned char _previous = GPIO_NONE;
  247. OneWire * _wire = NULL;
  248. };
  249. #endif // SENSOR_SUPPORT && DALLAS_SUPPORT