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.

144 lines
4.1 KiB

  1. // -----------------------------------------------------------------------------
  2. // Event Counter Sensor
  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 EventSensor : public BaseSensor {
  9. public:
  10. // ---------------------------------------------------------------------
  11. // Public
  12. // ---------------------------------------------------------------------
  13. EventSensor(): BaseSensor() {
  14. _count = 1;
  15. _sensor_id = SENSOR_EVENTS_ID;
  16. }
  17. ~EventSensor() {
  18. detachInterrupt(_gpio);
  19. }
  20. // ---------------------------------------------------------------------
  21. void setGPIO(unsigned char gpio) {
  22. _gpio = gpio;
  23. }
  24. void setMode(unsigned char mode) {
  25. _mode = mode;
  26. }
  27. void setInterruptMode(unsigned char mode) {
  28. _interrupt_mode = mode;
  29. }
  30. void setDebounceTime(unsigned long debounce) {
  31. _debounce = debounce;
  32. }
  33. // ---------------------------------------------------------------------
  34. unsigned char getGPIO() {
  35. return _gpio;
  36. }
  37. unsigned char getMode() {
  38. return _mode;
  39. }
  40. unsigned char getInterruptMode() {
  41. return _interrupt_mode;
  42. }
  43. unsigned long getDebounceTime() {
  44. return _debounce;
  45. }
  46. // ---------------------------------------------------------------------
  47. // Sensors API
  48. // ---------------------------------------------------------------------
  49. // Initialization method, must be idempotent
  50. // Defined outside the class body
  51. void begin() {
  52. if (_interrupt_gpio != GPIO_NONE) detach(_interrupt_gpio);
  53. pinMode(_gpio, _mode);
  54. attach(this, _gpio, _interrupt_mode);
  55. }
  56. // Descriptive name of the sensor
  57. String description() {
  58. char buffer[20];
  59. snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
  60. return String(buffer);
  61. }
  62. // Descriptive name of the slot # index
  63. String slot(unsigned char index) {
  64. (void) index;
  65. return description();
  66. }
  67. // Type for slot # index
  68. magnitude_t type(unsigned char index) {
  69. _error = SENSOR_ERROR_OK;
  70. if (index == 0) return MAGNITUDE_EVENTS;
  71. _error = SENSOR_ERROR_OUT_OF_RANGE;
  72. return MAGNITUDE_NONE;
  73. }
  74. // Current value for slot # index
  75. double value(unsigned char index) {
  76. _error = SENSOR_ERROR_OK;
  77. if (index == 0) {
  78. double value = _events;
  79. _events = 0;
  80. return value;
  81. };
  82. _error = SENSOR_ERROR_OUT_OF_RANGE;
  83. return 0;
  84. }
  85. // Handle interrupt calls
  86. void handleInterrupt(unsigned char gpio) {
  87. (void) gpio;
  88. static unsigned long last = 0;
  89. if (millis() - last > _debounce) {
  90. _events = _events + 1;
  91. last = millis();
  92. }
  93. }
  94. // Interrupt attach callback
  95. void attached(unsigned char gpio) {
  96. BaseSensor::attached(gpio);
  97. _interrupt_gpio = gpio;
  98. }
  99. // Interrupt detach callback
  100. void detached(unsigned char gpio) {
  101. BaseSensor::detached(gpio);
  102. if (_interrupt_gpio == gpio) _interrupt_gpio = GPIO_NONE;
  103. }
  104. protected:
  105. // ---------------------------------------------------------------------
  106. // Protected
  107. // ---------------------------------------------------------------------
  108. volatile unsigned long _events = 0;
  109. unsigned long _debounce = EVENTS_DEBOUNCE;
  110. unsigned char _gpio;
  111. unsigned char _mode;
  112. unsigned char _interrupt_mode;
  113. unsigned char _interrupt_gpio = GPIO_NONE;
  114. };