Merge pull request #144 from UltimateHackingKeyboard/sleep-wake

Make some improvements to the sleep/wake code
This commit is contained in:
László Monda
2018-07-26 22:47:40 +02:00
committed by GitHub
3 changed files with 28 additions and 21 deletions

View File

@@ -163,23 +163,30 @@ static usb_device_class_config_list_struct_t UsbDeviceCompositeConfigList = {
} }
}}; }};
bool IsHostSleeping = false; volatile bool SleepModeActive = true;
static volatile bool wakeUpHostAllowed;
static void suspendHost(void) { static void suspendUhk(void) {
IsHostSleeping = true; SleepModeActive = true;
LedSlaveDriver_DisableLeds(); LedSlaveDriver_DisableLeds();
} }
void WakeUpHost(bool sendResume) { static void wakeUpUhk(void) {
if (sendResume) { // The device should wake up the host. SleepModeActive = false;
// Send resume signal - this will call USB_DeviceKhciControl(khciHandle, kUSB_DeviceControlResume, NULL);
USB_DeviceSetStatus(UsbCompositeDevice.deviceHandle, kUSB_DeviceStatusBus, NULL);
}
IsHostSleeping = false;
LedSlaveDriver_UpdateLeds(); LedSlaveDriver_UpdateLeds();
} }
void WakeUpHost(void) {
if (!wakeUpHostAllowed) {
return;
}
// Send resume signal - this will call USB_DeviceKhciControl(khciHandle, kUSB_DeviceControlResume, NULL);
USB_DeviceSetStatus(UsbCompositeDevice.deviceHandle, kUSB_DeviceStatusBus, NULL);
while (SleepModeActive) {
;
}
}
static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, void *param) static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, void *param)
{ {
usb_status_t status = kStatus_USB_Error; usb_status_t status = kStatus_USB_Error;
@@ -190,10 +197,6 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event,
return status; return status;
} }
if (IsHostSleeping) {
WakeUpHost(false); // Wake up the keyboard if there is any activity on the bus.
}
switch (event) { switch (event) {
case kUSB_DeviceEventBusReset: case kUSB_DeviceEventBusReset:
UsbCompositeDevice.attach = 0; UsbCompositeDevice.attach = 0;
@@ -201,17 +204,17 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event,
break; break;
case kUSB_DeviceEventSuspend: case kUSB_DeviceEventSuspend:
if (UsbCompositeDevice.attach) { if (UsbCompositeDevice.attach) {
suspendHost(); // The host sends this event when it goes to sleep, so turn off all the LEDs. suspendUhk(); // The host sends this event when it goes to sleep, so turn off all the LEDs.
status = kStatus_USB_Success; status = kStatus_USB_Success;
} }
break; break;
case kUSB_DeviceEventResume: case kUSB_DeviceEventResume:
// We will just wake up the host if there is any activity on the bus. wakeUpUhk();
// The problem is that the host won't send a resume event when it boots, so the lights will never come back on.
status = kStatus_USB_Success; status = kStatus_USB_Success;
break; break;
case kUSB_DeviceEventSetConfiguration: case kUSB_DeviceEventSetConfiguration:
UsbCompositeDevice.attach = 1; UsbCompositeDevice.attach = 1;
wakeUpUhk();
UsbCompositeDevice.currentConfiguration = *temp8; UsbCompositeDevice.currentConfiguration = *temp8;
UsbGenericHidSetConfiguration(UsbCompositeDevice.genericHidHandle, *temp8); UsbGenericHidSetConfiguration(UsbCompositeDevice.genericHidHandle, *temp8);
UsbBasicKeyboardSetConfiguration(UsbCompositeDevice.basicKeyboardHandle, *temp8); UsbBasicKeyboardSetConfiguration(UsbCompositeDevice.basicKeyboardHandle, *temp8);
@@ -266,6 +269,10 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event,
case kUSB_DeviceEventGetHidPhysicalDescriptor: case kUSB_DeviceEventGetHidPhysicalDescriptor:
status = USB_DeviceGetHidPhysicalDescriptor(handle, (usb_device_get_hid_physical_descriptor_struct_t *)param); status = USB_DeviceGetHidPhysicalDescriptor(handle, (usb_device_get_hid_physical_descriptor_struct_t *)param);
break; break;
case kUSB_DeviceEventSetRemoteWakeup:
wakeUpHostAllowed = *temp8;
status = kStatus_USB_Success;
break;
} }
return status; return status;

View File

@@ -28,12 +28,12 @@
// Variables: // Variables:
extern bool IsHostSleeping; extern volatile bool SleepModeActive;
extern usb_composite_device_t UsbCompositeDevice; extern usb_composite_device_t UsbCompositeDevice;
//Functions: //Functions:
void InitUsb(void); void InitUsb(void);
void WakeUpHost(bool sendResume); void WakeUpHost(void);
#endif #endif

View File

@@ -425,11 +425,11 @@ void UpdateUsbReports(void)
KeyStates[SlotId_RightKeyboardHalf][keyId].current = RightKeyMatrix.keyStates[keyId]; KeyStates[SlotId_RightKeyboardHalf][keyId].current = RightKeyMatrix.keyStates[keyId];
} }
if (IsHostSleeping) { if (SleepModeActive) {
for (uint8_t slotId = 0; slotId < SLOT_COUNT; slotId++) { for (uint8_t slotId = 0; slotId < SLOT_COUNT; slotId++) {
for (uint8_t keyId = 0; keyId < MAX_KEY_COUNT_PER_MODULE; keyId++) { for (uint8_t keyId = 0; keyId < MAX_KEY_COUNT_PER_MODULE; keyId++) {
if (KeyStates[slotId][keyId].current) { if (KeyStates[slotId][keyId].current) {
WakeUpHost(true); WakeUpHost();
return; return;
} }
} }