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.

54 lines
1.4 KiB

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