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.

166 lines
4.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include <Arduino.h>
  7. typedef enum magnitude_t {
  8. MAGNITUDE_NONE = 0,
  9. MAGNITUDE_TEMPERATURE,
  10. MAGNITUDE_HUMIDITY,
  11. MAGNITUDE_PRESSURE,
  12. MAGNITUDE_CURRENT,
  13. MAGNITUDE_VOLTAGE,
  14. MAGNITUDE_POWER_ACTIVE,
  15. MAGNITUDE_POWER_APPARENT,
  16. MAGNITUDE_POWER_REACTIVE,
  17. MAGNITUDE_ENERGY,
  18. MAGNITUDE_ENERGY_DELTA,
  19. MAGNITUDE_POWER_FACTOR,
  20. MAGNITUDE_ANALOG,
  21. MAGNITUDE_DIGITAL,
  22. MAGNITUDE_EVENTS,
  23. MAGNITUDE_PM1dot0,
  24. MAGNITUDE_PM2dot5,
  25. MAGNITUDE_PM10,
  26. MAGNITUDE_CO2,
  27. MAGNITUDE_MAX,
  28. } magnitude_t;
  29. #define GPIO_NONE 0x99
  30. #define SENSOR_ERROR_OK 0 // No error
  31. #define SENSOR_ERROR_OUT_OF_RANGE 1 // Result out of sensor range
  32. #define SENSOR_ERROR_WARM_UP 2 // Sensor is warming-up
  33. #define SENSOR_ERROR_TIMEOUT 3 // Response from sensor timed out
  34. #define SENSOR_ERROR_UNKNOWN_ID 4 // Sensor did not report a known ID
  35. #define SENSOR_ERROR_CRC 5 // Sensor data corrupted
  36. class BaseSensor {
  37. public:
  38. // Constructor
  39. BaseSensor() {}
  40. // Destructor
  41. ~BaseSensor() {}
  42. // Initialization method, must be idempotent
  43. virtual void begin() {}
  44. // Loop-like method, call it in your main loop
  45. virtual void tick() {}
  46. // Pre-read hook (usually to populate registers with up-to-date data)
  47. virtual void pre() {}
  48. // Post-read hook (usually to reset things)
  49. virtual void post() {}
  50. // Descriptive name of the sensor
  51. virtual String name() {}
  52. // Descriptive name of the slot # index
  53. virtual String slot(unsigned char index) {}
  54. // Type for slot # index
  55. virtual magnitude_t type(unsigned char index) {}
  56. // Current value for slot # index
  57. virtual double value(unsigned char index) {}
  58. // Return sensor status (true for ready)
  59. bool status() { return _error == 0; }
  60. // Return sensor last internal error
  61. int error() { return _error; }
  62. // Number of available slots
  63. unsigned char count() { return _count; }
  64. // Handle interrupt calls
  65. virtual void handleInterrupt(unsigned char gpio) {}
  66. // Interrupt attach callback
  67. void attached(unsigned char gpio) {
  68. #if SENSOR_DEBUG
  69. Serial.printf("[SENSOR] GPIO%d interrupt attached to %s\n", gpio, name().c_str());
  70. #endif
  71. }
  72. // Interrupt detach callback
  73. void detached(unsigned char gpio) {
  74. #if SENSOR_DEBUG
  75. Serial.printf("[SENSOR] GPIO%d interrupt detached from %s\n", gpio, name().c_str());
  76. #endif
  77. }
  78. protected:
  79. // Attach interrupt
  80. void attach(BaseSensor * instance, unsigned char gpio, unsigned char mode);
  81. // Detach interrupt
  82. void detach(unsigned char gpio);
  83. int _error = 0;
  84. unsigned char _count = 0;
  85. };
  86. // -----------------------------------------------------------------------------
  87. // Interrupt helpers
  88. // -----------------------------------------------------------------------------
  89. BaseSensor * _isr_sensor_instance[16] = {NULL};
  90. void _sensor_isr(unsigned char gpio) {
  91. if (_isr_sensor_instance[gpio]) {
  92. _isr_sensor_instance[gpio]->handleInterrupt(gpio);
  93. }
  94. }
  95. void _sensor_isr_0() { _sensor_isr(0); }
  96. void _sensor_isr_2() { _sensor_isr(2); }
  97. void _sensor_isr_4() { _sensor_isr(4); }
  98. void _sensor_isr_5() { _sensor_isr(5); }
  99. void _sensor_isr_12() { _sensor_isr(12); }
  100. void _sensor_isr_13() { _sensor_isr(13); }
  101. void _sensor_isr_14() { _sensor_isr(14); }
  102. void _sensor_isr_15() { _sensor_isr(15); }
  103. void (*_sensor_isrs[16])() = {
  104. _sensor_isr_0, NULL, _sensor_isr_2, NULL, _sensor_isr_4, _sensor_isr_5,
  105. NULL, NULL, NULL, NULL, NULL, NULL,
  106. _sensor_isr_12, _sensor_isr_13, _sensor_isr_14, _sensor_isr_15
  107. };
  108. void BaseSensor::attach(BaseSensor * instance, unsigned char gpio, unsigned char mode) {
  109. detach(gpio);
  110. if (_sensor_isrs[gpio]) {
  111. _isr_sensor_instance[gpio] = instance;
  112. attachInterrupt(gpio, _sensor_isrs[gpio], mode);
  113. instance->attached(gpio);
  114. }
  115. }
  116. void BaseSensor::detach(unsigned char gpio) {
  117. if (_isr_sensor_instance[gpio]) {
  118. detachInterrupt(gpio);
  119. _isr_sensor_instance[gpio]->detached(gpio);
  120. _isr_sensor_instance[gpio] = NULL;
  121. }
  122. }