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.

288 lines
8.9 KiB

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