3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Main source file for the Joystick demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
39 /* Project Tags, for reading out using the ButtLoad project */
40 BUTTLOADTAG(ProjName
, "LUFA Joystick App");
41 BUTTLOADTAG(BuildTime
, __TIME__
);
42 BUTTLOADTAG(BuildDate
, __DATE__
);
43 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
);
45 /* Scheduler Task List */
48 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
49 { Task
: USB_Joystick_Report
, TaskStatus
: TASK_STOP
},
52 /** Main program entry point. This routine configures the hardware required by the application, then
53 * starts the scheduler to run the application tasks.
57 /* Disable watchdog if enabled by bootloader/fuses */
58 MCUSR
&= ~(1 << WDRF
);
61 /* Disable clock division */
62 clock_prescale_set(clock_div_1
);
64 /* Hardware Initialization */
69 /* Indicate USB not ready */
70 UpdateStatus(Status_USBNotReady
);
72 /* Initialize Scheduler so that it can be used */
75 /* Initialize USB Subsystem */
78 /* Scheduling - routine never returns, so put this last in the main function */
82 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
83 * starts the library USB task to begin the enumeration and USB management process.
85 EVENT_HANDLER(USB_Connect
)
87 /* Start USB management task */
88 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
90 /* Indicate USB enumerating */
91 UpdateStatus(Status_USBEnumerating
);
94 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
95 * the status LEDs and stops the USB management and joystick reporting tasks.
97 EVENT_HANDLER(USB_Disconnect
)
99 /* Stop running joystick reporting and USB management tasks */
100 Scheduler_SetTaskMode(USB_Joystick_Report
, TASK_STOP
);
101 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
103 /* Indicate USB not ready */
104 UpdateStatus(Status_USBNotReady
);
107 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
108 * of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.
110 EVENT_HANDLER(USB_ConfigurationChanged
)
112 /* Setup Joystick Report Endpoint */
113 Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM
, EP_TYPE_INTERRUPT
,
114 ENDPOINT_DIR_IN
, JOYSTICK_EPSIZE
,
115 ENDPOINT_BANK_SINGLE
);
117 /* Indicate USB connected and ready */
118 UpdateStatus(Status_USBReady
);
120 /* Start joystick reporting task */
121 Scheduler_SetTaskMode(USB_Joystick_Report
, TASK_RUN
);
124 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
125 * control requests that are not handled internally by the USB library (including the HID commands, which are
126 * all issued via the control endpoint), so that they can be handled appropriately for the application.
128 EVENT_HANDLER(USB_UnhandledControlPacket
)
130 /* Handle HID Class specific requests */
134 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
136 USB_JoystickReport_Data_t JoystickReportData
;
138 /* Create the next HID report to send to the host */
139 GetNextReport(&JoystickReportData
);
141 /* Ignore report type and ID number value */
142 Endpoint_Discard_Word();
144 /* Ignore unused Interface number value */
145 Endpoint_Discard_Word();
147 /* Read in the number of bytes in the report to send to the host */
148 uint16_t wLength
= Endpoint_Read_Word_LE();
150 /* If trying to send more bytes than exist to the host, clamp the value at the report size */
151 if (wLength
> sizeof(JoystickReportData
))
152 wLength
= sizeof(JoystickReportData
);
154 Endpoint_ClearSetupReceived();
156 /* Write the report data to the control endpoint */
157 Endpoint_Write_Control_Stream_LE(&JoystickReportData
, wLength
);
159 /* Finalize the stream transfer to send the last packet or clear the host abort */
160 Endpoint_ClearSetupOUT();
167 /** Fills the given HID report data structure with the next HID report to send to the host.
169 * \param ReportData Pointer to a HID report data structure to be filled
171 * \return Boolean true if the new report differs from the last report, false otherwise
173 bool GetNextReport(USB_JoystickReport_Data_t
* ReportData
)
175 static uint8_t PrevJoyStatus
= 0;
176 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
177 bool InputChanged
= false;
179 /* Clear the report contents */
180 memset(ReportData
, 0, sizeof(USB_JoystickReport_Data_t
));
182 if (JoyStatus_LCL
& JOY_UP
)
183 ReportData
->Y
= -100;
184 else if (JoyStatus_LCL
& JOY_DOWN
)
187 if (JoyStatus_LCL
& JOY_RIGHT
)
189 else if (JoyStatus_LCL
& JOY_LEFT
)
190 ReportData
->X
= -100;
192 if (JoyStatus_LCL
& JOY_PRESS
)
193 ReportData
->Button
= (1 << 1);
196 ReportData
->Button
|= (1 << 0);
198 /* Check if the new report is different to the previous report */
199 InputChanged
= (uint8_t)(PrevJoyStatus
^ JoyStatus_LCL
);
201 /* Save the current joystick status for later comparison */
202 PrevJoyStatus
= JoyStatus_LCL
;
204 /* Return whether the new report is different to the previous report or not */
208 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
209 * log to a serial port, or anything else that is suitable for status updates.
211 * \param CurrentStatus Current status of the system, from the Joystick_StatusCodes_t enum
213 void UpdateStatus(uint8_t CurrentStatus
)
215 uint8_t LEDMask
= LEDS_NO_LEDS
;
217 /* Set the LED mask to the appropriate LED mask based on the given status code */
218 switch (CurrentStatus
)
220 case Status_USBNotReady
:
221 LEDMask
= (LEDS_LED1
);
223 case Status_USBEnumerating
:
224 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
226 case Status_USBReady
:
227 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
231 /* Set the board LEDs to the new LED mask */
232 LEDs_SetAllLEDs(LEDMask
);
235 /** Function to manage HID report generation and transmission to the host. */
236 TASK(USB_Joystick_Report
)
238 /* Check if the USB System is connected to a Host */
241 /* Select the Joystick Report Endpoint */
242 Endpoint_SelectEndpoint(JOYSTICK_EPNUM
);
244 /* Check if Joystick Endpoint Ready for Read/Write */
245 if (Endpoint_ReadWriteAllowed())
247 USB_JoystickReport_Data_t JoystickReportData
;
249 /* Create the next HID report to send to the host */
250 GetNextReport(&JoystickReportData
);
252 /* Write Joystick Report Data */
253 Endpoint_Write_Stream_LE(&JoystickReportData
, sizeof(JoystickReportData
));
255 /* Finalize the stream transfer to send the last packet */
256 Endpoint_ClearCurrentBank();
258 /* Clear the report data afterwards */
259 JoystickReportData
.X
= 0;
260 JoystickReportData
.Y
= 0;
261 JoystickReportData
.Button
= 0;