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.

102 lines
2.7 KiB

6 years ago
  1. // -----------------------------------------------------------------------------
  2. // Analog Sensor (maps to an analogRead)
  3. // Copyright (C) 2017-2018 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. // ---------------------------------------------------------------------
  28. unsigned int getSamples() {
  29. return _samples;
  30. }
  31. unsigned long getDelay() {
  32. return _micros;
  33. }
  34. // ---------------------------------------------------------------------
  35. // Sensor API
  36. // ---------------------------------------------------------------------
  37. // Initialization method, must be idempotent
  38. void begin() {
  39. pinMode(0, INPUT);
  40. _ready = true;
  41. }
  42. // Descriptive name of the sensor
  43. String description() {
  44. return String("ANALOG @ TOUT");
  45. }
  46. // Descriptive name of the slot # index
  47. String slot(unsigned char index) {
  48. return description();
  49. };
  50. // Address of the sensor (it could be the GPIO or I2C address)
  51. String address(unsigned char index) {
  52. return String("0");
  53. }
  54. // Type for slot # index
  55. unsigned char type(unsigned char index) {
  56. if (index == 0) return MAGNITUDE_ANALOG;
  57. return MAGNITUDE_NONE;
  58. }
  59. // Current value for slot # index
  60. double value(unsigned char index) {
  61. if (index == 0) return _read();
  62. return 0;
  63. }
  64. protected:
  65. unsigned int _read() {
  66. if (1 == _samples) return analogRead(0);
  67. unsigned long sum = 0;
  68. for (unsigned int i=0; i<_samples; i++) {
  69. if (i>0) delayMicroseconds(_micros);
  70. sum += analogRead(0);
  71. }
  72. return sum / _samples;
  73. }
  74. unsigned int _samples = 1;
  75. unsigned long _micros = 0;
  76. };
  77. #endif // SENSOR_SUPPORT && ANALOG_SUPPORT