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.

128 lines
3.6 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. // Set ADC to TOUT pin
  8. #undef ADC_MODE_VALUE
  9. #define ADC_MODE_VALUE ADC_TOUT
  10. #include "Arduino.h"
  11. #include "EmonSensor.h"
  12. #define EMON_ANALOG_RESOLUTION 10
  13. #define EMON_ANALOG_CHANNELS 1
  14. class EmonAnalogSensor : public EmonSensor {
  15. public:
  16. // ---------------------------------------------------------------------
  17. // Public
  18. // ---------------------------------------------------------------------
  19. EmonAnalogSensor(): EmonSensor() {
  20. _channels = EMON_ANALOG_CHANNELS;
  21. _sensor_id = SENSOR_EMON_ANALOG_ID;
  22. init();
  23. }
  24. // ---------------------------------------------------------------------
  25. // Sensor API
  26. // ---------------------------------------------------------------------
  27. // Initialization method, must be idempotent
  28. void begin() {
  29. if (!_dirty) return;
  30. _dirty = false;
  31. // Number of slots
  32. _count = _magnitudes;
  33. // Bit depth
  34. _resolution = EMON_ANALOG_RESOLUTION;
  35. // Init analog PIN)
  36. pinMode(0, INPUT);
  37. // Call the parent class method
  38. EmonSensor::begin();
  39. // warmup channel 0 (the only one)
  40. read(0);
  41. }
  42. // Descriptive name of the sensor
  43. String description() {
  44. return String("EMON @ ANALOG @ GPIO0");
  45. }
  46. // Address of the sensor (it could be the GPIO or I2C address)
  47. String address(unsigned char index) {
  48. return String("0");
  49. }
  50. // Type for slot # index
  51. unsigned char type(unsigned char index) {
  52. unsigned char i=0;
  53. #if EMON_REPORT_CURRENT
  54. if (index == i++) return MAGNITUDE_CURRENT;
  55. #endif
  56. #if EMON_REPORT_POWER
  57. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  58. #endif
  59. #if EMON_REPORT_ENERGY
  60. if (index == i) return MAGNITUDE_ENERGY;
  61. #endif
  62. return MAGNITUDE_NONE;
  63. }
  64. // Pre-read hook (usually to populate registers with up-to-date data)
  65. void pre() {
  66. _current[0] = read(0);
  67. #if EMON_REPORT_ENERGY
  68. static unsigned long last = 0;
  69. if (last > 0) {
  70. _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
  71. }
  72. last = millis();
  73. #endif
  74. _error = SENSOR_ERROR_OK;
  75. }
  76. // Current value for slot # index
  77. double value(unsigned char index) {
  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. return 0;
  90. }
  91. protected:
  92. unsigned int readADC(unsigned char channel) {
  93. (void) channel;
  94. return analogRead(0);
  95. }
  96. };
  97. #endif // SENSOR_SUPPORT && EMON_ANALOG_SUPPORT