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.

109 lines
3.0 KiB

  1. // -----------------------------------------------------------------------------
  2. // Digital Sensor (maps to a digitalRead)
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && DIGITAL_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. class DigitalSensor : public BaseSensor {
  10. public:
  11. // ---------------------------------------------------------------------
  12. // Public
  13. // ---------------------------------------------------------------------
  14. DigitalSensor(): BaseSensor() {
  15. _count = 1;
  16. _sensor_id = SENSOR_DIGITAL_ID;
  17. }
  18. // ---------------------------------------------------------------------
  19. void setGPIO(unsigned char gpio) {
  20. _gpio = gpio;
  21. }
  22. void setMode(unsigned char mode) {
  23. _mode = mode;
  24. }
  25. void setDefault(bool value) {
  26. _default = value;
  27. }
  28. // ---------------------------------------------------------------------
  29. unsigned char getGPIO() {
  30. return _gpio;
  31. }
  32. unsigned char getMode() {
  33. return _mode;
  34. }
  35. bool getDefault() {
  36. return _default;
  37. }
  38. // ---------------------------------------------------------------------
  39. // Sensor API
  40. // ---------------------------------------------------------------------
  41. // Initialization method, must be idempotent
  42. void begin() {
  43. pinMode(_gpio, _mode);
  44. }
  45. // Descriptive name of the sensor
  46. String description() {
  47. char buffer[20];
  48. snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
  49. return String(buffer);
  50. }
  51. // Descriptive name of the slot # index
  52. String slot(unsigned char index) {
  53. return description();
  54. };
  55. // Address of the sensor (it could be the GPIO or I2C address)
  56. String address(unsigned char index) {
  57. return String(_gpio);
  58. }
  59. // Type for slot # index
  60. unsigned char type(unsigned char index) {
  61. _error = SENSOR_ERROR_OK;
  62. if (index == 0) return MAGNITUDE_DIGITAL;
  63. _error = SENSOR_ERROR_OUT_OF_RANGE;
  64. return MAGNITUDE_NONE;
  65. }
  66. // Current value for slot # index
  67. double value(unsigned char index) {
  68. _error = SENSOR_ERROR_OK;
  69. if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
  70. _error = SENSOR_ERROR_OUT_OF_RANGE;
  71. return 0;
  72. }
  73. protected:
  74. // ---------------------------------------------------------------------
  75. // Protected
  76. // ---------------------------------------------------------------------
  77. unsigned char _gpio;
  78. unsigned char _mode;
  79. bool _default = false;
  80. };
  81. #endif // SENSOR_SUPPORT && DIGITAL_SUPPORT