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.7 KiB

6 years ago
  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. // Set ADC to TOUT pin
  9. #undef ADC_MODE_VALUE
  10. #define ADC_MODE_VALUE ADC_TOUT
  11. #include "Arduino.h"
  12. #include "BaseSensor.h"
  13. // http://www.eoc-inc.com/genicom/GUVA-S12SD.pdf
  14. //
  15. // GUVA-S12D has a wide spectral range of 200nm-400nm
  16. // 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.
  17. // illumination intensity unit: mW/m2 for the combination strength of UV light with wavelength range: 200nm-400nm
  18. // UV Index = illumination intensity / 200
  19. //
  20. // UV Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 10+
  21. // -----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+--------
  22. // mV | <50 | 227 | 318 | 408 | 503 | 606 | 696 | 795 | 881 | 976 | 1079 | 1170+
  23. // analog val | <10 | 46 | 65 | 83 | 103 | 124 | 142 | 162 | 180 | 200 | 221 | 240+
  24. //
  25. #define UV_SAMPLE_RATE 1
  26. class GUVAS12SDSensor : public BaseSensor {
  27. public:
  28. // ---------------------------------------------------------------------
  29. // Public
  30. // ---------------------------------------------------------------------
  31. GUVAS12SDSensor(): BaseSensor() {
  32. _count = 1;
  33. _sensor_id = SENSOR_GUVAS12SD_ID;
  34. }
  35. ~GUVAS12SDSensor() {
  36. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  37. }
  38. // ---------------------------------------------------------------------
  39. void setGPIO(unsigned char gpio) {
  40. _gpio = gpio;
  41. }
  42. // ---------------------------------------------------------------------
  43. unsigned char getGPIO() {
  44. return _gpio;
  45. }
  46. // ---------------------------------------------------------------------
  47. // Sensor API
  48. // ---------------------------------------------------------------------
  49. // Initialization method, must be idempotent
  50. void begin() {
  51. // Manage GPIO lock
  52. if (_previous != GPIO_NONE) gpioReleaseLock(_previous);
  53. _previous = GPIO_NONE;
  54. if (!gpioGetLock(_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 slot(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_UV;
  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