Add explicit guards to all device mode tasks to ensure the device is connected and...
[pub/USBasp.git] / Demos / Device / LowLevel / MIDI / MIDI.c
index 84d3778..954cbcb 100644 (file)
@@ -116,16 +116,17 @@ void MIDI_Task(void)
 {\r
        static uint8_t PrevJoystickStatus;\r
 \r
-       /* Select the MIDI IN stream */\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+\r
        Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);\r
 \r
-       /* Check if endpoint is ready to be written to */\r
        if (Endpoint_IsINReady())\r
        {\r
                uint8_t MIDICommand = 0;\r
                uint8_t MIDIPitch;\r
        \r
-               /* Get current joystick mask, XOR with previous to detect joystick changes */\r
                uint8_t JoystickStatus  = Joystick_GetStatus();\r
                uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);\r
                \r