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.

138 lines
3.9 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. // Type for slot # index
  63. magnitude_t type(unsigned char index) {
  64. _error = SENSOR_ERROR_OK;
  65. if (index == 0) return MAGNITUDE_EVENTS;
  66. _error = SENSOR_ERROR_OUT_OF_RANGE;
  67. return MAGNITUDE_NONE;
  68. }
  69. // Current value for slot # index
  70. double value(unsigned char index) {
  71. _error = SENSOR_ERROR_OK;
  72. if (index == 0) {
  73. double value = _events;
  74. _events = 0;
  75. return value;
  76. };
  77. _error = SENSOR_ERROR_OUT_OF_RANGE;
  78. return 0;
  79. }
  80. // Handle interrupt calls
  81. void handleInterrupt(unsigned char gpio) {
  82. (void) gpio;
  83. static unsigned long last = 0;
  84. if (millis() - last > _debounce) {
  85. _events = _events + 1;
  86. last = millis();
  87. }
  88. }
  89. // Interrupt attach callback
  90. void attached(unsigned char gpio) {
  91. BaseSensor::attached(gpio);
  92. _interrupt_gpio = gpio;
  93. }
  94. // Interrupt detach callback
  95. void detached(unsigned char gpio) {
  96. BaseSensor::detached(gpio);
  97. if (_interrupt_gpio == gpio) _interrupt_gpio = GPIO_NONE;
  98. }
  99. protected:
  100. // ---------------------------------------------------------------------
  101. // Protected
  102. // ---------------------------------------------------------------------
  103. volatile unsigned long _events = 0;
  104. unsigned long _debounce = EVENTS_DEBOUNCE;
  105. unsigned char _gpio;
  106. unsigned char _mode;
  107. unsigned char _interrupt_mode;
  108. unsigned char _interrupt_gpio = GPIO_NONE;
  109. };