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.

115 lines
3.1 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. #define SENSOR_ERROR_GPIO_USED 7 // The GPIO is already in use
  39. class BaseSensor {
  40. public:
  41. // Constructor
  42. BaseSensor() {}
  43. // Destructor
  44. ~BaseSensor() {}
  45. // Initialization method, must be idempotent
  46. virtual void begin() {}
  47. // Loop-like method, call it in your main loop
  48. virtual void tick() {}
  49. // Pre-read hook (usually to populate registers with up-to-date data)
  50. virtual void pre() {}
  51. // Post-read hook (usually to reset things)
  52. virtual void post() {}
  53. // Descriptive name of the sensor
  54. virtual String description() {}
  55. // Type for slot # index
  56. virtual magnitude_t type(unsigned char index) {}
  57. // Current value for slot # index
  58. virtual double value(unsigned char index) {}
  59. // Retrieve current instance configuration
  60. virtual void getConfig(JsonObject& root) {};
  61. // Save current instance configuration
  62. virtual void setConfig(JsonObject& root) {};
  63. // Load the configuration manifest
  64. static void manifest(JsonArray& root) {};
  65. // Descriptive name of the slot # index
  66. String slot(unsigned char index) { return description(); }
  67. // Sensor ID
  68. unsigned char getID() { return _sensor_id; };
  69. // Return sensor status (true for ready)
  70. bool status() { return _error == 0; }
  71. // Return sensor last internal error
  72. int error() { return _error; }
  73. // Number of available slots
  74. unsigned char count() { return _count; }
  75. protected:
  76. unsigned char _sensor_id = 0x00;
  77. int _error = 0;
  78. bool _dirty = true;
  79. unsigned char _count = 0;
  80. };