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.

179 lines
4.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // LDR Sensor (maps to a LDRSensor)
  3. // Copyright (C) 2019 by Altan Altay
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && LDR_SUPPORT
  6. #pragma once
  7. #include <Arduino.h>
  8. #include "AnalogSensor.h"
  9. extern "C" {
  10. #include "../libs/fs_math.h"
  11. }
  12. #define LDR_GL5516 1
  13. #define LDR_GL5528 2
  14. #define LDR_GL5537_1 3
  15. #define LDR_GL5537_2 4
  16. #define LDR_GL5539 5
  17. #define LDR_GL5549 6
  18. #define LDR_OTHER 99
  19. class LDRSensor : public AnalogSensor {
  20. public:
  21. // ---------------------------------------------------------------------
  22. // Public
  23. // ---------------------------------------------------------------------
  24. LDRSensor() {
  25. _count = 1;
  26. _sensor_id = SENSOR_LDR_ID;
  27. }
  28. void setType(unsigned char type) {
  29. _type = type;
  30. switch (_type) {
  31. case LDR_GL5516:
  32. _mult_value = 29634400;
  33. _pow_value = 1.6689;
  34. break;
  35. case LDR_GL5537_1:
  36. _mult_value = 32435800;
  37. _pow_value = 1.4899;
  38. break;
  39. case LDR_GL5537_2:
  40. _mult_value = 2801820;
  41. _pow_value = 1.1772;
  42. break;
  43. case LDR_GL5539:
  44. _mult_value = 208510000;
  45. _pow_value = 1.4850;
  46. break;
  47. case LDR_GL5549:
  48. _mult_value = 44682100;
  49. _pow_value = 1.2750;
  50. break;
  51. case LDR_OTHER:
  52. _mult_value = LDR_MULTIPLICATION;
  53. _pow_value = LDR_POWER;
  54. break;
  55. case LDR_GL5528:
  56. default:
  57. _mult_value = 32017200;
  58. _pow_value = 1.5832;
  59. break;
  60. }
  61. }
  62. /*!
  63. * \brief setPhotocellPositionOnGround Configure the photocell as connected to 3.3V or GND
  64. *
  65. * \param on_ground (bool) True if the photocell is connected to GND, else false
  66. *
  67. * True: EXTERNAL ADC
  68. * ^ ^
  69. * _____ | ___/___
  70. * 3.3V |---|_____|--*--|__/____|--| GND
  71. * Other /
  72. * Resistor Photocell
  73. *
  74. * False:
  75. * EXTERNAL ADC
  76. * ^ ^
  77. * _____ | ___/___
  78. * GND |---|_____|--*--|__/____|--| 3.3V
  79. * Other /
  80. * Resistor Photocell
  81. */
  82. void setPhotocellPositionOnGround(bool on_ground) {
  83. _photocell_on_ground = on_ground;
  84. }
  85. void setResistor(unsigned long resistor) {
  86. _resistor = resistor;
  87. }
  88. /*!
  89. * \brief updatePhotocellParameters Redefine the photocell parameters
  90. *
  91. * \parameter mult_value (float) Multiplication parameter in "I[lux]=mult_value/(R[Ω]^pow_value)" expression
  92. * \parameter pow_value (float) Power parameter in "I[lux]=mult_value/(R[Ω]^pow_value)" expression
  93. */
  94. void setPhotocellParameters(float mult_value, float pow_value) {
  95. if (_type == LDR_OTHER) {
  96. _mult_value = mult_value;
  97. _pow_value = pow_value;
  98. }
  99. }
  100. // ---------------------------------------------------------------------
  101. // ---------------------------------------------------------------------
  102. // Sensor API
  103. // ---------------------------------------------------------------------
  104. // Descriptive name of the sensor
  105. String description() {
  106. return String("LDR @ TOUT");
  107. }
  108. // Descriptive name of the slot # index
  109. String slot(unsigned char index) {
  110. return description();
  111. }
  112. // Address of the sensor (it could be the GPIO or I2C address)
  113. String address(unsigned char index) {
  114. return String("0");
  115. }
  116. // Type for slot # index
  117. unsigned char type(unsigned char index) {
  118. if (index == 0) return MAGNITUDE_LUX;
  119. return MAGNITUDE_NONE;
  120. }
  121. // Current value for slot # index
  122. double value(unsigned char index) {
  123. double current_lux = 0;
  124. if (index == 0) {
  125. unsigned long photocell_resistor = 0;
  126. // sampled reading
  127. double read = _read();
  128. float ratio = ((float)1024/(float)read) - 1;
  129. if (_photocell_on_ground) {
  130. photocell_resistor = _resistor / ratio;
  131. } else {
  132. photocell_resistor = _resistor * ratio;
  133. }
  134. current_lux = _mult_value / (float)pow(photocell_resistor, _pow_value);
  135. }
  136. return current_lux;
  137. }
  138. protected:
  139. unsigned char _type = LDR_GL5528;
  140. bool _photocell_on_ground = false;
  141. unsigned long _resistor = 10000;
  142. float _mult_value = 0;
  143. float _pow_value = 0;
  144. };
  145. #endif // SENSOR_SUPPORT && LDR_SUPPORT