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.

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