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