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
8.6 KiB

providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
  1. // -----------------------------------------------------------------------------
  2. // DHTXX Sensor
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && DHT_SUPPORT
  6. #pragma once
  7. #include <Arduino.h>
  8. #include "../gpio.h"
  9. #include "../utils.h"
  10. #include "BaseSensor.h"
  11. constexpr const double DHT_DUMMY_VALUE = -255;
  12. constexpr const size_t DHT_MAX_DATA = 5;
  13. constexpr const size_t DHT_MAX_ERRORS = 5;
  14. constexpr const uint32_t DHT_MIN_INTERVAL = 2000;
  15. enum class DHTChipType {
  16. DHT11,
  17. DHT12,
  18. DHT21,
  19. DHT22,
  20. AM2301,
  21. SI7021
  22. };
  23. // Note: backwards compatibility for configuration headers
  24. #define DHT_CHIP_DHT11 DHTChipType::DHT11
  25. #define DHT_CHIP_DHT12 DHTChipType::DHT12
  26. #define DHT_CHIP_DHT22 DHTChipType::DHT22
  27. #define DHT_CHIP_DHT21 DHTChipType::DHT21
  28. #define DHT_CHIP_AM2301 DHTChipType::AM2301
  29. #define DHT_CHIP_SI7021 DHTChipType::SI7021
  30. int dhtchip_to_number(DHTChipType chip) {
  31. switch (chip) {
  32. case DHTChipType::DHT11:
  33. return 11;
  34. case DHTChipType::DHT12:
  35. return 12;
  36. case DHTChipType::DHT21:
  37. case DHTChipType::AM2301:
  38. return 21;
  39. case DHTChipType::DHT22:
  40. case DHTChipType::SI7021:
  41. return 22;
  42. default:
  43. return -1;
  44. }
  45. }
  46. class DHTSensor : public BaseSensor {
  47. public:
  48. // ---------------------------------------------------------------------
  49. // Public
  50. // ---------------------------------------------------------------------
  51. DHTSensor() {
  52. _count = 2;
  53. _sensor_id = SENSOR_DHTXX_ID;
  54. }
  55. ~DHTSensor() {
  56. gpioUnlock(_gpio);
  57. }
  58. // ---------------------------------------------------------------------
  59. void setGPIO(unsigned char gpio) {
  60. _gpio = gpio;
  61. }
  62. void setType(DHTChipType type) {
  63. _type = type;
  64. }
  65. // ---------------------------------------------------------------------
  66. unsigned char getGPIO() {
  67. return _gpio;
  68. }
  69. int getType() {
  70. return dhtchip_to_number(_type);
  71. }
  72. DHTChipType getChipType() {
  73. return _type;
  74. }
  75. // ---------------------------------------------------------------------
  76. // Sensor API
  77. // ---------------------------------------------------------------------
  78. // Initialization method, must be idempotent
  79. void begin() {
  80. _count = 0;
  81. // Manage GPIO lock (note that this only handles the basic *hw* I/O)
  82. if (_previous != GPIO_NONE) {
  83. gpioUnlock(_previous);
  84. }
  85. _previous = GPIO_NONE;
  86. if (!gpioLock(_gpio)) {
  87. _error = SENSOR_ERROR_GPIO_USED;
  88. return;
  89. }
  90. _previous = _gpio;
  91. // Set now to fail the check in _read at least once
  92. _last_ok = millis();
  93. _count = 2;
  94. _ready = true;
  95. }
  96. // Pre-read hook (usually to populate registers with up-to-date data)
  97. void pre() {
  98. _error = SENSOR_ERROR_OK;
  99. _read();
  100. }
  101. // Descriptive name of the sensor
  102. String description() {
  103. char buffer[20];
  104. snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", dhtchip_to_number(_type), _gpio);
  105. return String(buffer);
  106. }
  107. // Descriptive name of the slot # index
  108. String description(unsigned char index) {
  109. return description();
  110. };
  111. // Address of the sensor (it could be the GPIO or I2C address)
  112. String address(unsigned char index) {
  113. return String(_gpio);
  114. }
  115. // Type for slot # index
  116. unsigned char type(unsigned char index) {
  117. if (index == 0) return MAGNITUDE_TEMPERATURE;
  118. if (index == 1) return MAGNITUDE_HUMIDITY;
  119. return MAGNITUDE_NONE;
  120. }
  121. // Current value for slot # index
  122. double value(unsigned char index) {
  123. if (index == 0) return _temperature;
  124. if (index == 1) return _humidity;
  125. return 0;
  126. }
  127. protected:
  128. // ---------------------------------------------------------------------
  129. // Protected
  130. // ---------------------------------------------------------------------
  131. void _read() {
  132. if ((_last_ok > 0) && (millis() - _last_ok < DHT_MIN_INTERVAL)) {
  133. if ((_temperature == DHT_DUMMY_VALUE) && (_humidity == DHT_DUMMY_VALUE)) {
  134. _error = SENSOR_ERROR_WARM_UP;
  135. } else {
  136. _error = SENSOR_ERROR_OK;
  137. }
  138. return;
  139. }
  140. unsigned long low = 0;
  141. unsigned long high = 0;
  142. unsigned char dhtData[DHT_MAX_DATA] = {0};
  143. unsigned char byteInx = 0;
  144. unsigned char bitInx = 7;
  145. pinMode(_gpio, OUTPUT);
  146. // Send start signal to DHT sensor
  147. if (++_errors > DHT_MAX_ERRORS) {
  148. _errors = 0;
  149. digitalWrite(_gpio, HIGH);
  150. nice_delay(250);
  151. }
  152. noInterrupts();
  153. digitalWrite(_gpio, LOW);
  154. if ((_type == DHT_CHIP_DHT11) || (_type == DHT_CHIP_DHT12)) {
  155. nice_delay(20);
  156. } else if (_type == DHT_CHIP_SI7021) {
  157. delayMicroseconds(500);
  158. } else {
  159. delayMicroseconds(1100);
  160. }
  161. digitalWrite(_gpio, HIGH);
  162. delayMicroseconds(40);
  163. pinMode(_gpio, INPUT_PULLUP);
  164. delayMicroseconds(10);
  165. // No errors, read the 40 data bits
  166. for( int k = 0; k < 41; k++ ) {
  167. // Starts new data transmission with >50us low signal
  168. low = _signal(100, LOW);
  169. if (low == 0) {
  170. _error = SENSOR_ERROR_TIMEOUT;
  171. return;
  172. }
  173. // Check to see if after >70us rx data is a 0 or a 1
  174. high = _signal(100, HIGH);
  175. if (high == 0) {
  176. _error = SENSOR_ERROR_TIMEOUT;
  177. return;
  178. }
  179. // Skip the first bit
  180. if (k == 0) continue;
  181. // add the current read to the output data
  182. // since all dhtData array where set to 0 at the start,
  183. // only look for "1" (>28us us)
  184. if (high > low) dhtData[byteInx] |= (1 << bitInx);
  185. // index to next byte
  186. if (bitInx == 0) {
  187. bitInx = 7;
  188. ++byteInx;
  189. } else {
  190. --bitInx;
  191. }
  192. }
  193. interrupts();
  194. // Verify checksum
  195. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  196. _error = SENSOR_ERROR_CRC;
  197. return;
  198. }
  199. // Get humidity from Data[0] and Data[1]
  200. if (_type == DHT_CHIP_DHT11) {
  201. _humidity = dhtData[0];
  202. } else if (_type == DHT_CHIP_DHT12) {
  203. _humidity = dhtData[0];
  204. _humidity += dhtData[1] * 0.1;
  205. } else {
  206. _humidity = dhtData[0] * 256 + dhtData[1];
  207. _humidity /= 10;
  208. }
  209. // Get temp from Data[2] and Data[3]
  210. if (_type == DHT_CHIP_DHT11) {
  211. _temperature = dhtData[2];
  212. } else if (_type == DHT_CHIP_DHT12) {
  213. _temperature = (dhtData[2] & 0x7F);
  214. _temperature += dhtData[3] * 0.1;
  215. if (dhtData[2] & 0x80) _temperature *= -1;
  216. } else {
  217. _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  218. _temperature /= 10;
  219. if (dhtData[2] & 0x80) _temperature *= -1;
  220. }
  221. _last_ok = millis();
  222. _errors = 0;
  223. _error = SENSOR_ERROR_OK;
  224. }
  225. unsigned long _signal(unsigned long usTimeOut, bool state) {
  226. unsigned long uSec = 1;
  227. while (digitalRead(_gpio) == state) {
  228. if (++uSec > usTimeOut) return 0;
  229. delayMicroseconds(1);
  230. }
  231. return uSec;
  232. }
  233. DHTChipType _type = DHT_CHIP_DHT22;
  234. unsigned char _gpio = GPIO_NONE;
  235. unsigned char _previous = GPIO_NONE;
  236. unsigned long _last_ok = 0;
  237. unsigned char _errors = 0;
  238. double _temperature = DHT_DUMMY_VALUE;
  239. double _humidity = DHT_DUMMY_VALUE;
  240. };
  241. #endif // SENSOR_SUPPORT && DHT_SUPPORT