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.

209 lines
6.0 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. #include <ArduinoJson.h>
  8. typedef enum magnitude_t {
  9. MAGNITUDE_NONE = 0,
  10. MAGNITUDE_TEMPERATURE,
  11. MAGNITUDE_HUMIDITY,
  12. MAGNITUDE_PRESSURE,
  13. MAGNITUDE_CURRENT,
  14. MAGNITUDE_VOLTAGE,
  15. MAGNITUDE_POWER_ACTIVE,
  16. MAGNITUDE_POWER_APPARENT,
  17. MAGNITUDE_POWER_REACTIVE,
  18. MAGNITUDE_ENERGY,
  19. MAGNITUDE_ENERGY_DELTA,
  20. MAGNITUDE_POWER_FACTOR,
  21. MAGNITUDE_ANALOG,
  22. MAGNITUDE_DIGITAL,
  23. MAGNITUDE_EVENTS,
  24. MAGNITUDE_PM1dot0,
  25. MAGNITUDE_PM2dot5,
  26. MAGNITUDE_PM10,
  27. MAGNITUDE_CO2,
  28. MAGNITUDE_MAX,
  29. } magnitude_t;
  30. #define GPIO_NONE 0x99
  31. #define SENSOR_ERROR_OK 0 // No error
  32. #define SENSOR_ERROR_OUT_OF_RANGE 1 // Result out of sensor range
  33. #define SENSOR_ERROR_WARM_UP 2 // Sensor is warming-up
  34. #define SENSOR_ERROR_TIMEOUT 3 // Response from sensor timed out
  35. #define SENSOR_ERROR_UNKNOWN_ID 4 // Sensor did not report a known ID
  36. #define SENSOR_ERROR_CRC 5 // Sensor data corrupted
  37. #define SENSOR_ERROR_I2C 6 // Wrong or locked I2C address
  38. class BaseSensor {
  39. public:
  40. // Constructor
  41. BaseSensor() {}
  42. // Destructor
  43. ~BaseSensor() {}
  44. // Initialization method, must be idempotent
  45. virtual void begin() {}
  46. // Loop-like method, call it in your main loop
  47. virtual void tick() {}
  48. // Pre-read hook (usually to populate registers with up-to-date data)
  49. virtual void pre() {}
  50. // Post-read hook (usually to reset things)
  51. virtual void post() {}
  52. // Descriptive name of the sensor
  53. virtual String description() {}
  54. // Descriptive name of the slot # index
  55. virtual String slot(unsigned char index) {}
  56. // Type for slot # index
  57. virtual magnitude_t type(unsigned char index) {}
  58. // Current value for slot # index
  59. virtual double value(unsigned char index) {}
  60. // Retrieve current instance configuration
  61. virtual void getConfig(JsonObject& root) {};
  62. // Save current instance configuration
  63. virtual void setConfig(JsonObject& root) {};
  64. // Load the configuration manifest
  65. static void manifest(JsonObject& root) {};
  66. // Specific for I2C sensors
  67. unsigned char lock_i2c(unsigned char address, size_t size, unsigned char * addresses) {
  68. // Check if we should release a previously locked address
  69. if (_previous_address != address) {
  70. i2cReleaseLock(_previous_address);
  71. }
  72. // If we have already an address, check it is not locked
  73. if (address && !i2cGetLock(address)) {
  74. _error = SENSOR_ERROR_I2C;
  75. // If we don't have an address...
  76. } else {
  77. // Trigger auto-discover
  78. address = i2cFindAndLock(size, addresses);
  79. // If still nothing exit with error
  80. if (address == 0) _error = SENSOR_ERROR_I2C;
  81. }
  82. _previous_address = address;
  83. return address;
  84. }
  85. // Return sensor status (true for ready)
  86. bool status() { return _error == 0; }
  87. // Return sensor last internal error
  88. int error() { return _error; }
  89. // Number of available slots
  90. unsigned char count() { return _count; }
  91. // Handle interrupt calls
  92. virtual void handleInterrupt(unsigned char gpio) {}
  93. // Interrupt attach callback
  94. void attached(unsigned char gpio) {
  95. #if SENSOR_DEBUG
  96. DEBUG_MSG("[SENSOR] GPIO%d interrupt attached to %s\n", gpio, description().c_str());
  97. #endif
  98. }
  99. // Interrupt detach callback
  100. void detached(unsigned char gpio) {
  101. #if SENSOR_DEBUG
  102. DEBUG_MSG("[SENSOR] GPIO%d interrupt detached from %s\n", gpio, description().c_str());
  103. #endif
  104. }
  105. protected:
  106. // Attach interrupt
  107. void attach(BaseSensor * instance, unsigned char gpio, unsigned char mode);
  108. // Detach interrupt
  109. void detach(unsigned char gpio);
  110. int _error = 0;
  111. bool _dirty = true;
  112. unsigned char _count = 0;
  113. // I2C
  114. unsigned char _previous_address = 0;
  115. unsigned char _address = 0;
  116. };
  117. // -----------------------------------------------------------------------------
  118. // Interrupt helpers
  119. // -----------------------------------------------------------------------------
  120. BaseSensor * _isr_sensor_instance[16] = {NULL};
  121. void _sensor_isr(unsigned char gpio) {
  122. if (_isr_sensor_instance[gpio]) {
  123. _isr_sensor_instance[gpio]->handleInterrupt(gpio);
  124. }
  125. }
  126. void _sensor_isr_0() { _sensor_isr(0); }
  127. void _sensor_isr_2() { _sensor_isr(2); }
  128. void _sensor_isr_4() { _sensor_isr(4); }
  129. void _sensor_isr_5() { _sensor_isr(5); }
  130. void _sensor_isr_12() { _sensor_isr(12); }
  131. void _sensor_isr_13() { _sensor_isr(13); }
  132. void _sensor_isr_14() { _sensor_isr(14); }
  133. void _sensor_isr_15() { _sensor_isr(15); }
  134. void (*_sensor_isrs[16])() = {
  135. _sensor_isr_0, NULL, _sensor_isr_2, NULL, _sensor_isr_4, _sensor_isr_5,
  136. NULL, NULL, NULL, NULL, NULL, NULL,
  137. _sensor_isr_12, _sensor_isr_13, _sensor_isr_14, _sensor_isr_15
  138. };
  139. void BaseSensor::attach(BaseSensor * instance, unsigned char gpio, unsigned char mode) {
  140. detach(gpio);
  141. if (_sensor_isrs[gpio]) {
  142. _isr_sensor_instance[gpio] = instance;
  143. attachInterrupt(gpio, _sensor_isrs[gpio], mode);
  144. instance->attached(gpio);
  145. }
  146. }
  147. void BaseSensor::detach(unsigned char gpio) {
  148. if (_isr_sensor_instance[gpio]) {
  149. detachInterrupt(gpio);
  150. _isr_sensor_instance[gpio]->detached(gpio);
  151. _isr_sensor_instance[gpio] = NULL;
  152. }
  153. }