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.

147 lines
4.8 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include <Arduino.h>
  7. #include <ArduinoJson.h>
  8. #include <functional>
  9. #include "../sensor.h"
  10. using TSensorCallback = std::function<void(unsigned char, double)>;
  11. class BaseSensor {
  12. public:
  13. // Constructor
  14. BaseSensor() {}
  15. // Destructor
  16. ~BaseSensor() {}
  17. // Initialization method, must be idempotent
  18. virtual void begin() {}
  19. // Loop-like method, call it in your main loop
  20. virtual void tick() {}
  21. // Pre-read hook (usually to populate registers with up-to-date data)
  22. virtual void pre() {}
  23. // Post-read hook (usually to reset things)
  24. virtual void post() {}
  25. // Descriptive name of the sensor
  26. virtual String description() = 0;
  27. // Address of the sensor (it could be the GPIO or I2C address)
  28. virtual String address(unsigned char index) = 0;
  29. // Descriptive name of the slot # index
  30. virtual String slot(unsigned char index) = 0;
  31. // Type of sensor
  32. virtual unsigned char type() { return sensor::type::Base; }
  33. // Type for slot # index
  34. virtual unsigned char type(unsigned char index) = 0;
  35. // Number of decimals for a unit (or -1 for default)
  36. virtual signed char decimals(sensor::Unit) { return -1; }
  37. // Current value for slot # index
  38. virtual double value(unsigned char index) = 0;
  39. // Generic calibration
  40. virtual void calibrate() {};
  41. // Retrieve current instance configuration
  42. virtual void getConfig(JsonObject& root) {};
  43. // Save current instance configuration
  44. virtual void setConfig(JsonObject& root) {};
  45. // Load the configuration manifest
  46. static void manifest(JsonArray& root) {};
  47. // Sensor ID
  48. unsigned char getID() { return _sensor_id; };
  49. // Return status (true if no errors)
  50. bool status() { return 0 == _error; }
  51. // Return ready status (true for ready)
  52. bool ready() { return _ready; }
  53. // Return sensor last internal error
  54. int error() { return _error; }
  55. // Number of available slots
  56. unsigned char count() { return _count; }
  57. // Hook for event callback
  58. void onEvent(TSensorCallback fn) { _callback = fn; };
  59. // Specify units attached to magnitudes
  60. virtual sensor::Unit units(unsigned char type) {
  61. switch (type) {
  62. case MAGNITUDE_TEMPERATURE:
  63. return sensor::Unit::Celcius;
  64. case MAGNITUDE_HUMIDITY:
  65. case MAGNITUDE_POWER_FACTOR:
  66. return sensor::Unit::Percentage;
  67. case MAGNITUDE_PRESSURE:
  68. return sensor::Unit::Hectopascal;
  69. case MAGNITUDE_CURRENT:
  70. return sensor::Unit::Ampere;
  71. case MAGNITUDE_VOLTAGE:
  72. return sensor::Unit::Volt;
  73. case MAGNITUDE_POWER_ACTIVE:
  74. return sensor::Unit::Watt;
  75. case MAGNITUDE_POWER_APPARENT:
  76. return sensor::Unit::Voltampere;
  77. case MAGNITUDE_POWER_REACTIVE:
  78. return sensor::Unit::VoltampereReactive;
  79. case MAGNITUDE_ENERGY_DELTA:
  80. return sensor::Unit::Joule;
  81. case MAGNITUDE_ENERGY:
  82. return sensor::Unit::KilowattHour;
  83. case MAGNITUDE_PM1dot0:
  84. case MAGNITUDE_PM2dot5:
  85. return sensor::Unit::MicrogrammPerCubicMeter;
  86. case MAGNITUDE_CO2:
  87. case MAGNITUDE_NO2:
  88. case MAGNITUDE_CO:
  89. return sensor::Unit::PartsPerMillion;
  90. case MAGNITUDE_LUX:
  91. return sensor::Unit::Lux;
  92. case MAGNITUDE_RESISTANCE:
  93. return sensor::Unit::Ohm;
  94. case MAGNITUDE_HCHO:
  95. return sensor::Unit::MilligrammPerCubicMeter;
  96. case MAGNITUDE_GEIGER_CPM:
  97. return sensor::Unit::CountsPerMinute;
  98. case MAGNITUDE_GEIGER_SIEVERT:
  99. return sensor::Unit::MicrosievertPerHour;
  100. case MAGNITUDE_DISTANCE:
  101. return sensor::Unit::Meter;
  102. default:
  103. return sensor::Unit::None;
  104. }
  105. }
  106. protected:
  107. TSensorCallback _callback = NULL;
  108. unsigned char _sensor_id = 0x00;
  109. int _error = 0;
  110. bool _dirty = true;
  111. unsigned char _count = 0;
  112. bool _ready = false;
  113. };