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.

187 lines
3.7 KiB

providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
providers: relays, lights and buttons refactoring (#2414) - gpio module now tracks the known providers (right now, hardware and mcp expander) - refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions - refactored button module to use gpio provider instead of referencing types itself - removed dual & stm code from buttons, migrate both to relay module - added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did) - relays runtime configuration keys - relay command now shows configured relays and current & target statuses - refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead - remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT - allow to bind rf codes to real relays - drop tuya-specific lights provider, remove tuya code from relays and lights modules - integrate tuya via relay listeners and providers, use lights custom provider - implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle) - lights custom provider (global, not per-pin) and state listeners - remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT - lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT - refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing - transition time + step parameter for the lightUpdate - report mask parameter for the lightUpdate - minor fixes across the board resolve #2222
3 years ago
  1. /*
  2. TUYA MODULE
  3. Copyright (C) 2019 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. */
  5. #pragma once
  6. #include <cstdint>
  7. #include <vector>
  8. #include "tuya_types.h"
  9. #include "tuya_transport.h"
  10. namespace tuya {
  11. using container = std::vector<uint8_t>;
  12. using const_iterator = container::const_iterator;
  13. namespace util {
  14. template <typename T>
  15. container serialize_frame(const T& frame) {
  16. container result;
  17. result.reserve(6 + frame.length());
  18. result.push_back(frame.version());
  19. result.push_back(frame.command());
  20. result.push_back(static_cast<uint8_t>(frame.length() >> 8) & 0xff);
  21. result.push_back(static_cast<uint8_t>(frame.length() & 0xff));
  22. if (frame.length()) {
  23. result.insert(result.end(), frame.cbegin(), frame.cend());
  24. }
  25. return result;
  26. }
  27. } // namespace util
  28. class DataFrameView {
  29. public:
  30. explicit DataFrameView(const Transport& input) :
  31. _version(input[2]),
  32. _command(input[3]),
  33. _length((input[4] << 8) + input[5]),
  34. _begin(input.cbegin() + 6),
  35. _end(_begin + _length)
  36. {}
  37. explicit DataFrameView(const_iterator it) :
  38. _version(it[0]),
  39. _command(it[1]),
  40. _length((it[2] << 8) + it[3]),
  41. _begin(it + 4),
  42. _end(_begin + _length)
  43. {}
  44. explicit DataFrameView(const container& data) :
  45. DataFrameView(data.cbegin())
  46. {}
  47. container data() const {
  48. return container(_begin, _end);
  49. }
  50. container serialize() const {
  51. return util::serialize_frame(*this);
  52. }
  53. const_iterator cbegin() const {
  54. return _begin;
  55. };
  56. const_iterator cend() const {
  57. return _end;
  58. };
  59. uint8_t operator[](size_t i) const {
  60. return *(_begin + i);
  61. }
  62. uint8_t version() const {
  63. return _version;
  64. }
  65. uint8_t command() const {
  66. return _command;
  67. }
  68. uint16_t length() const {
  69. return _length;
  70. }
  71. private:
  72. uint8_t _version { 0u };
  73. uint8_t _command { 0u };
  74. uint16_t _length { 0u };
  75. const_iterator _begin;
  76. const_iterator _end;
  77. };
  78. class DataFrame {
  79. public:
  80. template <typename T>
  81. DataFrame(Command command, uint8_t version, T&& data) :
  82. _data(std::forward<T>(data)),
  83. _command(static_cast<uint8_t>(command)),
  84. _version(version)
  85. {}
  86. template <typename T>
  87. DataFrame(Command command, T&& data) :
  88. _data(std::forward<T>(data)),
  89. _command(static_cast<uint8_t>(command))
  90. {}
  91. explicit DataFrame(uint8_t command) :
  92. _command(command)
  93. {}
  94. explicit DataFrame(Command command) :
  95. DataFrame(static_cast<uint8_t>(command))
  96. {}
  97. explicit DataFrame(const Transport& input) :
  98. _version(input[2]),
  99. _command(input[3])
  100. {
  101. auto length = (input[4] << 8) + input[5];
  102. _data.reserve(length);
  103. auto data = input.cbegin() + 6;
  104. _data.insert(_data.begin(), data, data + length);
  105. }
  106. explicit DataFrame(const DataFrameView& view) :
  107. _data(view.cbegin(), view.cend()),
  108. _version(view.version()),
  109. _command(view.command())
  110. {}
  111. const container& data() const {
  112. return _data;
  113. }
  114. const_iterator cbegin() const {
  115. return _data.cbegin();
  116. };
  117. const_iterator cend() const {
  118. return _data.cend();
  119. };
  120. uint8_t operator[](size_t i) const {
  121. return _data[i];
  122. }
  123. container serialize() const {
  124. return util::serialize_frame(*this);
  125. }
  126. uint8_t version() const {
  127. return _version;
  128. }
  129. uint8_t command() const {
  130. return _command;
  131. }
  132. uint16_t length() const {
  133. return _data.size();
  134. }
  135. private:
  136. container _data;
  137. uint8_t _version { 0u };
  138. uint8_t _command { 0u };
  139. };
  140. } // namespace