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.

56 lines
1.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // Digital Sensor (maps to a digitalRead)
  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 DigitalSensor : public BaseSensor {
  9. public:
  10. DigitalSensor(unsigned char gpio, int mode = INPUT, bool default_state = false): BaseSensor() {
  11. _gpio = gpio;
  12. _default = default_state;
  13. pinMode(_gpio, mode);
  14. _count = 1;
  15. }
  16. // Descriptive name of the sensor
  17. String name() {
  18. char buffer[20];
  19. snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
  20. return String(buffer);
  21. }
  22. // Descriptive name of the slot # index
  23. String slot(unsigned char index) {
  24. return name();
  25. }
  26. // Type for slot # index
  27. magnitude_t type(unsigned char index) {
  28. _error = SENSOR_ERROR_OK;
  29. if (index == 0) return MAGNITUDE_DIGITAL;
  30. _error = SENSOR_ERROR_OUT_OF_RANGE;
  31. return MAGNITUDE_NONE;
  32. }
  33. // Current value for slot # index
  34. double value(unsigned char index) {
  35. _error = SENSOR_ERROR_OK;
  36. if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
  37. _error = SENSOR_ERROR_OUT_OF_RANGE;
  38. return 0;
  39. }
  40. protected:
  41. unsigned char _gpio;
  42. bool _default;
  43. };