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.

151 lines
5.0 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. virtual ~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. // Descriptive name of the slot # index
  28. virtual String description(unsigned char index) = 0;
  29. // Address of the sensor (it could be the GPIO or I2C address)
  30. virtual String address(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. // Convert slot # index to a magnitude # index
  58. virtual unsigned char local(unsigned char slot) { return 0; }
  59. // Hook for event callback
  60. void onEvent(TSensorCallback fn) { _callback = fn; };
  61. // Specify units attached to magnitudes
  62. virtual sensor::Unit units(unsigned char index) {
  63. switch (type(index)) {
  64. case MAGNITUDE_TEMPERATURE:
  65. return sensor::Unit::Celcius;
  66. case MAGNITUDE_HUMIDITY:
  67. return sensor::Unit::Percentage;
  68. case MAGNITUDE_PRESSURE:
  69. return sensor::Unit::Hectopascal;
  70. case MAGNITUDE_CURRENT:
  71. return sensor::Unit::Ampere;
  72. case MAGNITUDE_VOLTAGE:
  73. return sensor::Unit::Volt;
  74. case MAGNITUDE_POWER_ACTIVE:
  75. return sensor::Unit::Watt;
  76. case MAGNITUDE_POWER_APPARENT:
  77. return sensor::Unit::Voltampere;
  78. case MAGNITUDE_POWER_REACTIVE:
  79. return sensor::Unit::VoltampereReactive;
  80. case MAGNITUDE_ENERGY_DELTA:
  81. return sensor::Unit::Joule;
  82. case MAGNITUDE_ENERGY:
  83. return sensor::Unit::KilowattHour;
  84. case MAGNITUDE_PM1dot0:
  85. case MAGNITUDE_PM2dot5:
  86. return sensor::Unit::MicrogrammPerCubicMeter;
  87. case MAGNITUDE_CO2:
  88. case MAGNITUDE_NO2:
  89. case MAGNITUDE_CO:
  90. return sensor::Unit::PartsPerMillion;
  91. case MAGNITUDE_LUX:
  92. return sensor::Unit::Lux;
  93. case MAGNITUDE_RESISTANCE:
  94. return sensor::Unit::Ohm;
  95. case MAGNITUDE_HCHO:
  96. return sensor::Unit::MilligrammPerCubicMeter;
  97. case MAGNITUDE_GEIGER_CPM:
  98. return sensor::Unit::CountsPerMinute;
  99. case MAGNITUDE_GEIGER_SIEVERT:
  100. return sensor::Unit::MicrosievertPerHour;
  101. case MAGNITUDE_DISTANCE:
  102. return sensor::Unit::Meter;
  103. case MAGNITUDE_FREQUENCY:
  104. return sensor::Unit::Hertz;
  105. default:
  106. return sensor::Unit::None;
  107. }
  108. }
  109. protected:
  110. TSensorCallback _callback = NULL;
  111. unsigned char _sensor_id = 0x00;
  112. int _error = 0;
  113. bool _dirty = true;
  114. unsigned char _count = 0;
  115. bool _ready = false;
  116. };