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.

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