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.

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