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.

170 lines
5.6 KiB

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