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.

149 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. class BaseSensor {
  11. public:
  12. // Constructor
  13. BaseSensor() {}
  14. // Destructor
  15. virtual ~BaseSensor() {}
  16. // Initialization method, must be idempotent
  17. virtual void begin() {}
  18. // Loop-like method, call it in your main loop
  19. virtual void tick() {}
  20. // Pre-read hook (usually to populate registers with up-to-date data)
  21. virtual void pre() {}
  22. // Post-read hook (usually to reset things)
  23. virtual void post() {}
  24. // Descriptive name of the sensor
  25. virtual String description() = 0;
  26. // Descriptive name of the slot # index
  27. virtual String description(unsigned char index) = 0;
  28. // Address of the sensor (it could be the GPIO or I2C address)
  29. virtual String address(unsigned char index) = 0;
  30. // Type of sensor
  31. virtual unsigned char type() { return sensor::type::Base; }
  32. // Type for slot # index
  33. virtual unsigned char type(unsigned char index) = 0;
  34. // Number of decimals for a unit (or -1 for default)
  35. virtual signed char decimals(sensor::Unit) { return -1; }
  36. // Current value for slot # index
  37. virtual double value(unsigned char index) = 0;
  38. // Generic calibration
  39. virtual void calibrate() {};
  40. // Retrieve current instance configuration
  41. virtual void getConfig(JsonObject& root) {};
  42. // Save current instance configuration
  43. virtual void setConfig(JsonObject& root) {};
  44. // Load the configuration manifest
  45. static void manifest(JsonArray& root) {};
  46. // Sensor ID
  47. unsigned char getID() { return _sensor_id; };
  48. // Return status (true if no errors)
  49. bool status() { return 0 == _error; }
  50. // Return ready status (true for ready)
  51. bool ready() { return _ready; }
  52. // Return sensor last internal error
  53. int error() { return _error; }
  54. // Number of available slots
  55. unsigned char count() { return _count; }
  56. // Convert slot # index to a magnitude # index
  57. virtual unsigned char local(unsigned char slot) { return 0; }
  58. // Specify units attached to magnitudes
  59. virtual sensor::Unit units(unsigned char index) {
  60. switch (type(index)) {
  61. case MAGNITUDE_TEMPERATURE:
  62. return sensor::Unit::Celcius;
  63. case MAGNITUDE_HUMIDITY:
  64. case MAGNITUDE_POWER_FACTOR:
  65. return sensor::Unit::Percentage;
  66. case MAGNITUDE_PRESSURE:
  67. return sensor::Unit::Hectopascal;
  68. case MAGNITUDE_CURRENT:
  69. return sensor::Unit::Ampere;
  70. case MAGNITUDE_VOLTAGE:
  71. return sensor::Unit::Volt;
  72. case MAGNITUDE_POWER_ACTIVE:
  73. return sensor::Unit::Watt;
  74. case MAGNITUDE_POWER_APPARENT:
  75. return sensor::Unit::Voltampere;
  76. case MAGNITUDE_POWER_REACTIVE:
  77. return sensor::Unit::VoltampereReactive;
  78. case MAGNITUDE_ENERGY_DELTA:
  79. return sensor::Unit::Joule;
  80. case MAGNITUDE_ENERGY:
  81. return sensor::Unit::KilowattHour;
  82. case MAGNITUDE_PM1dot0:
  83. case MAGNITUDE_PM2dot5:
  84. return sensor::Unit::MicrogrammPerCubicMeter;
  85. case MAGNITUDE_CO:
  86. case MAGNITUDE_CO2:
  87. case MAGNITUDE_NO2:
  88. case MAGNITUDE_VOC:
  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. case MAGNITUDE_FREQUENCY:
  103. return sensor::Unit::Hertz;
  104. case MAGNITUDE_PH:
  105. return sensor::Unit::Ph;
  106. default:
  107. return sensor::Unit::None;
  108. }
  109. }
  110. protected:
  111. unsigned char _sensor_id = 0x00;
  112. int _error = 0;
  113. bool _dirty = true;
  114. unsigned char _count = 0;
  115. bool _ready = false;
  116. };