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.

74 lines
2.1 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. // ---------------------------------------------------------------------
  11. // Public
  12. // ---------------------------------------------------------------------
  13. DigitalSensor(): BaseSensor() {
  14. _count = 1;
  15. }
  16. void setGPIO(unsigned char gpio, int mode = INPUT) {
  17. _gpio = gpio;
  18. pinMode(_gpio, mode);
  19. }
  20. void setDefault(bool value) {
  21. _default = value;
  22. }
  23. // ---------------------------------------------------------------------
  24. // Sensor API
  25. // ---------------------------------------------------------------------
  26. // Descriptive name of the sensor
  27. String name() {
  28. char buffer[20];
  29. snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
  30. return String(buffer);
  31. }
  32. // Descriptive name of the slot # index
  33. String slot(unsigned char index) {
  34. return name();
  35. }
  36. // Type for slot # index
  37. magnitude_t type(unsigned char index) {
  38. _error = SENSOR_ERROR_OK;
  39. if (index == 0) return MAGNITUDE_DIGITAL;
  40. _error = SENSOR_ERROR_OUT_OF_RANGE;
  41. return MAGNITUDE_NONE;
  42. }
  43. // Current value for slot # index
  44. double value(unsigned char index) {
  45. _error = SENSOR_ERROR_OK;
  46. if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
  47. _error = SENSOR_ERROR_OUT_OF_RANGE;
  48. return 0;
  49. }
  50. protected:
  51. // ---------------------------------------------------------------------
  52. // Protected
  53. // ---------------------------------------------------------------------
  54. unsigned char _gpio;
  55. bool _default = false;
  56. };