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.

167 lines
4.7 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. bool _dirty = true;
  85. unsigned char _count = 0;
  86. };
  87. // -----------------------------------------------------------------------------
  88. // Interrupt helpers
  89. // -----------------------------------------------------------------------------
  90. BaseSensor * _isr_sensor_instance[16] = {NULL};
  91. void _sensor_isr(unsigned char gpio) {
  92. if (_isr_sensor_instance[gpio]) {
  93. _isr_sensor_instance[gpio]->handleInterrupt(gpio);
  94. }
  95. }
  96. void _sensor_isr_0() { _sensor_isr(0); }
  97. void _sensor_isr_2() { _sensor_isr(2); }
  98. void _sensor_isr_4() { _sensor_isr(4); }
  99. void _sensor_isr_5() { _sensor_isr(5); }
  100. void _sensor_isr_12() { _sensor_isr(12); }
  101. void _sensor_isr_13() { _sensor_isr(13); }
  102. void _sensor_isr_14() { _sensor_isr(14); }
  103. void _sensor_isr_15() { _sensor_isr(15); }
  104. void (*_sensor_isrs[16])() = {
  105. _sensor_isr_0, NULL, _sensor_isr_2, NULL, _sensor_isr_4, _sensor_isr_5,
  106. NULL, NULL, NULL, NULL, NULL, NULL,
  107. _sensor_isr_12, _sensor_isr_13, _sensor_isr_14, _sensor_isr_15
  108. };
  109. void BaseSensor::attach(BaseSensor * instance, unsigned char gpio, unsigned char mode) {
  110. detach(gpio);
  111. if (_sensor_isrs[gpio]) {
  112. _isr_sensor_instance[gpio] = instance;
  113. attachInterrupt(gpio, _sensor_isrs[gpio], mode);
  114. instance->attached(gpio);
  115. }
  116. }
  117. void BaseSensor::detach(unsigned char gpio) {
  118. if (_isr_sensor_instance[gpio]) {
  119. detachInterrupt(gpio);
  120. _isr_sensor_instance[gpio]->detached(gpio);
  121. _isr_sensor_instance[gpio] = NULL;
  122. }
  123. }