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.

101 lines
2.5 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 SENSOR_ERROR_OK 0 // No error
  30. #define SENSOR_ERROR_OUT_OF_RANGE 1 // Result out of sensor range
  31. #define SENSOR_ERROR_WARM_UP 2 // Sensor is warming-up
  32. #define SENSOR_ERROR_TIMEOUT 3 // Response from sensor timed out
  33. #define SENSOR_ERROR_UNKNOWN_ID 4 // Sensor did not report a known ID
  34. #define SENSOR_ERROR_CRC 5 // Sensor data corrupted
  35. class BaseSensor {
  36. public:
  37. // Constructor
  38. BaseSensor() {}
  39. // Destructor
  40. ~BaseSensor() {}
  41. // General interrupt handler
  42. virtual void InterruptHandler() {}
  43. // Initialization method, must be idempotent
  44. virtual void begin() {}
  45. // Loop-like method, call it in your main loop
  46. virtual void tick() {}
  47. // Pre-read hook (usually to populate registers with up-to-date data)
  48. virtual void pre() {}
  49. // Post-read hook (usually to reset things)
  50. virtual void post() {}
  51. // Descriptive name of the sensor
  52. virtual String name() {}
  53. // Descriptive name of the slot # index
  54. virtual String slot(unsigned char index) {}
  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. // Return sensor status (true for ready)
  60. bool status() { return _error == 0; }
  61. // Return sensor last internal error
  62. int error() { return _error; }
  63. // Number of available slots
  64. unsigned char count() { return _count; }
  65. protected:
  66. int _error = 0;
  67. unsigned char _count = 0;
  68. };