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.

157 lines
4.3 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 <cstddef>
  8. #include <cstdint>
  9. #include <functional>
  10. class BaseSensor {
  11. public:
  12. // Must implement as virtual.
  13. // Allows inhereting class correctly call it's own destructor through the ~BaseSensor()
  14. virtual ~BaseSensor() {
  15. }
  16. // Initialization method, must be idempotent
  17. virtual void begin() {
  18. }
  19. // Loop-like method, call it in your main loop
  20. virtual void tick() {
  21. }
  22. // Pre-read hook (usually to populate registers with up-to-date data)
  23. virtual void pre() {
  24. }
  25. // Post-read hook (usually to reset things)
  26. virtual void post() {
  27. }
  28. // Type of sensor
  29. virtual unsigned char type() {
  30. return sensor::type::Base;
  31. }
  32. // Number of decimals for a unit (or -1 for default)
  33. virtual signed char decimals(sensor::Unit) {
  34. return -1;
  35. }
  36. // Generic calibration
  37. virtual void calibrate() {
  38. };
  39. // Sensor ID
  40. virtual unsigned char getID() {
  41. return _sensor_id;
  42. };
  43. // Return status (true if no errors)
  44. bool status() {
  45. return 0 == _error;
  46. }
  47. // Return ready status (true for ready)
  48. bool ready() {
  49. return _ready;
  50. }
  51. // Return sensor last internal error
  52. int error() {
  53. return _error;
  54. }
  55. // Number of available slots
  56. unsigned char count() {
  57. return _count;
  58. }
  59. // Convert slot # index to a magnitude # index
  60. virtual unsigned char local(unsigned char slot) {
  61. return 0;
  62. }
  63. // Specify units attached to magnitudes
  64. virtual sensor::Unit units(unsigned char index) {
  65. switch (type(index)) {
  66. case MAGNITUDE_TEMPERATURE:
  67. return sensor::Unit::Celcius;
  68. case MAGNITUDE_HUMIDITY:
  69. case MAGNITUDE_POWER_FACTOR:
  70. return sensor::Unit::Percentage;
  71. case MAGNITUDE_PRESSURE:
  72. return sensor::Unit::Hectopascal;
  73. case MAGNITUDE_CURRENT:
  74. return sensor::Unit::Ampere;
  75. case MAGNITUDE_VOLTAGE:
  76. return sensor::Unit::Volt;
  77. case MAGNITUDE_POWER_ACTIVE:
  78. return sensor::Unit::Watt;
  79. case MAGNITUDE_POWER_APPARENT:
  80. return sensor::Unit::Voltampere;
  81. case MAGNITUDE_POWER_REACTIVE:
  82. return sensor::Unit::VoltampereReactive;
  83. case MAGNITUDE_ENERGY_DELTA:
  84. return sensor::Unit::Joule;
  85. case MAGNITUDE_ENERGY:
  86. return sensor::Unit::KilowattHour;
  87. case MAGNITUDE_PM1dot0:
  88. case MAGNITUDE_PM2dot5:
  89. return sensor::Unit::MicrogrammPerCubicMeter;
  90. case MAGNITUDE_CO:
  91. case MAGNITUDE_CO2:
  92. case MAGNITUDE_NO2:
  93. case MAGNITUDE_VOC:
  94. return sensor::Unit::PartsPerMillion;
  95. case MAGNITUDE_LUX:
  96. return sensor::Unit::Lux;
  97. case MAGNITUDE_RESISTANCE:
  98. return sensor::Unit::Ohm;
  99. case MAGNITUDE_HCHO:
  100. return sensor::Unit::MilligrammPerCubicMeter;
  101. case MAGNITUDE_GEIGER_CPM:
  102. return sensor::Unit::CountsPerMinute;
  103. case MAGNITUDE_GEIGER_SIEVERT:
  104. return sensor::Unit::MicrosievertPerHour;
  105. case MAGNITUDE_DISTANCE:
  106. return sensor::Unit::Meter;
  107. case MAGNITUDE_FREQUENCY:
  108. return sensor::Unit::Hertz;
  109. case MAGNITUDE_PH:
  110. return sensor::Unit::Ph;
  111. default:
  112. break;
  113. }
  114. return sensor::Unit::None;
  115. }
  116. // Descriptive name of the sensor
  117. virtual String description() = 0;
  118. // Descriptive name of the slot # index
  119. virtual String description(unsigned char index) = 0;
  120. // Address of the sensor (it could be the GPIO or I2C address)
  121. virtual String address(unsigned char index) = 0;
  122. // Type for slot # index
  123. virtual unsigned char type(unsigned char index) = 0;
  124. // Current value for slot # index
  125. virtual double value(unsigned char index) = 0;
  126. protected:
  127. unsigned char _sensor_id = 0;
  128. int _error = 0;
  129. bool _dirty = true;
  130. unsigned char _count = 0;
  131. bool _ready = false;
  132. };