Combined Mouse, MouseViaInt and MouseFullInt demos into a single unified demo.
[pub/USBasp.git] / Demos / Mouse / Mouse.c
index f8ec0a0..b61850a 100644 (file)
@@ -45,8 +45,13 @@ BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);
 /* Scheduler Task List */\r
 TASK_LIST\r
 {\r
+       #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
        { Task: USB_USBTask          , TaskStatus: TASK_STOP },\r
+       #endif\r
+       \r
+       #if !defined(INTERRUPT_DATA_ENDPOINT)\r
        { Task: USB_Mouse_Report     , TaskStatus: TASK_STOP },\r
+       #endif\r
 };\r
 \r
 /* Global Variables */\r
@@ -118,14 +123,34 @@ EVENT_HANDLER(USB_Connect)
        UsingReportProtocol = true;\r
 }\r
 \r
+/** Event handler for the USB_Reset event. This fires when the USB interface is reset by the USB host, before the\r
+ *  enumeration process begins, and enables the control endpoint interrupt so that control requests can be handled\r
+ *  asynchronously when they arrive rather than when the control endpoint is polled manually.\r
+ */\r
+EVENT_HANDLER(USB_Reset)\r
+{\r
+       #if defined(INTERRUPT_CONTROL_ENDPOINT)\r
+       /* Select the control endpoint */\r
+       Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);\r
+\r
+       /* Enable the endpoint SETUP interrupt ISR for the control endpoint */\r
+       USB_INT_Enable(ENDPOINT_INT_SETUP);\r
+       #endif\r
+}\r
+\r
 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via\r
  *  the status LEDs and stops the USB management and Mouse reporting tasks.\r
  */\r
 EVENT_HANDLER(USB_Disconnect)\r
 {\r
-       /* Stop running mouse reporting and USB management tasks */\r
+       /* Stop running keyboard reporting and USB management tasks */\r
+       #if !defined(INTERRUPT_DATA_ENDPOINT)\r
        Scheduler_SetTaskMode(USB_Mouse_Report, TASK_STOP);\r
+       #endif\r
+\r
+       #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
        Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);\r
+       #endif\r
 \r
        /* Indicate USB not ready */\r
        UpdateStatus(Status_USBNotReady);\r
@@ -141,11 +166,18 @@ EVENT_HANDLER(USB_ConfigurationChanged)
                                       ENDPOINT_DIR_IN, MOUSE_EPSIZE,\r
                                   ENDPOINT_BANK_SINGLE);\r
 \r
+       #if defined(INTERRUPT_DATA_ENDPOINT)\r
+       /* Enable the endpoint IN interrupt ISR for the report endpoint */\r
+       USB_INT_Enable(ENDPOINT_INT_IN);\r
+       #endif\r
+\r
        /* Indicate USB connected and ready */\r
        UpdateStatus(Status_USBReady);\r
 \r
+       #if !defined(INTERRUPT_DATA_ENDPOINT)\r
        /* Start running mouse reporting task */\r
        Scheduler_SetTaskMode(USB_Mouse_Report, TASK_RUN);\r
+       #endif\r
 }\r
 \r
 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific\r
@@ -163,7 +195,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                USB_MouseReport_Data_t MouseReportData;\r
 \r
                                /* Create the next mouse report for transmission to the host */\r
-                               GetNextReport(&MouseReportData);\r
+                               CreateMouseReport(&MouseReportData);\r
 \r
                                /* Ignore report type and ID number value */\r
                                Endpoint_Discard_Word();\r
@@ -278,7 +310,7 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
  *\r
  *  \return Boolean true if the new report differs from the last report, false otherwise\r
  */\r
-bool GetNextReport(USB_MouseReport_Data_t* ReportData)\r
+bool CreateMouseReport(USB_MouseReport_Data_t* ReportData)\r
 {\r
        static uint8_t PrevJoyStatus = 0;\r
        static bool    PrevHWBStatus = false;\r
@@ -315,6 +347,45 @@ bool GetNextReport(USB_MouseReport_Data_t* ReportData)
        return InputChanged;\r
 }\r
 \r
+/** Sends the next HID report to the host, via the keyboard data endpoint. */\r
+static inline void SendNextReport(void)\r
+{\r
+       USB_MouseReport_Data_t MouseReportData;\r
+       bool                   SendReport = true;\r
+       \r
+       /* Create the next mouse report for transmission to the host */\r
+       CreateMouseReport(&MouseReportData);\r
+       \r
+       /* Check if the idle period is set*/\r
+       if (IdleCount)\r
+       {\r
+               /* Determine if the idle period has elapsed */\r
+               if (!(IdleMSRemaining))\r
+               {\r
+                       /* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */\r
+                       IdleMSRemaining = (IdleCount << 2);             \r
+               }\r
+               else\r
+               {\r
+                       /* Idle period not elapsed, indicate that a report must not be sent */\r
+                       SendReport = false;\r
+               }\r
+       }\r
+       \r
+       /* Select the Mouse Report Endpoint */\r
+       Endpoint_SelectEndpoint(MOUSE_EPNUM);\r
+\r
+       /* Check if Mouse Endpoint Ready for Read/Write and if we should send a new report */\r
+       if (Endpoint_ReadWriteAllowed() && SendReport)\r
+       {\r
+               /* Write Mouse Report Data */\r
+               Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
+               \r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearCurrentBank();\r
+       }\r
+}\r
+\r
 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to\r
  *  log to a serial port, or anything else that is suitable for status updates.\r
  *\r
@@ -342,45 +413,57 @@ void UpdateStatus(uint8_t CurrentStatus)
        LEDs_SetAllLEDs(LEDMask);\r
 }\r
 \r
+#if !defined(INTERRUPT_DATA_ENDPOINT)\r
 /** Task to manage HID report generation and transmission to the host, when in report mode. */\r
 TASK(USB_Mouse_Report)\r
 {\r
-       USB_MouseReport_Data_t MouseReportData;\r
-       bool                   SendReport = true;\r
-       \r
-       /* Create the next mouse report for transmission to the host */\r
-       GetNextReport(&MouseReportData);\r
-       \r
-       /* Check if the idle period is set*/\r
-       if (IdleCount)\r
+       /* Check if the USB system is connected to a host */\r
+       if (USB_IsConnected)\r
        {\r
-               /* Determine if the idle period has elapsed */\r
-               if (!(IdleMSRemaining))\r
-               {\r
-                       /* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */\r
-                       IdleMSRemaining = (IdleCount << 2);             \r
-               }\r
-               else\r
-               {\r
-                       /* Idle period not elapsed, indicate that a report must not be sent */\r
-                       SendReport = false;\r
-               }\r
+               /* Send the next mouse report to the host */\r
+               SendNextReport();\r
        }\r
+}\r
+#endif\r
+\r
+/** ISR for the general Pipe/Endpoint interrupt vector. This ISR fires when an endpoint's status changes (such as\r
+ *  a packet has been received) on an endpoint with its corresponding ISR enabling bits set. This is used to send\r
+ *  HID packets to the host each time the HID interrupt endpoints polling period elapses, as managed by the USB\r
+ *  controller. It is also used to respond to standard and class specific requests send to the device on the control\r
+ *  endpoint, by handing them off to the LUFA library when they are received.\r
+ */\r
+ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)\r
+{\r
+       #if defined(INTERRUPT_CONTROL_ENDPOINT)\r
+       /* Check if the control endpoint has received a request */\r
+       if (Endpoint_HasEndpointInterrupted(ENDPOINT_CONTROLEP))\r
+       {\r
+               /* Clear the endpoint interrupt */\r
+               Endpoint_ClearEndpointInterrupt(ENDPOINT_CONTROLEP);\r
+\r
+               /* Process the control request */\r
+               USB_USBTask();\r
+\r
+               /* Handshake the endpoint setup interrupt - must be after the call to USB_USBTask() */\r
+               USB_INT_Clear(ENDPOINT_INT_SETUP);\r
+       }\r
+       #endif\r
        \r
-       /* Check if the USB system is connected to a host */\r
-       if (USB_IsConnected)\r
+       #if defined(INTERRUPT_DATA_ENDPOINT)\r
+       /* Check if mouse endpoint has interrupted */\r
+       if (Endpoint_HasEndpointInterrupted(MOUSE_EPNUM))\r
        {\r
                /* Select the Mouse Report Endpoint */\r
                Endpoint_SelectEndpoint(MOUSE_EPNUM);\r
 \r
-               /* Check if Mouse Endpoint Ready for Read/Write and if we should send a new report */\r
-               if (Endpoint_ReadWriteAllowed() && SendReport)\r
-               {\r
-                       /* Write Mouse Report Data */\r
-                       Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
-                       \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearCurrentBank();\r
-               }\r
+               /* Clear the endpoint IN interrupt flag */\r
+               USB_INT_Clear(ENDPOINT_INT_IN);\r
+\r
+               /* Clear the Mouse Report endpoint interrupt and select the endpoint */\r
+               Endpoint_ClearEndpointInterrupt(MOUSE_EPNUM);\r
+\r
+               /* Send the next mouse report to the host */\r
+               SendNextReport();\r
        }\r
+       #endif\r
 }\r