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.

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