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.

175 lines
5.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
  1. // -----------------------------------------------------------------------------
  2. // GUVA-S12SD UV Sensor
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // by Mustafa Tufan
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && GUVAS12SD_SUPPORT
  7. #pragma once
  8. #include <Arduino.h>
  9. #include "BaseSensor.h"
  10. #include "../utils.h"
  11. // http://www.eoc-inc.com/genicom/GUVA-S12SD.pdf
  12. //
  13. // GUVA-S12D has a wide spectral range of 200nm-400nm
  14. // The output voltage and the UV index is linear, illumination intensity = 307 * Vsig where: Vsig is the value of voltage measured from the SIG pin of the interface, unit V.
  15. // illumination intensity unit: mW/m2 for the combination strength of UV light with wavelength range: 200nm-400nm
  16. // UV Index = illumination intensity / 200
  17. //
  18. // UV Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 10+
  19. // -----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+--------
  20. // mV | <50 | 227 | 318 | 408 | 503 | 606 | 696 | 795 | 881 | 976 | 1079 | 1170+
  21. // analog val | <10 | 46 | 65 | 83 | 103 | 124 | 142 | 162 | 180 | 200 | 221 | 240+
  22. //
  23. #define UV_SAMPLE_RATE 1
  24. class GUVAS12SDSensor : public BaseSensor {
  25. public:
  26. // ---------------------------------------------------------------------
  27. // Public
  28. // ---------------------------------------------------------------------
  29. GUVAS12SDSensor() {
  30. _count = 1;
  31. _sensor_id = SENSOR_GUVAS12SD_ID;
  32. }
  33. ~GUVAS12SDSensor() {
  34. gpioUnlock(_gpio);
  35. }
  36. // ---------------------------------------------------------------------
  37. void setGPIO(unsigned char gpio) {
  38. _gpio = gpio;
  39. }
  40. // ---------------------------------------------------------------------
  41. unsigned char getGPIO() {
  42. return _gpio;
  43. }
  44. // ---------------------------------------------------------------------
  45. // Sensor API
  46. // ---------------------------------------------------------------------
  47. // Initialization method, must be idempotent
  48. void begin() {
  49. // Manage GPIO lock
  50. if (_previous != GPIO_NONE) {
  51. gpioUnlock(_previous);
  52. }
  53. _previous = GPIO_NONE;
  54. if (!gpioLock(_gpio)) {
  55. _error = SENSOR_ERROR_GPIO_USED;
  56. return;
  57. }
  58. _previous = _gpio;
  59. _ready = true;
  60. }
  61. // Pre-read hook (usually to populate registers with up-to-date data)
  62. void pre() {
  63. _error = SENSOR_ERROR_OK;
  64. _read();
  65. }
  66. // Descriptive name of the sensor
  67. String description() {
  68. char buffer[18];
  69. snprintf(buffer, sizeof(buffer), "GUVAS12SD @ GPIO%d", _gpio);
  70. return String(buffer);
  71. }
  72. // Descriptive name of the slot # index
  73. String description(unsigned char index) {
  74. return description();
  75. };
  76. // Address of the sensor (it could be the GPIO or I2C address)
  77. String address(unsigned char index) {
  78. return String(_gpio);
  79. }
  80. // Type for slot # index
  81. unsigned char type(unsigned char index) {
  82. if (index == 0) return MAGNITUDE_UVI;
  83. return MAGNITUDE_NONE;
  84. }
  85. // Current value for slot # index
  86. double value(unsigned char index) {
  87. if (index == 0) return _uvindex;
  88. return 0;
  89. }
  90. protected:
  91. // ---------------------------------------------------------------------
  92. // Protected
  93. // ---------------------------------------------------------------------
  94. void _read() {
  95. int _average = 0;
  96. #if UV_SAMPLE_RATE == 1
  97. _average = analogRead(0);
  98. #else
  99. for (unsigned int i=0; i < UV_SAMPLE_RATE; i++) {
  100. _average += analogRead(0);
  101. nice_delay(2);
  102. }
  103. _average = (_average / UV_SAMPLE_RATE);
  104. #endif
  105. // _sensormV = _average / 1023*3.3;
  106. if (_average < 10) {
  107. _uvindex = 0;
  108. } else if (_average < 46) {
  109. _uvindex = (_average - 10) / (46-10);
  110. } else if (_average < 65) {
  111. _uvindex = 1 + ((_average - 46) / (65-46));
  112. } else if (_average < 83) {
  113. _uvindex = 2 + ((_average - 65) / (83-65));
  114. } else if (_average < 103) {
  115. _uvindex = 3 + ((_average - 83) / (103- 83));
  116. } else if (_average < 124) {
  117. _uvindex = 4 + ((_average - 103) / (124-103));
  118. } else if (_average < 142) {
  119. _uvindex = 5 + ((_average - 124) / (142-124));
  120. } else if (_average < 162) {
  121. _uvindex = 6 + ((_average - 142) / (162-142));
  122. } else if (_average < 180) {
  123. _uvindex = 7 + ((_average - 162) / (180-162));
  124. } else if (_average < 200) {
  125. _uvindex = 8 + ((_average - 180) / (200-180));
  126. } else if (_average < 221) {
  127. _uvindex = 9 + ((_average - 200) / (221-200));
  128. } else {
  129. _uvindex = 10;
  130. }
  131. }
  132. unsigned char _gpio = GPIO_NONE;
  133. unsigned char _previous = GPIO_NONE;
  134. double _uvindex = 0;
  135. };
  136. #endif // SENSOR_SUPPORT && GUVAS12SD_SUPPORT