X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/b9b03aadb219d06fbad9d110e508db93e45461af..bb3879331211a19c3adc3927cac870cc7e36b775:/Demos/Device/LowLevel/Joystick/Joystick.c diff --git a/Demos/Device/LowLevel/Joystick/Joystick.c b/Demos/Device/LowLevel/Joystick/Joystick.c index 8c50a2c16..aa5244c15 100644 --- a/Demos/Device/LowLevel/Joystick/Joystick.c +++ b/Demos/Device/LowLevel/Joystick/Joystick.c @@ -36,18 +36,25 @@ #include "Joystick.h" -/* Scheduler Task List */ -TASK_LIST -{ - { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, - { .Task = USB_Joystick_Report , .TaskStatus = TASK_STOP }, -}; - /** Main program entry point. This routine configures the hardware required by the application, then - * starts the scheduler to run the application tasks. + * enters a loop to run the application tasks in sequence. */ int main(void) { + SetupHardware(); + + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + + for (;;) + { + HID_Task(); + USB_USBTask(); + } +} + +/** Configures the board hardware and chip peripherals for the demo's functionality. */ +void SetupHardware(void) +{ /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); @@ -59,67 +66,49 @@ int main(void) Joystick_Init(); LEDs_Init(); Buttons_Init(); - - /* Indicate USB not ready */ - UpdateStatus(Status_USBNotReady); - - /* Initialize Scheduler so that it can be used */ - Scheduler_Init(); - - /* Initialize USB Subsystem */ USB_Init(); - - /* Scheduling - routine never returns, so put this last in the main function */ - Scheduler_Start(); } /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and * starts the library USB task to begin the enumeration and USB management process. */ -void EVENT_USB_Connect(void) +void EVENT_USB_Device_Connect(void) { - /* Start USB management task */ - Scheduler_SetTaskMode(USB_USBTask, TASK_RUN); - /* Indicate USB enumerating */ - UpdateStatus(Status_USBEnumerating); + LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); } /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via * the status LEDs and stops the USB management and joystick reporting tasks. */ -void EVENT_USB_Disconnect(void) +void EVENT_USB_Device_Disconnect(void) { - /* Stop running joystick reporting and USB management tasks */ - Scheduler_SetTaskMode(USB_Joystick_Report, TASK_STOP); - Scheduler_SetTaskMode(USB_USBTask, TASK_STOP); - /* Indicate USB not ready */ - UpdateStatus(Status_USBNotReady); + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); } /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration * of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started. */ -void EVENT_USB_ConfigurationChanged(void) +void EVENT_USB_Device_ConfigurationChanged(void) { - /* Setup Joystick Report Endpoint */ - Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, JOYSTICK_EPSIZE, - ENDPOINT_BANK_SINGLE); - /* Indicate USB connected and ready */ - UpdateStatus(Status_USBReady); + LEDs_SetAllLEDs(LEDMASK_USB_READY); - /* Start joystick reporting task */ - Scheduler_SetTaskMode(USB_Joystick_Report, TASK_RUN); + /* Setup Joystick Report Endpoint */ + if (!(Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM, EP_TYPE_INTERRUPT, + ENDPOINT_DIR_IN, JOYSTICK_EPSIZE, + ENDPOINT_BANK_SINGLE))) + { + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + } } -/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific +/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific * control requests that are not handled internally by the USB library (including the HID commands, which are * all issued via the control endpoint), so that they can be handled appropriately for the application. */ -void EVENT_USB_UnhandledControlPacket(void) +void EVENT_USB_Device_UnhandledControlRequest(void) { /* Handle HID Class specific requests */ switch (USB_ControlRequest.bRequest) @@ -147,7 +136,7 @@ void EVENT_USB_UnhandledControlPacket(void) /** Fills the given HID report data structure with the next HID report to send to the host. * - * \param ReportData Pointer to a HID report data structure to be filled + * \param[out] ReportData Pointer to a HID report data structure to be filled * * \return Boolean true if the new report differs from the last report, false otherwise */ @@ -167,10 +156,10 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData) else if (JoyStatus_LCL & JOY_DOWN) ReportData->Y = 100; - if (JoyStatus_LCL & JOY_RIGHT) - ReportData->X = 100; - else if (JoyStatus_LCL & JOY_LEFT) + if (JoyStatus_LCL & JOY_LEFT) ReportData->X = -100; + else if (JoyStatus_LCL & JOY_RIGHT) + ReportData->X = 100; if (JoyStatus_LCL & JOY_PRESS) ReportData->Button = (1 << 1); @@ -189,58 +178,31 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData) return InputChanged; } -/** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to - * log to a serial port, or anything else that is suitable for status updates. - * - * \param CurrentStatus Current status of the system, from the Joystick_StatusCodes_t enum - */ -void UpdateStatus(uint8_t CurrentStatus) +/** Function to manage HID report generation and transmission to the host. */ +void HID_Task(void) { - uint8_t LEDMask = LEDS_NO_LEDS; - - /* Set the LED mask to the appropriate LED mask based on the given status code */ - switch (CurrentStatus) + /* Device must be connected and configured for the task to run */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + + /* Select the Joystick Report Endpoint */ + Endpoint_SelectEndpoint(JOYSTICK_EPNUM); + + /* Check to see if the host is ready for another packet */ + if (Endpoint_IsINReady()) { - case Status_USBNotReady: - LEDMask = (LEDS_LED1); - break; - case Status_USBEnumerating: - LEDMask = (LEDS_LED1 | LEDS_LED2); - break; - case Status_USBReady: - LEDMask = (LEDS_LED2 | LEDS_LED4); - break; - } + USB_JoystickReport_Data_t JoystickReportData; + + /* Create the next HID report to send to the host */ + GetNextReport(&JoystickReportData); - /* Set the board LEDs to the new LED mask */ - LEDs_SetAllLEDs(LEDMask); -} + /* Write Joystick Report Data */ + Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData)); -/** Function to manage HID report generation and transmission to the host. */ -TASK(USB_Joystick_Report) -{ - /* Check if the USB System is connected to a Host */ - if (USB_IsConnected) - { - /* Select the Joystick Report Endpoint */ - Endpoint_SelectEndpoint(JOYSTICK_EPNUM); - - /* Check to see if the host is ready for another packet */ - if (Endpoint_IsINReady()) - { - USB_JoystickReport_Data_t JoystickReportData; - - /* Create the next HID report to send to the host */ - GetNextReport(&JoystickReportData); + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); - /* Write Joystick Report Data */ - Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData)); - - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); - - /* Clear the report data afterwards */ - memset(&JoystickReportData, 0, sizeof(JoystickReportData)); - } + /* Clear the report data afterwards */ + memset(&JoystickReportData, 0, sizeof(JoystickReportData)); } }