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.

143 lines
4.3 KiB

6 years ago
  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)
  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. pinMode(0, INPUT);
  54. _ready = true;
  55. }
  56. // Descriptive name of the sensor
  57. String description() {
  58. return String("ANALOG @ TOUT");
  59. }
  60. // Descriptive name of the slot # index
  61. String slot(unsigned char index) {
  62. return description();
  63. };
  64. // Address of the sensor (it could be the GPIO or I2C address)
  65. String address(unsigned char index) {
  66. return String("0");
  67. }
  68. // Type for slot # index
  69. unsigned char type(unsigned char index) {
  70. if (index == 0) return MAGNITUDE_ANALOG;
  71. return MAGNITUDE_NONE;
  72. }
  73. // Current value for slot # index
  74. // Changed return type as moving to scaled value
  75. double value(unsigned char index) {
  76. if (index == 0) return _read();
  77. return 0;
  78. }
  79. protected:
  80. //CICM: this should be for raw values
  81. // renaming protected function "_read" to "_rawRead"
  82. unsigned int _rawRead() {
  83. if (1 == _samples) return analogRead(0);
  84. unsigned long sum = 0;
  85. for (unsigned int i=0; i<_samples; i++) {
  86. if (i>0) delayMicroseconds(_micros);
  87. sum += analogRead(0);
  88. }
  89. return sum / _samples;
  90. }
  91. //CICM: and proper read should be scalable and thus needs sign
  92. //and decimal part
  93. double _read() {
  94. //Raw measure could also be a class variable with getter so that can
  95. //be reported through MQTT, ...
  96. unsigned int rawValue;
  97. double scaledValue;
  98. // Debugging doubles to string
  99. //DEBUG_MSG(("[ANALOG_SENSOR] Started standard read, factor: %s , offset: %s, decimals: %d \n"), String(_factor).c_str(), String(_offset).c_str(), ANALOG_DECIMALS);
  100. rawValue = _rawRead();
  101. //DEBUG_MSG(("[ANALOG_SENSOR] Raw read received: %d \n"), rawValue);
  102. scaledValue = _factor*rawValue + _offset;
  103. //DEBUG_MSG(("[ANALOG_SENSOR] Scaled value result: %s \n"), String(scaledValue).c_str());
  104. return scaledValue;
  105. }
  106. unsigned int _samples = 1;
  107. unsigned long _micros = 0;
  108. //CICM: for scaling and offset, also with getters and setters
  109. double _factor = 1.0;
  110. double _offset = 0.0;
  111. };
  112. #endif // SENSOR_SUPPORT && ANALOG_SUPPORT