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.

188 lines
4.4 KiB

  1. /*
  2. POWER V9261F MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if POWER_PROVIDER == POWER_PROVIDER_V9261F
  6. // -----------------------------------------------------------------------------
  7. // MODULE GLOBALS AND CACHE
  8. // -----------------------------------------------------------------------------
  9. #include <SoftwareSerial.h>
  10. SoftwareSerial * _v9261f_uart;
  11. double _v9261f_active = 0;
  12. double _v9261f_reactive = 0;
  13. double _v9261f_voltage = 0;
  14. double _v9261f_current = 0;
  15. unsigned char _v9261f_data[24];
  16. // -----------------------------------------------------------------------------
  17. // HAL
  18. // -----------------------------------------------------------------------------
  19. void _v9261fRead() {
  20. static unsigned char state = 0;
  21. static unsigned long last = 0;
  22. static bool found = false;
  23. static unsigned char index = 0;
  24. if (state == 0) {
  25. while (_v9261f_uart->available()) {
  26. _v9261f_uart->flush();
  27. found = true;
  28. last = millis();
  29. }
  30. if (found && (millis() - last > V9261F_SYNC_INTERVAL)) {
  31. _v9261f_uart->flush();
  32. index = 0;
  33. state = 1;
  34. }
  35. } else if (state == 1) {
  36. while (_v9261f_uart->available()) {
  37. _v9261f_uart->read();
  38. if (index++ >= 7) {
  39. _v9261f_uart->flush();
  40. index = 0;
  41. state = 2;
  42. }
  43. }
  44. } else if (state == 2) {
  45. while (_v9261f_uart->available()) {
  46. _v9261f_data[index] = _v9261f_uart->read();
  47. if (index++ >= 19) {
  48. _v9261f_uart->flush();
  49. last = millis();
  50. state = 3;
  51. }
  52. }
  53. } else if (state == 3) {
  54. if (checksumOK()) {
  55. _v9261f_active = (double) (
  56. (_v9261f_data[3]) +
  57. (_v9261f_data[4] << 8) +
  58. (_v9261f_data[5] << 16) +
  59. (_v9261f_data[6] << 24)
  60. ) / V9261F_POWER_FACTOR;
  61. _v9261f_reactive = (double) (
  62. (_v9261f_data[7]) +
  63. (_v9261f_data[8] << 8) +
  64. (_v9261f_data[9] << 16) +
  65. (_v9261f_data[10] << 24)
  66. ) / V9261F_RPOWER_FACTOR;
  67. _v9261f_voltage = (double) (
  68. (_v9261f_data[11]) +
  69. (_v9261f_data[12] << 8) +
  70. (_v9261f_data[13] << 16) +
  71. (_v9261f_data[14] << 24)
  72. ) / V9261F_VOLTAGE_FACTOR;
  73. _v9261f_current = (double) (
  74. (_v9261f_data[15]) +
  75. (_v9261f_data[16] << 8) +
  76. (_v9261f_data[17] << 16) +
  77. (_v9261f_data[18] << 24)
  78. ) / V9261F_CURRENT_FACTOR;
  79. _power_newdata = true;
  80. }
  81. last = millis();
  82. index = 0;
  83. state = 4;
  84. } else if (state == 4) {
  85. while (_v9261f_uart->available()) {
  86. _v9261f_uart->flush();
  87. last = millis();
  88. }
  89. if (millis() - last > V9261F_SYNC_INTERVAL) {
  90. state = 1;
  91. }
  92. }
  93. }
  94. boolean checksumOK() {
  95. unsigned char checksum = 0;
  96. for (unsigned char i = 0; i < 19; i++) {
  97. checksum = checksum + _v9261f_data[i];
  98. }
  99. checksum = ~checksum + 0x33;
  100. return checksum == _v9261f_data[19];
  101. }
  102. // -----------------------------------------------------------------------------
  103. double _powerCurrent() {
  104. return _v9261f_current;
  105. }
  106. double _powerVoltage() {
  107. return _v9261f_voltage;
  108. }
  109. double _powerActivePower() {
  110. return _v9261f_active;
  111. }
  112. double _powerApparentPower() {
  113. return sqrt(_v9261f_reactive * _v9261f_reactive + _v9261f_active * _v9261f_active);
  114. }
  115. double _powerReactivePower() {
  116. return _v9261f_reactive;
  117. }
  118. double _powerPowerFactor() {
  119. double apparent = _powerApparentPower();
  120. if (apparent > 0) return _powerActivePower() / apparent;
  121. return 1;
  122. }
  123. // -----------------------------------------------------------------------------
  124. // PUBLIC API
  125. // -----------------------------------------------------------------------------
  126. void powerEnabledProvider() {
  127. // Nothing to do
  128. }
  129. void powerConfigureProvider() {
  130. // Nothing to do
  131. }
  132. void powerSetupProvider() {
  133. _v9261f_uart = new SoftwareSerial(V9261F_PIN, SW_SERIAL_UNUSED_PIN, V9261F_PIN_INVERSE, 256);
  134. _v9261f_uart->begin(V9261F_BAUDRATE);
  135. }
  136. void powerLoopProvider(bool before) {
  137. if (before) {
  138. _v9261fRead();
  139. }
  140. }
  141. #endif // POWER_PROVIDER & POWER_PROVIDER_EMON