X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/e6881fd166586793a5a90effeefe4188092f383b..2a0c28e6e47c8a173f32fc99cd8666a2633c5c12:/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c diff --git a/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c b/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c index ddfe05a4b..7d486e16e 100644 --- a/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c +++ b/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c @@ -174,7 +174,11 @@ void EVENT_USB_UnhandledControlPacket(void) Endpoint_ClearSETUP(); /* Wait until the LED report has been sent by the host */ - while (!(Endpoint_IsOUTReceived())); + while (!(Endpoint_IsOUTReceived())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } /* Read in the LED report from the host */ uint8_t LEDStatus = Endpoint_Read_Byte(); @@ -195,9 +199,7 @@ void EVENT_USB_UnhandledControlPacket(void) /* Clear the endpoint data */ Endpoint_ClearOUT(); - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -212,6 +214,10 @@ void Keyboard_HID_Task(void) { uint8_t JoyStatus_LCL = Joystick_GetStatus(); + /* Device must be connected and configured for the task to run */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + /* Check if board button is not pressed, if so mouse mode enabled */ if (!(Buttons_GetStatus() & BUTTONS_BUTTON1)) { @@ -228,51 +234,47 @@ void Keyboard_HID_Task(void) if (JoyStatus_LCL & JOY_PRESS) KeyboardReportData.KeyCode[0] = 0x08; // E } - - /* Check if the USB system is connected to a host and report protocol mode is enabled */ - if (USB_IsConnected) + + /* Select the Keyboard Report Endpoint */ + Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM); + + /* Check if Keyboard Endpoint Ready for Read/Write */ + if (Endpoint_IsReadWriteAllowed()) { - /* Select the Keyboard Report Endpoint */ - Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM); - - /* Check if Keyboard Endpoint Ready for Read/Write */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Write Keyboard Report Data */ - Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData)); - - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); - - /* Clear the report data afterwards */ - memset(&KeyboardReportData, 0, sizeof(KeyboardReportData)); - } - - /* Select the Keyboard LED Report Endpoint */ - Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM); - - /* Check if Keyboard LED Endpoint Ready for Read/Write */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Read in the LED report from the host */ - uint8_t LEDStatus = Endpoint_Read_Byte(); - uint8_t LEDMask = LEDS_LED2; - - if (LEDStatus & 0x01) // NUM Lock - LEDMask |= LEDS_LED1; - - if (LEDStatus & 0x02) // CAPS Lock - LEDMask |= LEDS_LED3; + /* Write Keyboard Report Data */ + Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData)); + + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); - if (LEDStatus & 0x04) // SCROLL Lock - LEDMask |= LEDS_LED4; + /* Clear the report data afterwards */ + memset(&KeyboardReportData, 0, sizeof(KeyboardReportData)); + } + + /* Select the Keyboard LED Report Endpoint */ + Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM); + + /* Check if Keyboard LED Endpoint Ready for Read/Write */ + if (Endpoint_IsReadWriteAllowed()) + { + /* Read in the LED report from the host */ + uint8_t LEDStatus = Endpoint_Read_Byte(); + uint8_t LEDMask = LEDS_LED2; + + if (LEDStatus & 0x01) // NUM Lock + LEDMask |= LEDS_LED1; + + if (LEDStatus & 0x02) // CAPS Lock + LEDMask |= LEDS_LED3; - /* Set the status LEDs to the current Keyboard LED status */ - LEDs_SetAllLEDs(LEDMask); + if (LEDStatus & 0x04) // SCROLL Lock + LEDMask |= LEDS_LED4; - /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ - Endpoint_ClearOUT(); - } + /* Set the status LEDs to the current Keyboard LED status */ + LEDs_SetAllLEDs(LEDMask); + + /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ + Endpoint_ClearOUT(); } } @@ -283,6 +285,10 @@ void Mouse_HID_Task(void) { uint8_t JoyStatus_LCL = Joystick_GetStatus(); + /* Device must be connected and configured for the task to run */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + /* Check if board button is pressed, if so mouse mode enabled */ if (Buttons_GetStatus() & BUTTONS_BUTTON1) { @@ -300,23 +306,19 @@ void Mouse_HID_Task(void) MouseReportData.Button = (1 << 0); } - /* Check if the USB system is connected to a host and report protocol mode is enabled */ - if (USB_IsConnected) - { - /* Select the Mouse Report Endpoint */ - Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); + /* Select the Mouse Report Endpoint */ + Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); - /* Check if Mouse Endpoint Ready for Read/Write */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Write Mouse Report Data */ - Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData)); + /* Check if Mouse Endpoint Ready for Read/Write */ + if (Endpoint_IsReadWriteAllowed()) + { + /* Write Mouse Report Data */ + Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData)); - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); - /* Clear the report data afterwards */ - memset(&MouseReportData, 0, sizeof(MouseReportData)); - } + /* Clear the report data afterwards */ + memset(&MouseReportData, 0, sizeof(MouseReportData)); } }