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.

125 lines
3.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // Energy Monitor Sensor using builtin ADC
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && EMON_ANALOG_SUPPORT
  6. #pragma once
  7. #include "EmonSensor.h"
  8. #define EMON_ANALOG_RESOLUTION 10
  9. #define EMON_ANALOG_CHANNELS 1
  10. class EmonAnalogSensor : public EmonSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. EmonAnalogSensor() {
  16. _channels = EMON_ANALOG_CHANNELS;
  17. _sensor_id = SENSOR_EMON_ANALOG_ID;
  18. init();
  19. }
  20. // ---------------------------------------------------------------------
  21. // Sensor API
  22. // ---------------------------------------------------------------------
  23. // Initialization method, must be idempotent
  24. void begin() {
  25. if (!_dirty) return;
  26. _dirty = false;
  27. // Number of slots
  28. _count = _magnitudes;
  29. // Bit depth
  30. _resolution = EMON_ANALOG_RESOLUTION;
  31. // Init analog PIN)
  32. pinMode(0, INPUT);
  33. // Call the parent class method
  34. EmonSensor::begin();
  35. // warmup channel 0 (the only one)
  36. read(0);
  37. }
  38. // Descriptive name of the sensor
  39. String description() {
  40. return String("EMON @ ANALOG @ GPIO0");
  41. }
  42. // Address of the sensor (it could be the GPIO or I2C address)
  43. String address(unsigned char index) {
  44. return String("0");
  45. }
  46. // Type for slot # index
  47. unsigned char type(unsigned char index) {
  48. unsigned char i=0;
  49. #if EMON_REPORT_CURRENT
  50. if (index == i++) return MAGNITUDE_CURRENT;
  51. #endif
  52. #if EMON_REPORT_POWER
  53. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  54. #endif
  55. #if EMON_REPORT_ENERGY
  56. if (index == i) return MAGNITUDE_ENERGY;
  57. #endif
  58. return MAGNITUDE_NONE;
  59. }
  60. // Pre-read hook (usually to populate registers with up-to-date data)
  61. void pre() {
  62. _current[0] = read(0);
  63. #if EMON_REPORT_ENERGY
  64. static unsigned long last = 0;
  65. for (unsigned char channel = 0; channel < _channels; ++channel) {
  66. _energy[channel] += sensor::Ws {
  67. static_cast<uint32_t>(_current[channel] * _voltage * (millis() - last) / 1000)
  68. };
  69. }
  70. last = millis();
  71. #endif
  72. _error = SENSOR_ERROR_OK;
  73. }
  74. // Current value for slot # index
  75. double value(unsigned char index) {
  76. unsigned char channel = index / _magnitudes;
  77. unsigned char i=0;
  78. #if EMON_REPORT_CURRENT
  79. if (index == i++) return _current[channel];
  80. #endif
  81. #if EMON_REPORT_POWER
  82. if (index == i++) return _current[channel] * _voltage;
  83. #endif
  84. #if EMON_REPORT_ENERGY
  85. if (index == i) return _energy[channel].asDouble();
  86. #endif
  87. return 0;
  88. }
  89. protected:
  90. unsigned int readADC(unsigned char) {
  91. return analogRead(0);
  92. }
  93. };
  94. #endif // SENSOR_SUPPORT && EMON_ANALOG_SUPPORT