Combined Keyboad and Mouse normal and interrupt driven host demos into unified Keyboa...
[pub/USBasp.git] / Demos / MouseHost / MouseHost.c
index ee7aec9..44f20b4 100644 (file)
@@ -176,6 +176,51 @@ 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_MouseReport_Data_t MouseReport;\r
+       uint8_t                LEDMask = LEDS_NO_LEDS;\r
+\r
+       /* Select the mouse report data in pipe */\r
+       Pipe_SelectPipe(MOUSE_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 mouse report data */\r
+       Pipe_Read_Stream_LE(&MouseReport, sizeof(MouseReport));                         \r
+               \r
+       /* Clear the IN endpoint, ready for next data packet */\r
+       Pipe_ClearCurrentBank();\r
+               \r
+       /* Alter status LEDs according to mouse X movement */\r
+       if (MouseReport.X > 0)\r
+         LEDMask |= LEDS_LED1;\r
+       else if (MouseReport.X < 0)\r
+         LEDMask |= LEDS_LED2;\r
+               \r
+       /* Alter status LEDs according to mouse Y movement */\r
+       if (MouseReport.Y > 0)\r
+         LEDMask |= LEDS_LED3;\r
+       else if (MouseReport.Y < 0)\r
+         LEDMask |= LEDS_LED4;\r
+\r
+       /* Alter status LEDs according to mouse button position */\r
+       if (MouseReport.Button)\r
+         LEDMask  = LEDS_ALL_LEDS;\r
+       \r
+       LEDs_SetAllLEDs(LEDMask);\r
+       \r
+       /* Print mouse report data through the serial port */\r
+       printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X,\r
+                                                                                                 MouseReport.Y,\r
+                                                                                                 MouseReport.Button);\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
@@ -262,49 +307,45 @@ TASK(USB_Mouse_Host)
                        \r
                        USB_HostState = HOST_STATE_Ready;\r
                        break;\r
+               #if !defined(INTERRUPT_DATA_PIPE)\r
                case HOST_STATE_Ready:\r
                        /* Select and unfreeze mouse data pipe */\r
-                       Pipe_SelectPipe(MOUSE_DATAPIPE);        \r
+                       Pipe_SelectPipe(MOUSE_DATAPIPE);\r
                        Pipe_Unfreeze();\r
 \r
-                       /* Check if data has been received from the attached mouse */\r
+                       /* If a report has been received, read and process it */\r
                        if (Pipe_ReadWriteAllowed())\r
-                       {\r
-                               USB_MouseReport_Data_t MouseReport;\r
-                               uint8_t                LEDMask = LEDS_NO_LEDS;\r
-\r
-                               /* Read in mouse report data */\r
-                               Pipe_Read_Stream_LE(&MouseReport, sizeof(MouseReport));                         \r
-                                       \r
-                               /* Clear the IN endpoint, ready for next data packet */\r
-                               Pipe_ClearCurrentBank();\r
-\r
-                               /* Alter status LEDs according to mouse X movement */\r
-                               if (MouseReport.X > 0)\r
-                                 LEDMask |= LEDS_LED1;\r
-                               else if (MouseReport.X < 0)\r
-                                 LEDMask |= LEDS_LED2;\r
-                               \r
-                               /* Alter status LEDs according to mouse Y movement */\r
-                               if (MouseReport.Y > 0)\r
-                                 LEDMask |= LEDS_LED3;\r
-                               else if (MouseReport.Y < 0)\r
-                                 LEDMask |= LEDS_LED4;\r
-\r
-                               /* Alter status LEDs according to mouse button position */\r
-                               if (MouseReport.Button)\r
-                                 LEDMask = LEDS_ALL_LEDS;\r
-                                 \r
-                               LEDs_SetAllLEDs(LEDMask);\r
-                               \r
-                               /* Print mouse report data through the serial port */\r
-                               printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport.X,\r
-                                                                             MouseReport.Y,\r
-                                                                             MouseReport.Button);\r
-                       }\r
+                         ReadNextReport();\r
 \r
                        /* Freeze mouse 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 mouse data pipe has caused the interrupt */\r
+       if (Pipe_HasPipeInterrupted(MOUSE_DATAPIPE))\r
+       {\r
+               /* Clear the pipe interrupt, and select the mouse pipe */\r
+               Pipe_ClearPipeInterrupt(MOUSE_DATAPIPE);\r
+               Pipe_SelectPipe(MOUSE_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
 }\r
+#endif\r