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.

310 lines
9.7 KiB

6 years ago
6 years ago
6 years ago
  1. // -----------------------------------------------------------------------------
  2. // Dallas OneWire Sensor
  3. // Uses OneWire library
  4. // Copyright (C) 2017-2019 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. // Manage GPIO lock
  50. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  51. _previous = GPIO_NONE;
  52. if (!gpioGetLock(_gpio)) {
  53. _error = SENSOR_ERROR_GPIO_USED;
  54. return;
  55. }
  56. // OneWire
  57. if (_wire) delete _wire;
  58. _wire = new OneWire(_gpio);
  59. // Search devices
  60. loadDevices();
  61. // If no devices found check again pulling up the line
  62. if (_count == 0) {
  63. pinMode(_gpio, INPUT_PULLUP);
  64. loadDevices();
  65. }
  66. // Check connection
  67. if (_count == 0) {
  68. gpioReleaseLock(_gpio);
  69. } else {
  70. _previous = _gpio;
  71. }
  72. _ready = true;
  73. _dirty = false;
  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. // Force a CRC check error
  93. _devices[index].data[0] = _devices[index].data[0] + 1;
  94. return;
  95. }
  96. _wire->select(_devices[index].address);
  97. _wire->write(DS_CMD_READ_SCRATCHPAD);
  98. uint8_t data[DS_DATA_SIZE];
  99. for (unsigned char i = 0; i < DS_DATA_SIZE; i++) {
  100. data[i] = _wire->read();
  101. }
  102. if (_wire->reset() != 1) {
  103. // Force a CRC check error
  104. _devices[index].data[0] = _devices[index].data[0] + 1;
  105. return;
  106. }
  107. memcpy(_devices[index].data, data, DS_DATA_SIZE);
  108. }
  109. }
  110. conversion = !conversion;
  111. }
  112. // Descriptive name of the sensor
  113. String description() {
  114. char buffer[20];
  115. snprintf(buffer, sizeof(buffer), "Dallas @ GPIO%d", _gpio);
  116. return String(buffer);
  117. }
  118. // Address of the device
  119. String address(unsigned char index) {
  120. char buffer[20] = {0};
  121. if (index < _count) {
  122. uint8_t * address = _devices[index].address;
  123. snprintf(buffer, sizeof(buffer), "%02X%02X%02X%02X%02X%02X%02X%02X",
  124. address[0], address[1], address[2], address[3],
  125. address[4], address[5], address[6], address[7]
  126. );
  127. }
  128. return String(buffer);
  129. }
  130. // Descriptive name of the slot # index
  131. String slot(unsigned char index) {
  132. if (index < _count) {
  133. char buffer[40];
  134. uint8_t * address = _devices[index].address;
  135. snprintf(buffer, sizeof(buffer), "%s (%02X%02X%02X%02X%02X%02X%02X%02X) @ GPIO%d",
  136. chipAsString(index).c_str(),
  137. address[0], address[1], address[2], address[3],
  138. address[4], address[5], address[6], address[7],
  139. _gpio
  140. );
  141. return String(buffer);
  142. }
  143. return String();
  144. }
  145. // Type for slot # index
  146. unsigned char type(unsigned char index) {
  147. if (index < _count) return MAGNITUDE_TEMPERATURE;
  148. return MAGNITUDE_NONE;
  149. }
  150. // Pre-read hook (usually to populate registers with up-to-date data)
  151. void pre() {
  152. _error = SENSOR_ERROR_OK;
  153. }
  154. // Current value for slot # index
  155. double value(unsigned char index) {
  156. if (index >= _count) return 0;
  157. uint8_t * data = _devices[index].data;
  158. if (OneWire::crc8(data, DS_DATA_SIZE-1) != data[DS_DATA_SIZE-1]) {
  159. _error = SENSOR_ERROR_CRC;
  160. return 0;
  161. }
  162. // Registers
  163. // byte 0: temperature LSB
  164. // byte 1: temperature MSB
  165. // byte 2: high alarm temp
  166. // byte 3: low alarm temp
  167. // byte 4: DS18S20: store for crc
  168. // DS18B20 & DS1822: configuration register
  169. // byte 5: internal use & crc
  170. // byte 6: DS18S20: COUNT_REMAIN
  171. // DS18B20 & DS1822: store for crc
  172. // byte 7: DS18S20: COUNT_PER_C
  173. // DS18B20 & DS1822: store for crc
  174. // byte 8: SCRATCHPAD_CRC
  175. int16_t raw = (data[1] << 8) | data[0];
  176. if (chip(index) == DS_CHIP_DS18S20) {
  177. raw = raw << 3; // 9 bit resolution default
  178. if (data[7] == 0x10) {
  179. raw = (raw & 0xFFF0) + 12 - data[6]; // "count remain" gives full 12 bit resolution
  180. }
  181. } else {
  182. byte cfg = (data[4] & 0x60);
  183. if (cfg == 0x00) raw = raw & ~7; // 9 bit res, 93.75 ms
  184. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  185. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  186. // 12 bit res, 750 ms
  187. }
  188. double value = (float) raw / 16.0;
  189. if (value == DS_DISCONNECTED) {
  190. _error = SENSOR_ERROR_CRC;
  191. return 0;
  192. }
  193. return value;
  194. }
  195. protected:
  196. // ---------------------------------------------------------------------
  197. // Protected
  198. // ---------------------------------------------------------------------
  199. bool validateID(unsigned char id) {
  200. return (id == DS_CHIP_DS18S20) || (id == DS_CHIP_DS18B20) || (id == DS_CHIP_DS1822) || (id == DS_CHIP_DS1825);
  201. }
  202. unsigned char chip(unsigned char index) {
  203. if (index < _count) return _devices[index].address[0];
  204. return 0;
  205. }
  206. String chipAsString(unsigned char index) {
  207. unsigned char chip_id = chip(index);
  208. if (chip_id == DS_CHIP_DS18S20) return String("DS18S20");
  209. if (chip_id == DS_CHIP_DS18B20) return String("DS18B20");
  210. if (chip_id == DS_CHIP_DS1822) return String("DS1822");
  211. if (chip_id == DS_CHIP_DS1825) return String("DS1825");
  212. return String("Unknown");
  213. }
  214. void loadDevices() {
  215. uint8_t address[8];
  216. _wire->reset();
  217. _wire->reset_search();
  218. while (_wire->search(address)) {
  219. // Check CRC
  220. if (_wire->crc8(address, 7) == address[7]) {
  221. // Check ID
  222. if (validateID(address[0])) {
  223. ds_device_t device;
  224. memcpy(device.address, address, 8);
  225. _devices.push_back(device);
  226. }
  227. }
  228. }
  229. _count = _devices.size();
  230. }
  231. typedef struct {
  232. uint8_t address[8];
  233. uint8_t data[DS_DATA_SIZE];
  234. } ds_device_t;
  235. std::vector<ds_device_t> _devices;
  236. unsigned char _gpio = GPIO_NONE;
  237. unsigned char _previous = GPIO_NONE;
  238. OneWire * _wire = NULL;
  239. };
  240. #endif // SENSOR_SUPPORT && DALLAS_SUPPORT