Combined Keyboad and Mouse normal and interrupt driven host demos into unified Keyboa...
[pub/lufa.git] / Demos / KeyboardHost / KeyboardHost.c
index 74d6555..6249e52 100644 (file)
@@ -176,6 +176,56 @@ void UpdateStatus(uint8_t CurrentStatus)
        LEDs_SetAllLEDs(LEDMask);\r
 }\r
 \r
+/** Reads in and processes the next report from the attached device, displaying the report\r
+ *  contents on the board LEDs and via the serial port.\r
+ */\r
+void ReadNextReport(void)\r
+{\r
+       USB_KeyboardReport_Data_t KeyboardReport;\r
+               \r
+       /* Select the keyboard report data in pipe */\r
+       Pipe_SelectPipe(KEYBOARD_DATAPIPE);     \r
+\r
+       /* Ensure pipe contains data and is ready to be read before continuing */\r
+       if (!(Pipe_ReadWriteAllowed()))\r
+         return;\r
+\r
+       /* Read in keyboard report data */\r
+       Pipe_Read_Stream_LE(&KeyboardReport, sizeof(KeyboardReport));\r
+                                       \r
+       /* Clear the IN endpoint, ready for next data packet */\r
+       Pipe_ClearCurrentBank();\r
+\r
+       /* Indicate if the modifier byte is non-zero (special key such as shift is being pressed) */\r
+       LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0);\r
+       \r
+       /* Check if a key has been pressed */\r
+       if (KeyboardReport.KeyCode)\r
+       {\r
+               /* Toggle status LED to indicate keypress */\r
+               if (LEDs_GetLEDs() & LEDS_LED2)\r
+                 LEDs_TurnOffLEDs(LEDS_LED2);\r
+               else\r
+                 LEDs_TurnOnLEDs(LEDS_LED2);\r
+                         \r
+               char PressedKey = 0;\r
+\r
+               /* Retrieve pressed key character if alphanumeric */\r
+               if ((KeyboardReport.KeyCode >= 0x04) && (KeyboardReport.KeyCode <= 0x1D))\r
+                 PressedKey = (KeyboardReport.KeyCode - 0x04) + 'A';\r
+               else if ((KeyboardReport.KeyCode >= 0x1E) && (KeyboardReport.KeyCode <= 0x27))\r
+                 PressedKey = (KeyboardReport.KeyCode - 0x1E) + '0';\r
+               else if (KeyboardReport.KeyCode == 0x2C)\r
+                 PressedKey = ' ';                                             \r
+               else if (KeyboardReport.KeyCode == 0x28)\r
+                 PressedKey = '\n';\r
+                        \r
+               /* Print the pressed key character out through the serial port if valid */\r
+               if (PressedKey)\r
+                 putchar(PressedKey);\r
+       }\r
+}\r
+\r
 /** Task to set the configuration of the attached device after it has been enumerated, and to read and process\r
  *  HID reports from the device and display the results onto the board LEDs.\r
  */\r
@@ -261,54 +311,44 @@ TASK(USB_Keyboard_Host)
                                \r
                        USB_HostState = HOST_STATE_Ready;\r
                        break;\r
+               #if !defined(INTERRUPT_DATA_PIPE)\r
                case HOST_STATE_Ready:\r
                        /* Select and unfreeze keyboard data pipe */\r
                        Pipe_SelectPipe(KEYBOARD_DATAPIPE);     \r
                        Pipe_Unfreeze();\r
 \r
-                       /* Check if data has been received from the attached keyboard */\r
+                       /* If a report has been received, read and process it */\r
                        if (Pipe_ReadWriteAllowed())\r
-                       {\r
-                               USB_KeyboardReport_Data_t KeyboardReport;\r
-                                       \r
-                               /* Read in keyboard report data */\r
-                               Pipe_Read_Stream_LE(&KeyboardReport, sizeof(KeyboardReport));\r
-                                                               \r
-                               /* Clear the IN endpoint, ready for next data packet */\r
-                               Pipe_ClearCurrentBank();\r
-\r
-                               /* Indicate if the modifier byte is non-zero */\r
-                               LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0);\r
-                               \r
-                               /* Check if a key has been pressed */\r
-                               if (KeyboardReport.KeyCode)\r
-                               {\r
-                                       /* Toggle status LED to indicate keypress */\r
-                                       if (LEDs_GetLEDs() & LEDS_LED2)\r
-                                         LEDs_TurnOffLEDs(LEDS_LED2);\r
-                                       else\r
-                                         LEDs_TurnOnLEDs(LEDS_LED2);\r
-                                                 \r
-                                       char PressedKey = 0;\r
-\r
-                                       /* Retrieve pressed key character if alphanumeric */\r
-                                       if ((KeyboardReport.KeyCode >= 0x04) && (KeyboardReport.KeyCode <= 0x1D))\r
-                                         PressedKey = (KeyboardReport.KeyCode - 0x04) + 'A';\r
-                                       else if ((KeyboardReport.KeyCode >= 0x1E) && (KeyboardReport.KeyCode <= 0x27))\r
-                                         PressedKey = (KeyboardReport.KeyCode - 0x1E) + '0';\r
-                                       else if (KeyboardReport.KeyCode == 0x2C)\r
-                                         PressedKey = ' ';                                             \r
-                                       else if (KeyboardReport.KeyCode == 0x28)\r
-                                         PressedKey = '\n';\r
-                                                \r
-                                       /* Print the pressed key character out through the serial port if valid */\r
-                                       if (PressedKey)\r
-                                         putchar(PressedKey);\r
-                               }\r
-                       }\r
+                         ReadNextReport();\r
 \r
                        /* Freeze keyboard data pipe */\r
                        Pipe_Freeze();\r
                        break;\r
+               #endif\r
+       }\r
+}\r
+\r
+#if defined(INTERRUPT_DATA_PIPE)\r
+/** Interrupt handler for the Endpoint/Pipe interrupt vector. This interrupt fires each time an enabled\r
+ *  pipe interrupt occurs on a pipe which has had that interrupt enabled.\r
+ */\r
+ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)\r
+{\r
+       /* Check to see if the keyboard data pipe has caused the interrupt */\r
+       if (Pipe_HasPipeInterrupted(KEYBOARD_DATAPIPE))\r
+       {\r
+               /* Clear the pipe interrupt, and select the keyboard pipe */\r
+               Pipe_ClearPipeInterrupt(KEYBOARD_DATAPIPE);\r
+               Pipe_SelectPipe(KEYBOARD_DATAPIPE);     \r
+\r
+               /* Check to see if the pipe IN interrupt has fired */\r
+               if (USB_INT_HasOccurred(PIPE_INT_IN) && USB_INT_IsEnabled(PIPE_INT_IN))\r
+               {\r
+                       /* Clear interrupt flag */\r
+                       USB_INT_Clear(PIPE_INT_IN);             \r
+\r
+                       /* Read and process the next report from the device */\r
+                       ReadNextReport();\r
        }\r
 }\r
+#endif\r