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.

176 lines
5.7 KiB

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