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.

154 lines
5.1 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. case MAGNITUDE_POWER_FACTOR:
  68. return sensor::Unit::Percentage;
  69. case MAGNITUDE_PRESSURE:
  70. return sensor::Unit::Hectopascal;
  71. case MAGNITUDE_CURRENT:
  72. return sensor::Unit::Ampere;
  73. case MAGNITUDE_VOLTAGE:
  74. return sensor::Unit::Volt;
  75. case MAGNITUDE_POWER_ACTIVE:
  76. return sensor::Unit::Watt;
  77. case MAGNITUDE_POWER_APPARENT:
  78. return sensor::Unit::Voltampere;
  79. case MAGNITUDE_POWER_REACTIVE:
  80. return sensor::Unit::VoltampereReactive;
  81. case MAGNITUDE_ENERGY_DELTA:
  82. return sensor::Unit::Joule;
  83. case MAGNITUDE_ENERGY:
  84. return sensor::Unit::KilowattHour;
  85. case MAGNITUDE_PM1dot0:
  86. case MAGNITUDE_PM2dot5:
  87. return sensor::Unit::MicrogrammPerCubicMeter;
  88. case MAGNITUDE_CO2:
  89. case MAGNITUDE_NO2:
  90. case MAGNITUDE_CO:
  91. return sensor::Unit::PartsPerMillion;
  92. case MAGNITUDE_LUX:
  93. return sensor::Unit::Lux;
  94. case MAGNITUDE_RESISTANCE:
  95. return sensor::Unit::Ohm;
  96. case MAGNITUDE_HCHO:
  97. return sensor::Unit::MilligrammPerCubicMeter;
  98. case MAGNITUDE_GEIGER_CPM:
  99. return sensor::Unit::CountsPerMinute;
  100. case MAGNITUDE_GEIGER_SIEVERT:
  101. return sensor::Unit::MicrosievertPerHour;
  102. case MAGNITUDE_DISTANCE:
  103. return sensor::Unit::Meter;
  104. case MAGNITUDE_FREQUENCY:
  105. return sensor::Unit::Hertz;
  106. case MAGNITUDE_PH:
  107. return sensor::Unit::Ph;
  108. default:
  109. return sensor::Unit::None;
  110. }
  111. }
  112. protected:
  113. TSensorCallback _callback = NULL;
  114. unsigned char _sensor_id = 0x00;
  115. int _error = 0;
  116. bool _dirty = true;
  117. unsigned char _count = 0;
  118. bool _ready = false;
  119. };