From d1e52660368d34d693131f6aff3c8fd8584162e5 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Mon, 1 Jun 2009 11:03:39 +0000 Subject: Commit of new class abstraction APIs for all device demos other than the MIDI demo - not documented yet. Removed scheduler and memory allocation libraries. Added new EVENT_USB_StartOfFrame event in the library to indicate the start of each USB frame (when generated). Removed Tx interrupt from the USBtoSerial demo; now sends characters via polling to ensure more time for the Rx interrupt. --- Demos/Device/Joystick/Joystick.c | 211 +++++++++------------------------------ 1 file changed, 48 insertions(+), 163 deletions(-) (limited to 'Demos/Device/Joystick/Joystick.c') diff --git a/Demos/Device/Joystick/Joystick.c b/Demos/Device/Joystick/Joystick.c index 8c50a2c16..e073a87fd 100644 --- a/Demos/Device/Joystick/Joystick.c +++ b/Demos/Device/Joystick/Joystick.c @@ -28,25 +28,34 @@ this software. */ -/** \file - * - * Main source file for the Joystick demo. This file contains the main tasks of the demo and - * is responsible for the initial application hardware configuration. - */ - #include "Joystick.h" -/* Scheduler Task List */ -TASK_LIST -{ - { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, - { .Task = USB_Joystick_Report , .TaskStatus = TASK_STOP }, -}; +USB_ClassInfo_HID_t Joystick_HID_Interface = + { + .InterfaceNumber = 0, + + .ReportINEndpointNumber = JOYSTICK_EPNUM, + .ReportINEndpointSize = JOYSTICK_EPSIZE, + + .ReportBufferSize = sizeof(USB_JoystickReport_Data_t), + + .UsingReportProtocol = true, + }; -/** Main program entry point. This routine configures the hardware required by the application, then - * starts the scheduler to run the application tasks. - */ int main(void) +{ + SetupHardware(); + + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + + for (;;) + { + USB_HID_USBTask(&Joystick_HID_Interface); + USB_USBTask(); + } +} + +void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); @@ -59,188 +68,64 @@ 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) { - /* 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) { - /* 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) { - /* Setup Joystick Report Endpoint */ - Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, JOYSTICK_EPSIZE, - ENDPOINT_BANK_SINGLE); + LEDs_SetAllLEDs(LEDMASK_USB_READY); - /* Indicate USB connected and ready */ - UpdateStatus(Status_USBReady); - - /* Start joystick reporting task */ - Scheduler_SetTaskMode(USB_Joystick_Report, TASK_RUN); + if (!(USB_HID_ConfigureEndpoints(&Joystick_HID_Interface))) + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); } -/** Event handler for the USB_UnhandledControlPacket 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) { - /* Handle HID Class specific requests */ - switch (USB_ControlRequest.bRequest) - { - case REQ_GetReport: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { - USB_JoystickReport_Data_t JoystickReportData; - - Endpoint_ClearSETUP(); - - /* Create the next HID report to send to the host */ - GetNextReport(&JoystickReportData); - - /* Write the report data to the control endpoint */ - Endpoint_Write_Control_Stream_LE(&JoystickReportData, sizeof(JoystickReportData)); - - /* Finalize the stream transfer to send the last packet or clear the host abort */ - Endpoint_ClearOUT(); - } - - break; - } + USB_HID_ProcessControlPacket(&Joystick_HID_Interface); } -/** 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 - * - * \return Boolean true if the new report differs from the last report, false otherwise - */ -bool GetNextReport(USB_JoystickReport_Data_t* ReportData) +void EVENT_USB_StartOfFrame(void) { - static uint8_t PrevJoyStatus = 0; - static uint8_t PrevButtonStatus = 0; - uint8_t JoyStatus_LCL = Joystick_GetStatus(); - uint8_t ButtonStatus_LCL = Buttons_GetStatus(); - bool InputChanged = false; + USB_HID_RegisterStartOfFrame(&Joystick_HID_Interface); +} - /* Clear the report contents */ - memset(ReportData, 0, sizeof(USB_JoystickReport_Data_t)); +uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData) +{ + USB_JoystickReport_Data_t* JoystickReport = (USB_JoystickReport_Data_t*)ReportData; + + uint8_t JoyStatus_LCL = Joystick_GetStatus(); + uint8_t ButtonStatus_LCL = Buttons_GetStatus(); if (JoyStatus_LCL & JOY_UP) - ReportData->Y = -100; + JoystickReport->Y = -100; else if (JoyStatus_LCL & JOY_DOWN) - ReportData->Y = 100; + JoystickReport->Y = 100; if (JoyStatus_LCL & JOY_RIGHT) - ReportData->X = 100; + JoystickReport->X = 100; else if (JoyStatus_LCL & JOY_LEFT) - ReportData->X = -100; + JoystickReport->X = -100; if (JoyStatus_LCL & JOY_PRESS) - ReportData->Button = (1 << 1); + JoystickReport->Button = (1 << 1); if (ButtonStatus_LCL & BUTTONS_BUTTON1) - ReportData->Button |= (1 << 0); + JoystickReport->Button |= (1 << 0); - /* Check if the new report is different to the previous report */ - InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(PrevButtonStatus ^ ButtonStatus_LCL); - - /* Save the current joystick status for later comparison */ - PrevJoyStatus = JoyStatus_LCL; - PrevButtonStatus = ButtonStatus_LCL; - - /* Return whether the new report is different to the previous report or not */ - return InputChanged; + return sizeof(USB_JoystickReport_Data_t); } -/** 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) +void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData, uint16_t ReportSize) { - uint8_t LEDMask = LEDS_NO_LEDS; - - /* Set the LED mask to the appropriate LED mask based on the given status code */ - switch (CurrentStatus) - { - 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; - } - - /* Set the board LEDs to the new LED mask */ - LEDs_SetAllLEDs(LEDMask); -} - -/** 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); - - /* 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)); - } - } + // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports } -- cgit v1.2.3