Fixed minor issue with the RNDISEthernet demo DHCP protocol decoder routine using...
[pub/USBasp.git] / Demos / Device / Joystick / Joystick.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #include "Joystick.h"
38
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);
44
45 /* Scheduler Task List */
46 TASK_LIST
47 {
48 { Task: USB_USBTask , TaskStatus: TASK_STOP },
49 { Task: USB_Joystick_Report , TaskStatus: TASK_STOP },
50 };
51
52 /** Main program entry point. This routine configures the hardware required by the application, then
53 * starts the scheduler to run the application tasks.
54 */
55 int main(void)
56 {
57 /* Disable watchdog if enabled by bootloader/fuses */
58 MCUSR &= ~(1 << WDRF);
59 wdt_disable();
60
61 /* Disable clock division */
62 clock_prescale_set(clock_div_1);
63
64 /* Hardware Initialization */
65 Joystick_Init();
66 LEDs_Init();
67 HWB_Init();
68
69 /* Indicate USB not ready */
70 UpdateStatus(Status_USBNotReady);
71
72 /* Initialize Scheduler so that it can be used */
73 Scheduler_Init();
74
75 /* Initialize USB Subsystem */
76 USB_Init();
77
78 /* Scheduling - routine never returns, so put this last in the main function */
79 Scheduler_Start();
80 }
81
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.
84 */
85 EVENT_HANDLER(USB_Connect)
86 {
87 /* Start USB management task */
88 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
89
90 /* Indicate USB enumerating */
91 UpdateStatus(Status_USBEnumerating);
92 }
93
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.
96 */
97 EVENT_HANDLER(USB_Disconnect)
98 {
99 /* Stop running joystick reporting and USB management tasks */
100 Scheduler_SetTaskMode(USB_Joystick_Report, TASK_STOP);
101 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
102
103 /* Indicate USB not ready */
104 UpdateStatus(Status_USBNotReady);
105 }
106
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.
109 */
110 EVENT_HANDLER(USB_ConfigurationChanged)
111 {
112 /* Setup Joystick Report Endpoint */
113 Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM, EP_TYPE_INTERRUPT,
114 ENDPOINT_DIR_IN, JOYSTICK_EPSIZE,
115 ENDPOINT_BANK_SINGLE);
116
117 /* Indicate USB connected and ready */
118 UpdateStatus(Status_USBReady);
119
120 /* Start joystick reporting task */
121 Scheduler_SetTaskMode(USB_Joystick_Report, TASK_RUN);
122 }
123
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.
127 */
128 EVENT_HANDLER(USB_UnhandledControlPacket)
129 {
130 /* Handle HID Class specific requests */
131 switch (bRequest)
132 {
133 case REQ_GetReport:
134 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
135 {
136 USB_JoystickReport_Data_t JoystickReportData;
137
138 /* Create the next HID report to send to the host */
139 GetNextReport(&JoystickReportData);
140
141 /* Ignore report type and ID number value */
142 Endpoint_Discard_Word();
143
144 /* Ignore unused Interface number value */
145 Endpoint_Discard_Word();
146
147 /* Read in the number of bytes in the report to send to the host */
148 uint16_t wLength = Endpoint_Read_Word_LE();
149
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);
153
154 Endpoint_ClearSetupReceived();
155
156 /* Write the report data to the control endpoint */
157 Endpoint_Write_Control_Stream_LE(&JoystickReportData, wLength);
158
159 /* Finalize the stream transfer to send the last packet or clear the host abort */
160 Endpoint_ClearSetupOUT();
161 }
162
163 break;
164 }
165 }
166
167 /** Fills the given HID report data structure with the next HID report to send to the host.
168 *
169 * \param ReportData Pointer to a HID report data structure to be filled
170 *
171 * \return Boolean true if the new report differs from the last report, false otherwise
172 */
173 bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
174 {
175 static uint8_t PrevJoyStatus = 0;
176 uint8_t JoyStatus_LCL = Joystick_GetStatus();
177 bool InputChanged = false;
178
179 /* Clear the report contents */
180 memset(ReportData, 0, sizeof(USB_JoystickReport_Data_t));
181
182 if (JoyStatus_LCL & JOY_UP)
183 ReportData->Y = -100;
184 else if (JoyStatus_LCL & JOY_DOWN)
185 ReportData->Y = 100;
186
187 if (JoyStatus_LCL & JOY_RIGHT)
188 ReportData->X = 100;
189 else if (JoyStatus_LCL & JOY_LEFT)
190 ReportData->X = -100;
191
192 if (JoyStatus_LCL & JOY_PRESS)
193 ReportData->Button = (1 << 1);
194
195 if (HWB_GetStatus())
196 ReportData->Button |= (1 << 0);
197
198 /* Check if the new report is different to the previous report */
199 InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL);
200
201 /* Save the current joystick status for later comparison */
202 PrevJoyStatus = JoyStatus_LCL;
203
204 /* Return whether the new report is different to the previous report or not */
205 return InputChanged;
206 }
207
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.
210 *
211 * \param CurrentStatus Current status of the system, from the Joystick_StatusCodes_t enum
212 */
213 void UpdateStatus(uint8_t CurrentStatus)
214 {
215 uint8_t LEDMask = LEDS_NO_LEDS;
216
217 /* Set the LED mask to the appropriate LED mask based on the given status code */
218 switch (CurrentStatus)
219 {
220 case Status_USBNotReady:
221 LEDMask = (LEDS_LED1);
222 break;
223 case Status_USBEnumerating:
224 LEDMask = (LEDS_LED1 | LEDS_LED2);
225 break;
226 case Status_USBReady:
227 LEDMask = (LEDS_LED2 | LEDS_LED4);
228 break;
229 }
230
231 /* Set the board LEDs to the new LED mask */
232 LEDs_SetAllLEDs(LEDMask);
233 }
234
235 /** Function to manage HID report generation and transmission to the host. */
236 TASK(USB_Joystick_Report)
237 {
238 /* Check if the USB System is connected to a Host */
239 if (USB_IsConnected)
240 {
241 /* Select the Joystick Report Endpoint */
242 Endpoint_SelectEndpoint(JOYSTICK_EPNUM);
243
244 /* Check if Joystick Endpoint Ready for Read/Write */
245 if (Endpoint_ReadWriteAllowed())
246 {
247 USB_JoystickReport_Data_t JoystickReportData;
248
249 /* Create the next HID report to send to the host */
250 GetNextReport(&JoystickReportData);
251
252 /* Write Joystick Report Data */
253 Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
254
255 /* Finalize the stream transfer to send the last packet */
256 Endpoint_ClearCurrentBank();
257
258 /* Clear the report data afterwards */
259 JoystickReportData.X = 0;
260 JoystickReportData.Y = 0;
261 JoystickReportData.Button = 0;
262 }
263 }
264 }