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.

99 lines
2.7 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. // ---------------------------------------------------------------------
  17. void setGPIO(unsigned char gpio) {
  18. _gpio = gpio;
  19. }
  20. void setMode(unsigned char mode) {
  21. _mode = mode;
  22. }
  23. void setDefault(bool value) {
  24. _default = value;
  25. }
  26. // ---------------------------------------------------------------------
  27. unsigned char getGPIO() {
  28. return _gpio;
  29. }
  30. unsigned char getMode() {
  31. return _mode;
  32. }
  33. bool getDefault() {
  34. return _default;
  35. }
  36. // ---------------------------------------------------------------------
  37. // Sensor API
  38. // ---------------------------------------------------------------------
  39. // Initialization method, must be idempotent
  40. void begin() {
  41. pinMode(_gpio, _mode);
  42. }
  43. // Descriptive name of the sensor
  44. String name() {
  45. char buffer[20];
  46. snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
  47. return String(buffer);
  48. }
  49. // Descriptive name of the slot # index
  50. String slot(unsigned char index) {
  51. return name();
  52. }
  53. // Type for slot # index
  54. magnitude_t type(unsigned char index) {
  55. _error = SENSOR_ERROR_OK;
  56. if (index == 0) return MAGNITUDE_DIGITAL;
  57. _error = SENSOR_ERROR_OUT_OF_RANGE;
  58. return MAGNITUDE_NONE;
  59. }
  60. // Current value for slot # index
  61. double value(unsigned char index) {
  62. _error = SENSOR_ERROR_OK;
  63. if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
  64. _error = SENSOR_ERROR_OUT_OF_RANGE;
  65. return 0;
  66. }
  67. protected:
  68. // ---------------------------------------------------------------------
  69. // Protected
  70. // ---------------------------------------------------------------------
  71. unsigned char _gpio;
  72. unsigned char _mode;
  73. bool _default = false;
  74. };