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.

303 lines
9.4 KiB

7 years ago
7 years ago
7 years ago
  1. // -----------------------------------------------------------------------------
  2. // Dallas OneWire Sensor
  3. // Uses OneWire library
  4. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  5. // -----------------------------------------------------------------------------
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #include <vector>
  10. #include <OneWire.h>
  11. #define DS_CHIP_DS18S20 0x10
  12. #define DS_CHIP_DS1822 0x22
  13. #define DS_CHIP_DS18B20 0x28
  14. #define DS_CHIP_DS1825 0x3B
  15. #define DS_PARASITE 1
  16. #define DS_DISCONNECTED -127
  17. #define DS_CMD_START_CONVERSION 0x44
  18. #define DS_CMD_READ_SCRATCHPAD 0xBE
  19. #define DS_ERROR_FAILED_RESET -2
  20. #define DS_ERROR_FAILED_READ -3
  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 != 0xFF) 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 != 0xFF) gpioReleaseLock(_previous);
  52. _previous = 0xFF;
  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. _error = DS_ERROR_FAILED_RESET;
  92. return;
  93. }
  94. _wire->select(_devices[index].address);
  95. _wire->write(DS_CMD_READ_SCRATCHPAD);
  96. uint8_t data[9];
  97. for (unsigned char i = 0; i < 9; i++) {
  98. data[i] = _wire->read();
  99. }
  100. #if false
  101. Serial.printf("[DS18B20] Data = ");
  102. for (unsigned char i = 0; i < 9; i++) {
  103. Serial.printf("%02X ", data[i]);
  104. }
  105. Serial.printf(" CRC = %02X\n", OneWire::crc8(data, 8));
  106. #endif
  107. if (_wire->reset() != 1) {
  108. _error = DS_ERROR_FAILED_READ;
  109. return;
  110. }
  111. if (OneWire::crc8(data, 8) != data[8]) {
  112. _error = SENSOR_ERROR_CRC;
  113. return;
  114. }
  115. memcpy(_devices[index].data, data, 9);
  116. }
  117. }
  118. conversion = !conversion;
  119. }
  120. // Descriptive name of the sensor
  121. String description() {
  122. char buffer[20];
  123. snprintf(buffer, sizeof(buffer), "Dallas @ GPIO%d", _gpio);
  124. return String(buffer);
  125. }
  126. // Descriptive name of the slot # index
  127. String slot(unsigned char index) {
  128. _error = SENSOR_ERROR_OK;
  129. if (index < _count) {
  130. char buffer[40];
  131. uint8_t * address = _devices[index].address;
  132. snprintf(buffer, sizeof(buffer), "%s (%02X%02X%02X%02X%02X%02X%02X%02X) @ GPIO%d",
  133. chipAsString(index).c_str(),
  134. address[0], address[1], address[2], address[3],
  135. address[4], address[5], address[6], address[7],
  136. _gpio
  137. );
  138. return String(buffer);
  139. }
  140. _error = SENSOR_ERROR_OUT_OF_RANGE;
  141. return String();
  142. }
  143. // Type for slot # index
  144. magnitude_t type(unsigned char index) {
  145. _error = SENSOR_ERROR_OK;
  146. if (index < _count) return MAGNITUDE_TEMPERATURE;
  147. _error = SENSOR_ERROR_OUT_OF_RANGE;
  148. return MAGNITUDE_NONE;
  149. }
  150. // Current value for slot # index
  151. double value(unsigned char index) {
  152. if (index >= _count) {
  153. _error = SENSOR_ERROR_OUT_OF_RANGE;
  154. return 0;
  155. }
  156. uint8_t * data = _devices[index].data;
  157. // Registers
  158. // byte 0: temperature LSB
  159. // byte 1: temperature MSB
  160. // byte 2: high alarm temp
  161. // byte 3: low alarm temp
  162. // byte 4: DS18S20: store for crc
  163. // DS18B20 & DS1822: configuration register
  164. // byte 5: internal use & crc
  165. // byte 6: DS18S20: COUNT_REMAIN
  166. // DS18B20 & DS1822: store for crc
  167. // byte 7: DS18S20: COUNT_PER_C
  168. // DS18B20 & DS1822: store for crc
  169. // byte 8: SCRATCHPAD_CRC
  170. int16_t raw = (data[1] << 8) | data[0];
  171. if (chip(index) == DS_CHIP_DS18S20) {
  172. raw = raw << 3; // 9 bit resolution default
  173. if (data[7] == 0x10) {
  174. raw = (raw & 0xFFF0) + 12 - data[6]; // "count remain" gives full 12 bit resolution
  175. }
  176. } else {
  177. byte cfg = (data[4] & 0x60);
  178. if (cfg == 0x00) raw = raw & ~7; // 9 bit res, 93.75 ms
  179. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  180. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  181. // 12 bit res, 750 ms
  182. }
  183. double value = (float) raw / 16.0;
  184. if (value == DS_DISCONNECTED) {
  185. _error = SENSOR_ERROR_CRC;
  186. return 0;
  187. }
  188. _error = SENSOR_ERROR_OK;
  189. return value;
  190. }
  191. protected:
  192. // ---------------------------------------------------------------------
  193. // Protected
  194. // ---------------------------------------------------------------------
  195. bool validateID(unsigned char id) {
  196. return (id == DS_CHIP_DS18S20) || (id == DS_CHIP_DS18B20) || (id == DS_CHIP_DS1822) || (id == DS_CHIP_DS1825);
  197. }
  198. unsigned char chip(unsigned char index) {
  199. if (index < _count) return _devices[index].address[0];
  200. return 0;
  201. }
  202. String chipAsString(unsigned char index) {
  203. unsigned char chip_id = chip(index);
  204. if (chip_id == DS_CHIP_DS18S20) return String("DS18S20");
  205. if (chip_id == DS_CHIP_DS18B20) return String("DS18B20");
  206. if (chip_id == DS_CHIP_DS1822) return String("DS1822");
  207. if (chip_id == DS_CHIP_DS1825) return String("DS1825");
  208. return String("Unknown");
  209. }
  210. void loadDevices() {
  211. uint8_t address[8];
  212. _wire->reset();
  213. _wire->reset_search();
  214. while (_wire->search(address)) {
  215. // Check CRC
  216. if (_wire->crc8(address, 7) == address[7]) {
  217. // Check ID
  218. if (validateID(address[0])) {
  219. ds_device_t device;
  220. memcpy(device.address, address, 8);
  221. _devices.push_back(device);
  222. }
  223. }
  224. }
  225. _count = _devices.size();
  226. }
  227. typedef struct {
  228. uint8_t address[8];
  229. uint8_t data[9];
  230. } ds_device_t;
  231. std::vector<ds_device_t> _devices;
  232. unsigned char _gpio;
  233. unsigned char _previous = 0xFF;
  234. OneWire * _wire = NULL;
  235. };