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.

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