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.

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