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