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.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // Energy Monitor Sensor using builtin ADC
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include "Arduino.h"
  7. #include "BaseSensor.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(): EmonSensor() {
  17. _channels = EMON_ANALOG_CHANNELS;
  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 name() {
  40. return String("EMON @ ANALOG @ GPIO0");
  41. }
  42. // Descriptive name of the slot # index
  43. String slot(unsigned char index) {
  44. return name();
  45. }
  46. // Type for slot # index
  47. magnitude_t type(unsigned char index) {
  48. _error = SENSOR_ERROR_OK;
  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. _error = SENSOR_ERROR_OUT_OF_RANGE;
  60. return MAGNITUDE_NONE;
  61. }
  62. // Pre-read hook (usually to populate registers with up-to-date data)
  63. void pre() {
  64. _current[0] = read(0);
  65. #if EMON_REPORT_ENERGY
  66. static unsigned long last = 0;
  67. if (last > 0) {
  68. _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
  69. }
  70. last = millis();
  71. #endif
  72. }
  73. // Current value for slot # index
  74. double value(unsigned char index) {
  75. _error = SENSOR_ERROR_OK;
  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];
  86. #endif
  87. _error = SENSOR_ERROR_OUT_OF_RANGE;
  88. return 0;
  89. }
  90. protected:
  91. unsigned int readADC(unsigned char channel) {
  92. (void) channel;
  93. return analogRead(0);
  94. }
  95. };