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.

172 lines
5.6 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. #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. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  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) gpioReleaseLock(_previous);
  51. _previous = GPIO_NONE;
  52. if (!gpioGetLock(_gpio)) {
  53. _error = SENSOR_ERROR_GPIO_USED;
  54. return;
  55. }
  56. _previous = _gpio;
  57. _ready = true;
  58. }
  59. // Pre-read hook (usually to populate registers with up-to-date data)
  60. void pre() {
  61. _error = SENSOR_ERROR_OK;
  62. _read();
  63. }
  64. // Descriptive name of the sensor
  65. String description() {
  66. char buffer[18];
  67. snprintf(buffer, sizeof(buffer), "GUVAS12SD @ GPIO%d", _gpio);
  68. return String(buffer);
  69. }
  70. // Descriptive name of the slot # index
  71. String slot(unsigned char index) {
  72. return description();
  73. };
  74. // Address of the sensor (it could be the GPIO or I2C address)
  75. String address(unsigned char index) {
  76. return String(_gpio);
  77. }
  78. // Type for slot # index
  79. unsigned char type(unsigned char index) {
  80. if (index == 0) return MAGNITUDE_UVI;
  81. return MAGNITUDE_NONE;
  82. }
  83. // Current value for slot # index
  84. double value(unsigned char index) {
  85. if (index == 0) return _uvindex;
  86. return 0;
  87. }
  88. protected:
  89. // ---------------------------------------------------------------------
  90. // Protected
  91. // ---------------------------------------------------------------------
  92. void _read() {
  93. int _average = 0;
  94. #if UV_SAMPLE_RATE == 1
  95. _average = analogRead(0);
  96. #else
  97. for (unsigned int i=0; i < UV_SAMPLE_RATE; i++) {
  98. _average += analogRead(0);
  99. nice_delay(2);
  100. }
  101. _average = (_average / UV_SAMPLE_RATE);
  102. #endif
  103. // _sensormV = _average / 1023*3.3;
  104. if (_average < 10) {
  105. _uvindex = 0;
  106. } else if (_average < 46) {
  107. _uvindex = (_average - 10) / (46-10);
  108. } else if (_average < 65) {
  109. _uvindex = 1 + ((_average - 46) / (65-46));
  110. } else if (_average < 83) {
  111. _uvindex = 2 + ((_average - 65) / (83-65));
  112. } else if (_average < 103) {
  113. _uvindex = 3 + ((_average - 83) / (103- 83));
  114. } else if (_average < 124) {
  115. _uvindex = 4 + ((_average - 103) / (124-103));
  116. } else if (_average < 142) {
  117. _uvindex = 5 + ((_average - 124) / (142-124));
  118. } else if (_average < 162) {
  119. _uvindex = 6 + ((_average - 142) / (162-142));
  120. } else if (_average < 180) {
  121. _uvindex = 7 + ((_average - 162) / (180-162));
  122. } else if (_average < 200) {
  123. _uvindex = 8 + ((_average - 180) / (200-180));
  124. } else if (_average < 221) {
  125. _uvindex = 9 + ((_average - 200) / (221-200));
  126. } else {
  127. _uvindex = 10;
  128. }
  129. }
  130. unsigned char _gpio = GPIO_NONE;
  131. unsigned char _previous = GPIO_NONE;
  132. double _uvindex = 0;
  133. };
  134. #endif // SENSOR_SUPPORT && GUVAS12SD_SUPPORT