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.

138 lines
4.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // Analog Sensor (maps to an analogRead)
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && (ANALOG_SUPPORT || NTC_SUPPORT || LDR_SUPPORT)
  6. #pragma once
  7. #include "BaseSensor.h"
  8. #include "BaseAnalogSensor.h"
  9. class AnalogSensor : public BaseAnalogSensor {
  10. public:
  11. // ---------------------------------------------------------------------
  12. // Public
  13. // ---------------------------------------------------------------------
  14. AnalogSensor() {
  15. _count = 1;
  16. _sensor_id = SENSOR_ANALOG_ID;
  17. }
  18. void setSamples(unsigned int samples) {
  19. if (_samples > 0) _samples = samples;
  20. }
  21. void setDelay(unsigned long micros) {
  22. _micros = micros;
  23. }
  24. void setFactor(double factor) {
  25. //DEBUG_MSG(("[ANALOG_SENSOR] Factor set to: %s \n"), String(factor,6).c_str());
  26. _factor = factor;
  27. }
  28. void setOffset(double offset) {
  29. //DEBUG_MSG(("[ANALOG_SENSOR] Factor set to: %s \n"), String(offset,6).c_str());
  30. _offset = offset;
  31. }
  32. // ---------------------------------------------------------------------
  33. unsigned int getSamples() {
  34. return _samples;
  35. }
  36. unsigned long getDelay() {
  37. return _micros;
  38. }
  39. double getFactor() {
  40. return _factor;
  41. }
  42. double getOffset() {
  43. return _offset;
  44. }
  45. // ---------------------------------------------------------------------
  46. // Sensor API
  47. // ---------------------------------------------------------------------
  48. // Initialization method, must be idempotent
  49. void begin() {
  50. _ready = true;
  51. }
  52. // Descriptive name of the sensor
  53. String description() {
  54. return String("ANALOG @ TOUT");
  55. }
  56. // Descriptive name of the slot # index
  57. String description(unsigned char index) {
  58. return description();
  59. };
  60. // Address of the sensor (it could be the GPIO or I2C address)
  61. String address(unsigned char index) {
  62. return String("0");
  63. }
  64. // Type for slot # index
  65. unsigned char type(unsigned char index) {
  66. if (index == 0) return MAGNITUDE_ANALOG;
  67. return MAGNITUDE_NONE;
  68. }
  69. // Current value for slot # index
  70. // Changed return type as moving to scaled value
  71. double value(unsigned char index) {
  72. if (index == 0) return _read();
  73. return 0;
  74. }
  75. protected:
  76. //CICM: this should be for raw values
  77. // renaming protected function "_read" to "_rawRead"
  78. unsigned int _rawRead() {
  79. if (1 == _samples) return analogRead(0);
  80. unsigned long sum = 0;
  81. for (unsigned int i=0; i<_samples; i++) {
  82. if (i>0) delayMicroseconds(_micros);
  83. sum += analogRead(0);
  84. }
  85. return sum / _samples;
  86. }
  87. //CICM: and proper read should be scalable and thus needs sign
  88. //and decimal part
  89. double _read() {
  90. //Raw measure could also be a class variable with getter so that can
  91. //be reported through MQTT, ...
  92. unsigned int rawValue;
  93. double scaledValue;
  94. // Debugging doubles to string
  95. //DEBUG_MSG(("[ANALOG_SENSOR] Started standard read, factor: %s , offset: %s, decimals: %d \n"), String(_factor).c_str(), String(_offset).c_str(), ANALOG_DECIMALS);
  96. rawValue = _rawRead();
  97. //DEBUG_MSG(("[ANALOG_SENSOR] Raw read received: %d \n"), rawValue);
  98. scaledValue = _factor*rawValue + _offset;
  99. //DEBUG_MSG(("[ANALOG_SENSOR] Scaled value result: %s \n"), String(scaledValue).c_str());
  100. return scaledValue;
  101. }
  102. unsigned int _samples = 1;
  103. unsigned long _micros = 0;
  104. //CICM: for scaling and offset, also with getters and setters
  105. double _factor = 1.0;
  106. double _offset = 0.0;
  107. };
  108. #endif // SENSOR_SUPPORT && ANALOG_SUPPORT