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.

276 lines
8.5 KiB

  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. void setGPIO(unsigned char gpio) {
  27. if (_gpio == gpio) return;
  28. _gpio = gpio;
  29. _dirty = true;
  30. }
  31. void setPullUp(bool pullup) {
  32. if (_pullup == pullup) return;
  33. _pullup = pullup;
  34. _dirty = true;
  35. }
  36. // ---------------------------------------------------------------------
  37. unsigned char getGPIO() {
  38. return _gpio;
  39. }
  40. bool getPullUp() {
  41. return _pullup;
  42. }
  43. // ---------------------------------------------------------------------
  44. // Sensor API
  45. // ---------------------------------------------------------------------
  46. // Initialization method, must be idempotent
  47. void begin() {
  48. if (!_dirty) return;
  49. _dirty = false;
  50. _interval = SENSOR_READ_INTERVAL / 2;
  51. // OneWire
  52. if (_wire) delete _wire;
  53. _wire = new OneWire(_gpio);
  54. // Must be done after the OneWire initialization
  55. if (_pullup) pinMode(_gpio, INPUT_PULLUP);
  56. // Search devices
  57. loadDevices();
  58. }
  59. // Loop-like method, call it in your main loop
  60. void tick() {
  61. static unsigned long last = 0;
  62. if (millis() - last < _interval) return;
  63. last = millis();
  64. // Every second we either start a conversion or read the scratchpad
  65. static bool conversion = true;
  66. if (conversion) {
  67. // Start conversion
  68. _wire->reset();
  69. _wire->skip();
  70. _wire->write(DS_CMD_START_CONVERSION, DS_PARASITE);
  71. } else {
  72. // Read scratchpads
  73. for (unsigned char index=0; index<_devices.size(); index++) {
  74. // Read scratchpad
  75. if (_wire->reset() == 0) {
  76. _error = DS_ERROR_FAILED_RESET;
  77. return;
  78. }
  79. _wire->select(_devices[index].address);
  80. _wire->write(DS_CMD_READ_SCRATCHPAD);
  81. uint8_t data[9];
  82. for (unsigned char i = 0; i < 9; i++) {
  83. data[i] = _wire->read();
  84. }
  85. #if false
  86. Serial.printf("[DS18B20] Data = ");
  87. for (unsigned char i = 0; i < 9; i++) {
  88. Serial.printf("%02X ", data[i]);
  89. }
  90. Serial.printf(" CRC = %02X\n", OneWire::crc8(data, 8));
  91. #endif
  92. if (_wire->reset() != 1) {
  93. _error = DS_ERROR_FAILED_READ;
  94. return;
  95. }
  96. if (OneWire::crc8(data, 8) != data[8]) {
  97. _error = SENSOR_ERROR_CRC;
  98. return;
  99. }
  100. memcpy(_devices[index].data, data, 9);
  101. }
  102. }
  103. conversion = !conversion;
  104. }
  105. // Descriptive name of the sensor
  106. String name() {
  107. char buffer[20];
  108. snprintf(buffer, sizeof(buffer), "Dallas @ GPIO%d", _gpio);
  109. return String(buffer);
  110. }
  111. // Descriptive name of the slot # index
  112. String slot(unsigned char index) {
  113. _error = SENSOR_ERROR_OK;
  114. if (index < _count) {
  115. char buffer[40];
  116. uint8_t * address = _devices[index].address;
  117. snprintf(buffer, sizeof(buffer), "%s (%02X%02X%02X%02X%02X%02X%02X%02X) @ GPIO%d",
  118. chipAsString(index).c_str(),
  119. address[0], address[1], address[2], address[3],
  120. address[4], address[5], address[6], address[7],
  121. _gpio
  122. );
  123. return String(buffer);
  124. }
  125. _error = SENSOR_ERROR_OUT_OF_RANGE;
  126. return String();
  127. }
  128. // Type for slot # index
  129. magnitude_t type(unsigned char index) {
  130. _error = SENSOR_ERROR_OK;
  131. if (index < _count) return MAGNITUDE_TEMPERATURE;
  132. _error = SENSOR_ERROR_OUT_OF_RANGE;
  133. return MAGNITUDE_NONE;
  134. }
  135. // Current value for slot # index
  136. double value(unsigned char index) {
  137. if (index >= _count) {
  138. _error = SENSOR_ERROR_OUT_OF_RANGE;
  139. return 0;
  140. }
  141. uint8_t * data = _devices[index].data;
  142. // Registers
  143. // byte 0: temperature LSB
  144. // byte 1: temperature MSB
  145. // byte 2: high alarm temp
  146. // byte 3: low alarm temp
  147. // byte 4: DS18S20: store for crc
  148. // DS18B20 & DS1822: configuration register
  149. // byte 5: internal use & crc
  150. // byte 6: DS18S20: COUNT_REMAIN
  151. // DS18B20 & DS1822: store for crc
  152. // byte 7: DS18S20: COUNT_PER_C
  153. // DS18B20 & DS1822: store for crc
  154. // byte 8: SCRATCHPAD_CRC
  155. int16_t raw = (data[1] << 8) | data[0];
  156. if (chip(index) == DS_CHIP_DS18S20) {
  157. raw = raw << 3; // 9 bit resolution default
  158. if (data[7] == 0x10) {
  159. raw = (raw & 0xFFF0) + 12 - data[6]; // "count remain" gives full 12 bit resolution
  160. }
  161. } else {
  162. byte cfg = (data[4] & 0x60);
  163. if (cfg == 0x00) raw = raw & ~7; // 9 bit res, 93.75 ms
  164. else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  165. else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  166. // 12 bit res, 750 ms
  167. }
  168. double value = (float) raw / 16.0;
  169. if (value == DS_DISCONNECTED) {
  170. _error = SENSOR_ERROR_CRC;
  171. return 0;
  172. }
  173. _error = SENSOR_ERROR_OK;
  174. return value;
  175. }
  176. protected:
  177. // ---------------------------------------------------------------------
  178. // Protected
  179. // ---------------------------------------------------------------------
  180. unsigned char chip(unsigned char index) {
  181. if (index < _count) return _devices[index].address[0];
  182. return 0;
  183. }
  184. String chipAsString(unsigned char index) {
  185. unsigned char chip_id = chip(index);
  186. if (chip_id == DS_CHIP_DS18S20) return String("DS18S20");
  187. if (chip_id == DS_CHIP_DS18B20) return String("DS18B20");
  188. if (chip_id == DS_CHIP_DS1822) return String("DS1822");
  189. if (chip_id == DS_CHIP_DS1825) return String("DS1825");
  190. return String("Unknown");
  191. }
  192. void loadDevices() {
  193. uint8_t address[8];
  194. _wire->reset_search();
  195. while (_wire->search(address)) {
  196. // Check CRC
  197. if (_wire->crc8(address, 7) == address[7]) {
  198. ds_device_t device;
  199. memcpy(device.address, address, 8);
  200. _devices.push_back(device);
  201. }
  202. }
  203. _count = _devices.size();
  204. }
  205. typedef struct {
  206. uint8_t address[8];
  207. uint8_t data[9];
  208. } ds_device_t;
  209. std::vector<ds_device_t> _devices;
  210. unsigned char _gpio;
  211. unsigned long _interval;
  212. bool _pullup;
  213. OneWire * _wire;
  214. };