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 <algernon@keyboard.io>
master
Gergely Nagy 2 years ago
parent 6a12467b84
commit d0f1a812b0
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -36,10 +36,18 @@ namespace plugin {
bool HostPowerManagement::was_suspended_ = false; bool HostPowerManagement::was_suspended_ = false;
bool HostPowerManagement::initial_suspend_ = true; 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__ EventHandlerResult HostPowerManagement::beforeEachCycle() {
if ((_usbSuspendState & (1 << SUSPI))) { if (isSuspended()) {
if (!initial_suspend_) { if (!initial_suspend_) {
if (!was_suspended_) { if (!was_suspended_) {
was_suspended_ = true; was_suspended_ = true;
@ -56,23 +64,6 @@ EventHandlerResult HostPowerManagement::beforeEachCycle() {
hostPowerManagementEventHandler(Resume); 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; return EventHandlerResult::OK;
} }

@ -35,6 +35,8 @@ class HostPowerManagement : public kaleidoscope::Plugin {
private: private:
static bool was_suspended_; static bool was_suspended_;
static bool initial_suspend_; static bool initial_suspend_;
bool isSuspended();
}; };
} // namespace plugin } // namespace plugin

Loading…
Cancel
Save