From d0f1a812b0f8f6922b5a9101a9a79b44156db9cc Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 2 Oct 2022 11:59:23 +0200 Subject: [PATCH] HostPowerManagement: Lift out the suspension check, to remove code duplication The difference between the AVR and the GD32 implementation was the way we checked if the device is suspended, otherwise the logic was exactly the same, duplicated. To remove the need for duplicating the same code for every architecture we support, lift out the suspension check instead. This way the core logic becomes shared between all of them. Signed-off-by: Gergely Nagy --- .../plugin/HostPowerManagement.cpp | 31 +++++++------------ .../kaleidoscope/plugin/HostPowerManagement.h | 2 ++ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.cpp b/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.cpp index 8111955f..422f1d28 100644 --- a/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.cpp +++ b/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.cpp @@ -36,10 +36,18 @@ namespace plugin { bool HostPowerManagement::was_suspended_ = false; bool HostPowerManagement::initial_suspend_ = true; -EventHandlerResult HostPowerManagement::beforeEachCycle() { +bool HostPowerManagement::isSuspended() { +#if defined(__AVR__) + return USBDevice.isSuspended(); +#elif defined(ARDUINO_ARCH_GD32) + return USBCore().isSuspended(); +#else + return false; +#endif +} -#ifdef __AVR__ - if ((_usbSuspendState & (1 << SUSPI))) { +EventHandlerResult HostPowerManagement::beforeEachCycle() { + if (isSuspended()) { if (!initial_suspend_) { if (!was_suspended_) { was_suspended_ = true; @@ -56,23 +64,6 @@ EventHandlerResult HostPowerManagement::beforeEachCycle() { hostPowerManagementEventHandler(Resume); } } -#endif - -#ifdef ARDUINO_ARCH_GD32 - if (USBCore().isSuspended()) { - if (!was_suspended_) { - was_suspended_ = true; - hostPowerManagementEventHandler(Suspend); - } else { - hostPowerManagementEventHandler(Sleep); - } - } else { - if (was_suspended_) { - was_suspended_ = false; - hostPowerManagementEventHandler(Resume); - } - } -#endif return EventHandlerResult::OK; } diff --git a/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.h b/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.h index 27acb862..d8427680 100644 --- a/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.h +++ b/plugins/Kaleidoscope-HostPowerManagement/src/kaleidoscope/plugin/HostPowerManagement.h @@ -35,6 +35,8 @@ class HostPowerManagement : public kaleidoscope::Plugin { private: static bool was_suspended_; static bool initial_suspend_; + + bool isSuspended(); }; } // namespace plugin