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.

319 lines
10 KiB

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