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.

132 lines
3.7 KiB

  1. // -----------------------------------------------------------------------------
  2. // Energy Monitor Sensor using builtin ADC
  3. // Copyright (C) 2017-2018 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. _error = SENSOR_ERROR_OK;
  50. unsigned char i=0;
  51. #if EMON_REPORT_CURRENT
  52. if (index == i++) return MAGNITUDE_CURRENT;
  53. #endif
  54. #if EMON_REPORT_POWER
  55. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  56. #endif
  57. #if EMON_REPORT_ENERGY
  58. if (index == i) return MAGNITUDE_ENERGY;
  59. #endif
  60. _error = SENSOR_ERROR_OUT_OF_RANGE;
  61. return MAGNITUDE_NONE;
  62. }
  63. // Pre-read hook (usually to populate registers with up-to-date data)
  64. void pre() {
  65. _current[0] = read(0);
  66. #if EMON_REPORT_ENERGY
  67. static unsigned long last = 0;
  68. if (last > 0) {
  69. _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
  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. _error = SENSOR_ERROR_OK;
  78. unsigned char channel = index / _magnitudes;
  79. unsigned char i=0;
  80. #if EMON_REPORT_CURRENT
  81. if (index == i++) return _current[channel];
  82. #endif
  83. #if EMON_REPORT_POWER
  84. if (index == i++) return _current[channel] * _voltage;
  85. #endif
  86. #if EMON_REPORT_ENERGY
  87. if (index == i) return _energy[channel];
  88. #endif
  89. _error = SENSOR_ERROR_OUT_OF_RANGE;
  90. return 0;
  91. }
  92. protected:
  93. unsigned int readADC(unsigned char channel) {
  94. (void) channel;
  95. return analogRead(0);
  96. }
  97. };
  98. #endif // SENSOR_SUPPORT && EMON_ANALOG_SUPPORT