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.5 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(): EmonSensor() {
  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. if (last > 0) {
  67. _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
  68. }
  69. last = millis();
  70. #endif
  71. _error = SENSOR_ERROR_OK;
  72. }
  73. // Current value for slot # index
  74. double value(unsigned char index) {
  75. unsigned char channel = index / _magnitudes;
  76. unsigned char i=0;
  77. #if EMON_REPORT_CURRENT
  78. if (index == i++) return _current[channel];
  79. #endif
  80. #if EMON_REPORT_POWER
  81. if (index == i++) return _current[channel] * _voltage;
  82. #endif
  83. #if EMON_REPORT_ENERGY
  84. if (index == i) return _energy[channel];
  85. #endif
  86. return 0;
  87. }
  88. protected:
  89. unsigned int readADC(unsigned char channel) {
  90. UNUSED(channel);
  91. return analogRead(0);
  92. }
  93. };
  94. #endif // SENSOR_SUPPORT && EMON_ANALOG_SUPPORT