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.

121 lines
3.4 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 "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(): EmonSensor() {
  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. // Type for slot # index
  43. magnitude_t type(unsigned char index) {
  44. _error = SENSOR_ERROR_OK;
  45. unsigned char i=0;
  46. #if EMON_REPORT_CURRENT
  47. if (index == i++) return MAGNITUDE_CURRENT;
  48. #endif
  49. #if EMON_REPORT_POWER
  50. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  51. #endif
  52. #if EMON_REPORT_ENERGY
  53. if (index == i) return MAGNITUDE_ENERGY;
  54. #endif
  55. _error = SENSOR_ERROR_OUT_OF_RANGE;
  56. return MAGNITUDE_NONE;
  57. }
  58. // Pre-read hook (usually to populate registers with up-to-date data)
  59. void pre() {
  60. _current[0] = read(0);
  61. #if EMON_REPORT_ENERGY
  62. static unsigned long last = 0;
  63. if (last > 0) {
  64. _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
  65. }
  66. last = millis();
  67. #endif
  68. }
  69. // Current value for slot # index
  70. double value(unsigned char index) {
  71. _error = SENSOR_ERROR_OK;
  72. unsigned char channel = index / _magnitudes;
  73. unsigned char i=0;
  74. #if EMON_REPORT_CURRENT
  75. if (index == i++) return _current[channel];
  76. #endif
  77. #if EMON_REPORT_POWER
  78. if (index == i++) return _current[channel] * _voltage;
  79. #endif
  80. #if EMON_REPORT_ENERGY
  81. if (index == i) return _energy[channel];
  82. #endif
  83. _error = SENSOR_ERROR_OUT_OF_RANGE;
  84. return 0;
  85. }
  86. protected:
  87. unsigned int readADC(unsigned char channel) {
  88. (void) channel;
  89. return analogRead(0);
  90. }
  91. };