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.

315 lines
6.3 KiB

  1. /*
  2. SENSOR MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  5. */
  6. #pragma once
  7. struct sensor_magnitude_t;
  8. //--------------------------------------------------------------------------------
  9. namespace sensor {
  10. namespace type {
  11. enum Type : unsigned char {
  12. Base = 0,
  13. Emon = 1 << 0,
  14. Analog = 1 << 1
  15. };
  16. } // namespace type
  17. enum class Unit : int {
  18. Min_,
  19. None,
  20. Celcius,
  21. Farenheit,
  22. Kelvin,
  23. Percentage,
  24. Hectopascal,
  25. Ampere,
  26. Volt,
  27. Voltampere,
  28. Kilovoltampere,
  29. VoltampereReactive,
  30. KilovoltampereReactive,
  31. Watt,
  32. Kilowatt,
  33. WattSecond,
  34. Joule = WattSecond,
  35. KilowattHour,
  36. PartsPerMillion,
  37. Ohm,
  38. MicrogrammPerCubicMeter, // The concentration of an air pollutant
  39. MilligrammPerCubicMeter, //
  40. Lux,
  41. UltravioletIndex, // "measurement of the strength of sunburn-producing ultraviolet (UV) radiation at a particular place and time"
  42. // (XXX: Not a unit. Distinguish from None and specify decimals)
  43. CountsPerMinute, // Unit of local dose rate (Geiger counting)
  44. MicrosievertPerHour, // 2nd unit of local dose rate (Geiger counting)
  45. Meter,
  46. Max_
  47. };
  48. // Base units are 32 bit since we are the fastest with them.
  49. struct Ws {
  50. Ws();
  51. Ws(uint32_t);
  52. uint32_t value;
  53. };
  54. struct Wh {
  55. Wh();
  56. Wh(Ws);
  57. Wh(uint32_t);
  58. uint32_t value;
  59. };
  60. struct KWh {
  61. KWh();
  62. KWh(Ws);
  63. KWh(Wh);
  64. KWh(uint32_t);
  65. uint32_t value;
  66. };
  67. struct Energy {
  68. constexpr static uint32_t KwhMultiplier = 3600000ul;
  69. constexpr static uint32_t KwhLimit = ((1ul << 31ul) / KwhMultiplier);
  70. Energy() = default;
  71. // TODO: while we accept ws >= the kwh conversion limit,
  72. // should this be dealt with on the unit level?
  73. Energy(double);
  74. Energy(KWh, Ws);
  75. Energy(KWh);
  76. Energy(Wh);
  77. Energy(Ws);
  78. // Sets internal counters to zero
  79. void reset();
  80. // Check whether we have *any* energy recorded. Can be zero:
  81. // - on cold boot
  82. // - on overflow
  83. // - when we call `reset()`
  84. operator bool();
  85. // Generic conversion as-is
  86. double asDouble();
  87. // Convert back to input unit, with overflow mechanics when kwh values goes over 32 bit
  88. Ws asWs();
  89. // Generic sensors output energy in joules / watt-second
  90. Energy& operator +=(Ws);
  91. Energy operator +(Ws);
  92. // But sometimes we want to accept asDouble() value back
  93. Energy& operator =(double);
  94. // We are storing a kind-of integral and fractional parts
  95. // Using watt-second to avoid loosing precision, we don't expect these to be accessed directly
  96. KWh kwh;
  97. Ws ws;
  98. };
  99. }
  100. String magnitudeName(unsigned char index);
  101. String magnitudeUnits(unsigned char index);
  102. unsigned char magnitudeType(unsigned char index);
  103. // XXX: without param name it is kind of vague what exactly unsigned char is
  104. // consider using index instead of type or adding stronger param type
  105. String magnitudeTopic(unsigned char type);
  106. String magnitudeTopic(const sensor_magnitude_t& magnitude);
  107. String magnitudeUnits(const sensor_magnitude_t& magnitude);
  108. unsigned char sensorCount();
  109. unsigned char magnitudeCount();
  110. double magnitudeValue(unsigned char index);
  111. unsigned char magnitudeIndex(unsigned char index);
  112. String magnitudeTopicIndex(unsigned char index);
  113. void sensorSetup();
  114. void sensorLoop();
  115. //--------------------------------------------------------------------------------
  116. #include "filters/LastFilter.h"
  117. #include "filters/MaxFilter.h"
  118. #include "filters/MedianFilter.h"
  119. #include "filters/MovingAverageFilter.h"
  120. #include "filters/SumFilter.h"
  121. #include "sensors/BaseSensor.h"
  122. #include "sensors/BaseEmonSensor.h"
  123. #include "sensors/BaseAnalogSensor.h"
  124. #if AM2320_SUPPORT
  125. #include "sensors/AM2320Sensor.h"
  126. #endif
  127. #if ANALOG_SUPPORT
  128. #include "sensors/AnalogSensor.h"
  129. #endif
  130. #if BH1750_SUPPORT
  131. #include "sensors/BH1750Sensor.h"
  132. #endif
  133. #if BMP180_SUPPORT
  134. #include "sensors/BMP180Sensor.h"
  135. #endif
  136. #if BMX280_SUPPORT
  137. #include "sensors/BMX280Sensor.h"
  138. #endif
  139. #if CSE7766_SUPPORT
  140. #include "sensors/CSE7766Sensor.h"
  141. #endif
  142. #if DALLAS_SUPPORT
  143. #include "sensors/DallasSensor.h"
  144. #endif
  145. #if DHT_SUPPORT
  146. #include "sensors/DHTSensor.h"
  147. #endif
  148. #if DIGITAL_SUPPORT
  149. #include "sensors/DigitalSensor.h"
  150. #endif
  151. #if ECH1560_SUPPORT
  152. #include "sensors/ECH1560Sensor.h"
  153. #endif
  154. #if EMON_ADC121_SUPPORT
  155. #include "sensors/EmonADC121Sensor.h"
  156. #endif
  157. #if EMON_ADS1X15_SUPPORT
  158. #include "sensors/EmonADS1X15Sensor.h"
  159. #endif
  160. #if EMON_ANALOG_SUPPORT
  161. #include "sensors/EmonAnalogSensor.h"
  162. #endif
  163. #if EVENTS_SUPPORT
  164. #include "sensors/EventSensor.h"
  165. #endif
  166. #if EZOPH_SUPPORT
  167. #include "sensors/EZOPHSensor.h"
  168. #endif
  169. #if GEIGER_SUPPORT
  170. #include "sensors/GeigerSensor.h"
  171. #endif
  172. #if GUVAS12SD_SUPPORT
  173. #include "sensors/GUVAS12SDSensor.h"
  174. #endif
  175. #if HLW8012_SUPPORT
  176. #include "sensors/HLW8012Sensor.h"
  177. #endif
  178. #if LDR_SUPPORT
  179. #include "sensors/LDRSensor.h"
  180. #endif
  181. #if MAX6675_SUPPORT
  182. #include "sensors/MAX6675Sensor.h"
  183. #endif
  184. #if MICS2710_SUPPORT
  185. #include "sensors/MICS2710Sensor.h"
  186. #endif
  187. #if MICS5525_SUPPORT
  188. #include "sensors/MICS5525Sensor.h"
  189. #endif
  190. #if MHZ19_SUPPORT
  191. #include "sensors/MHZ19Sensor.h"
  192. #endif
  193. #if NTC_SUPPORT
  194. #include "sensors/NTCSensor.h"
  195. #endif
  196. #if SDS011_SUPPORT
  197. #include "sensors/SDS011Sensor.h"
  198. #endif
  199. #if SENSEAIR_SUPPORT
  200. #include "sensors/SenseAirSensor.h"
  201. #endif
  202. #if PMSX003_SUPPORT
  203. #include "sensors/PMSX003Sensor.h"
  204. #endif
  205. #if PULSEMETER_SUPPORT
  206. #include "sensors/PulseMeterSensor.h"
  207. #endif
  208. #if PZEM004T_SUPPORT
  209. #include "sensors/PZEM004TSensor.h"
  210. #endif
  211. #if SHT3X_I2C_SUPPORT
  212. #include "sensors/SHT3XI2CSensor.h"
  213. #endif
  214. #if SI7021_SUPPORT
  215. #include "sensors/SI7021Sensor.h"
  216. #endif
  217. #if SONAR_SUPPORT
  218. #include "sensors/SonarSensor.h"
  219. #endif
  220. #if T6613_SUPPORT
  221. #include "sensors/T6613Sensor.h"
  222. #endif
  223. #if TMP3X_SUPPORT
  224. #include "sensors/TMP3XSensor.h"
  225. #endif
  226. #if V9261F_SUPPORT
  227. #include "sensors/V9261FSensor.h"
  228. #endif
  229. #if VEML6075_SUPPORT
  230. #include "sensors/VEML6075Sensor.h"
  231. #endif
  232. #if VL53L1X_SUPPORT
  233. #include "sensors/VL53L1XSensor.h"
  234. #endif
  235. #if ADE7953_SUPPORT
  236. #include "sensors/ADE7953Sensor.h"
  237. #endif
  238. //--------------------------------------------------------------------------------