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