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.

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