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.

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