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.

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