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.

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