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.

104 lines
2.8 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. // Type for slot # index
  56. unsigned char type(unsigned char index) {
  57. _error = SENSOR_ERROR_OK;
  58. if (index == 0) return MAGNITUDE_DIGITAL;
  59. _error = SENSOR_ERROR_OUT_OF_RANGE;
  60. return MAGNITUDE_NONE;
  61. }
  62. // Current value for slot # index
  63. double value(unsigned char index) {
  64. _error = SENSOR_ERROR_OK;
  65. if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
  66. _error = SENSOR_ERROR_OUT_OF_RANGE;
  67. return 0;
  68. }
  69. protected:
  70. // ---------------------------------------------------------------------
  71. // Protected
  72. // ---------------------------------------------------------------------
  73. unsigned char _gpio;
  74. unsigned char _mode;
  75. bool _default = false;
  76. };
  77. #endif // SENSOR_SUPPORT && DIGITAL_SUPPORT