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.

53 lines
1.3 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHT Sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. class AnalogSensor : public BaseSensor {
  8. public:
  9. AnalogSensor(unsigned char gpio, int mode = INPUT): BaseSensor() {
  10. _gpio = gpio;
  11. pinMode(_gpio, mode);
  12. _count = 1;
  13. }
  14. // Descriptive name of the sensor
  15. String name() {
  16. char buffer[20];
  17. snprintf(buffer, sizeof(buffer), "ANALOG @ GPIO%d", _gpio);
  18. return String(buffer);
  19. }
  20. // Descriptive name of the slot # index
  21. String slot(unsigned char index) {
  22. return name();
  23. }
  24. // Type for slot # index
  25. magnitude_t type(unsigned char index) {
  26. _error = SENSOR_ERROR_OK;
  27. if (index == 0) return MAGNITUDE_ANALOG;
  28. _error = SENSOR_ERROR_OUT_OF_RANGE;
  29. return MAGNITUDE_NONE;
  30. }
  31. // Current value for slot # index
  32. double value(unsigned char index) {
  33. _error = SENSOR_ERROR_OK;
  34. if (index == 0) return analogRead(_gpio);
  35. _error = SENSOR_ERROR_OUT_OF_RANGE;
  36. return 0;
  37. }
  38. protected:
  39. unsigned char _gpio;
  40. };