Mirror of espurna firmware for wireless switches and more
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.

323 lines
9.5 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 "../gpio.h"
  8. #include "../utils.h"
  9. #include "BaseSensor.h"
  10. enum class DHTChipType {
  11. DHT11,
  12. DHT12,
  13. DHT21,
  14. DHT22,
  15. AM2301,
  16. SI7021
  17. };
  18. // Note: backwards compatibility for configuration headers
  19. #define DHT_CHIP_DHT11 DHTChipType::DHT11
  20. #define DHT_CHIP_DHT12 DHTChipType::DHT12
  21. #define DHT_CHIP_DHT22 DHTChipType::DHT22
  22. #define DHT_CHIP_DHT21 DHTChipType::DHT21
  23. #define DHT_CHIP_AM2301 DHTChipType::AM2301
  24. #define DHT_CHIP_SI7021 DHTChipType::SI7021
  25. int dhtchip_to_number(DHTChipType chip) {
  26. switch (chip) {
  27. case DHTChipType::DHT11:
  28. return 11;
  29. case DHTChipType::DHT12:
  30. return 12;
  31. case DHTChipType::DHT21:
  32. case DHTChipType::AM2301:
  33. return 21;
  34. case DHTChipType::DHT22:
  35. case DHTChipType::SI7021:
  36. return 22;
  37. }
  38. return -1;
  39. }
  40. class DHTSensor : public BaseSensor {
  41. public:
  42. // ---------------------------------------------------------------------
  43. // Public
  44. // ---------------------------------------------------------------------
  45. static constexpr double DummyValue = -255.0;
  46. static constexpr size_t MaxErrors = 5;
  47. using Data = std::array<uint8_t, 5>;
  48. using TimeSource = espurna::time::CoreClock;
  49. static constexpr auto MinInterval = espurna::duration::Milliseconds { 2000 };
  50. ~DHTSensor() {
  51. gpioUnlock(_gpio);
  52. }
  53. // ---------------------------------------------------------------------
  54. void setGPIO(unsigned char gpio) {
  55. _gpio = gpio;
  56. }
  57. void setType(DHTChipType type) {
  58. _type = type;
  59. }
  60. // ---------------------------------------------------------------------
  61. unsigned char getGPIO() const {
  62. return _gpio;
  63. }
  64. int getType() const {
  65. return dhtchip_to_number(_type);
  66. }
  67. DHTChipType getChipType() const {
  68. return _type;
  69. }
  70. // ---------------------------------------------------------------------
  71. // Sensor API
  72. // ---------------------------------------------------------------------
  73. unsigned char id() const override {
  74. return SENSOR_DHTXX_ID;
  75. }
  76. unsigned char count() const override {
  77. return 2;
  78. }
  79. // Initialization method, must be idempotent
  80. void begin() override {
  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 = TimeSource::now();
  93. _ready = true;
  94. }
  95. // Pre-read hook (usually to populate registers with up-to-date data)
  96. void pre() override {
  97. _error = SENSOR_ERROR_OK;
  98. _read();
  99. }
  100. // Descriptive name of the sensor
  101. String description() const override {
  102. char buffer[20];
  103. snprintf_P(buffer, sizeof(buffer),
  104. "DHT%d @ GPIO%hhu", dhtchip_to_number(_type), _gpio);
  105. return String(buffer);
  106. }
  107. // Address of the sensor (it could be the GPIO or I2C address)
  108. String address(unsigned char) const override {
  109. return String(_gpio, 10);
  110. }
  111. // Type for slot # index
  112. unsigned char type(unsigned char index) const override {
  113. if (index == 0) return MAGNITUDE_TEMPERATURE;
  114. if (index == 1) return MAGNITUDE_HUMIDITY;
  115. return MAGNITUDE_NONE;
  116. }
  117. // Current value for slot # index
  118. double value(unsigned char index) override {
  119. if (index == 0) return _temperature;
  120. if (index == 1) return _humidity;
  121. return 0;
  122. }
  123. protected:
  124. // ---------------------------------------------------------------------
  125. // Protected
  126. // ---------------------------------------------------------------------
  127. void _read_critical(Data& dhtData) {
  128. digitalWrite(_gpio, LOW);
  129. if ((_type == DHT_CHIP_DHT11) || (_type == DHT_CHIP_DHT12)) {
  130. espurna::time::blockingDelay(
  131. espurna::duration::Milliseconds(20));
  132. } else if (_type == DHT_CHIP_SI7021) {
  133. espurna::time::critical::delay(
  134. espurna::duration::critical::Microseconds(500));
  135. } else {
  136. espurna::time::critical::delay(
  137. espurna::duration::critical::Microseconds(1100));
  138. }
  139. digitalWrite(_gpio, HIGH);
  140. espurna::time::critical::delay(
  141. espurna::duration::critical::Microseconds(40));
  142. pinMode(_gpio, INPUT_PULLUP);
  143. espurna::time::critical::delay(
  144. espurna::duration::critical::Microseconds(10));
  145. unsigned long low = 0;
  146. unsigned long high = 0;
  147. unsigned char byteInx = 0;
  148. unsigned char bitInx = 7;
  149. // No errors, read the 40 data bits
  150. for( int k = 0; k < 41; k++ ) {
  151. // Starts new data transmission with >50us low signal
  152. low = _signal(100, LOW);
  153. if (low == 0) {
  154. _error = SENSOR_ERROR_TIMEOUT;
  155. return;
  156. }
  157. // Check to see if after >70us rx data is a 0 or a 1
  158. high = _signal(100, HIGH);
  159. if (high == 0) {
  160. _error = SENSOR_ERROR_TIMEOUT;
  161. return;
  162. }
  163. // Skip the first bit
  164. if (k == 0) continue;
  165. // add the current read to the output data
  166. // since all dhtData array where set to 0 at the start,
  167. // only look for "1" (>28us us)
  168. if (high > low) {
  169. dhtData[byteInx] |= (1 << bitInx);
  170. }
  171. // index to next byte
  172. if (bitInx == 0) {
  173. bitInx = 7;
  174. ++byteInx;
  175. } else {
  176. --bitInx;
  177. }
  178. }
  179. }
  180. void _read() {
  181. if (TimeSource::now() - _last_ok < MinInterval) {
  182. if ((_temperature == DummyValue) && (_humidity == DummyValue)) {
  183. _error = SENSOR_ERROR_WARM_UP;
  184. } else {
  185. _error = SENSOR_ERROR_OK;
  186. }
  187. return;
  188. }
  189. pinMode(_gpio, OUTPUT);
  190. // Send start signal to DHT sensor
  191. if (++_errors > MaxErrors) {
  192. _errors = 0;
  193. digitalWrite(_gpio, HIGH);
  194. espurna::time::blockingDelay(
  195. espurna::duration::Milliseconds(250));
  196. }
  197. Data dhtData{};
  198. noInterrupts();
  199. _read_critical(dhtData);
  200. interrupts();
  201. if (_error != SENSOR_ERROR_OK) {
  202. return;
  203. }
  204. // Verify checksum
  205. if (dhtData[4] != ((dhtData[0] + dhtData[1] + dhtData[2] + dhtData[3]) & 0xFF)) {
  206. _error = SENSOR_ERROR_CRC;
  207. return;
  208. }
  209. // Get humidity from Data[0] and Data[1]
  210. if (_type == DHT_CHIP_DHT11) {
  211. _humidity = dhtData[0];
  212. } else if (_type == DHT_CHIP_DHT12) {
  213. _humidity = dhtData[0];
  214. _humidity += dhtData[1] * 0.1;
  215. } else {
  216. _humidity = dhtData[0] * 256 + dhtData[1];
  217. _humidity /= 10;
  218. }
  219. // Get temp from Data[2] and Data[3]
  220. if (_type == DHT_CHIP_DHT11) {
  221. _temperature = dhtData[2];
  222. } else if (_type == DHT_CHIP_DHT12) {
  223. _temperature = (dhtData[2] & 0x7F);
  224. _temperature += dhtData[3] * 0.1;
  225. if (dhtData[2] & 0x80) {
  226. _temperature *= -1;
  227. }
  228. } else {
  229. _temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
  230. _temperature /= 10;
  231. if (dhtData[2] & 0x80) {
  232. _temperature *= -1;
  233. }
  234. }
  235. _last_ok = TimeSource::now();
  236. _errors = 0;
  237. _error = SENSOR_ERROR_OK;
  238. }
  239. unsigned long _signal(unsigned long maximum, bool state) {
  240. unsigned long ticks = 1;
  241. while (digitalRead(_gpio) == state) {
  242. if (++ticks > maximum) return 0;
  243. espurna::time::critical::delay(
  244. espurna::duration::critical::Microseconds(1));
  245. }
  246. return ticks;
  247. }
  248. DHTChipType _type = DHT_CHIP_DHT22;
  249. unsigned char _gpio = GPIO_NONE;
  250. unsigned char _previous = GPIO_NONE;
  251. TimeSource::time_point _last_ok;
  252. size_t _errors = 0;
  253. bool _warmup = false;
  254. double _temperature = DummyValue;
  255. double _humidity = 0.0;
  256. };
  257. #endif // SENSOR_SUPPORT && DHT_SUPPORT