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 /* Scheduler Task List */
42 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
43 { Task
: USB_Joystick_Report
, TaskStatus
: TASK_STOP
},
46 /** Main program entry point. This routine configures the hardware required by the application, then
47 * starts the scheduler to run the application tasks.
51 /* Disable watchdog if enabled by bootloader/fuses */
52 MCUSR
&= ~(1 << WDRF
);
55 /* Disable clock division */
56 clock_prescale_set(clock_div_1
);
58 /* Hardware Initialization */
63 /* Indicate USB not ready */
64 UpdateStatus(Status_USBNotReady
);
66 /* Initialize Scheduler so that it can be used */
69 /* Initialize USB Subsystem */
72 /* Scheduling - routine never returns, so put this last in the main function */
76 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
77 * starts the library USB task to begin the enumeration and USB management process.
79 EVENT_HANDLER(USB_Connect
)
81 /* Start USB management task */
82 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
84 /* Indicate USB enumerating */
85 UpdateStatus(Status_USBEnumerating
);
88 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
89 * the status LEDs and stops the USB management and joystick reporting tasks.
91 EVENT_HANDLER(USB_Disconnect
)
93 /* Stop running joystick reporting and USB management tasks */
94 Scheduler_SetTaskMode(USB_Joystick_Report
, TASK_STOP
);
95 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
97 /* Indicate USB not ready */
98 UpdateStatus(Status_USBNotReady
);
101 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
102 * of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.
104 EVENT_HANDLER(USB_ConfigurationChanged
)
106 /* Setup Joystick Report Endpoint */
107 Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM
, EP_TYPE_INTERRUPT
,
108 ENDPOINT_DIR_IN
, JOYSTICK_EPSIZE
,
109 ENDPOINT_BANK_SINGLE
);
111 /* Indicate USB connected and ready */
112 UpdateStatus(Status_USBReady
);
114 /* Start joystick reporting task */
115 Scheduler_SetTaskMode(USB_Joystick_Report
, TASK_RUN
);
118 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
119 * control requests that are not handled internally by the USB library (including the HID commands, which are
120 * all issued via the control endpoint), so that they can be handled appropriately for the application.
122 EVENT_HANDLER(USB_UnhandledControlPacket
)
124 /* Handle HID Class specific requests */
128 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
130 USB_JoystickReport_Data_t JoystickReportData
;
132 /* Create the next HID report to send to the host */
133 GetNextReport(&JoystickReportData
);
135 /* Ignore report type and ID number value */
136 Endpoint_Discard_Word();
138 /* Ignore unused Interface number value */
139 Endpoint_Discard_Word();
141 /* Read in the number of bytes in the report to send to the host */
142 uint16_t wLength
= Endpoint_Read_Word_LE();
144 /* If trying to send more bytes than exist to the host, clamp the value at the report size */
145 if (wLength
> sizeof(JoystickReportData
))
146 wLength
= sizeof(JoystickReportData
);
148 Endpoint_ClearControlSETUP();
150 /* Write the report data to the control endpoint */
151 Endpoint_Write_Control_Stream_LE(&JoystickReportData
, wLength
);
153 /* Finalize the stream transfer to send the last packet or clear the host abort */
154 Endpoint_ClearControlOUT();
161 /** Fills the given HID report data structure with the next HID report to send to the host.
163 * \param ReportData Pointer to a HID report data structure to be filled
165 * \return Boolean true if the new report differs from the last report, false otherwise
167 bool GetNextReport(USB_JoystickReport_Data_t
* ReportData
)
169 static uint8_t PrevJoyStatus
= 0;
170 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
171 bool InputChanged
= false;
173 /* Clear the report contents */
174 memset(ReportData
, 0, sizeof(USB_JoystickReport_Data_t
));
176 if (JoyStatus_LCL
& JOY_UP
)
177 ReportData
->Y
= -100;
178 else if (JoyStatus_LCL
& JOY_DOWN
)
181 if (JoyStatus_LCL
& JOY_RIGHT
)
183 else if (JoyStatus_LCL
& JOY_LEFT
)
184 ReportData
->X
= -100;
186 if (JoyStatus_LCL
& JOY_PRESS
)
187 ReportData
->Button
= (1 << 1);
190 ReportData
->Button
|= (1 << 0);
192 /* Check if the new report is different to the previous report */
193 InputChanged
= (uint8_t)(PrevJoyStatus
^ JoyStatus_LCL
);
195 /* Save the current joystick status for later comparison */
196 PrevJoyStatus
= JoyStatus_LCL
;
198 /* Return whether the new report is different to the previous report or not */
202 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
203 * log to a serial port, or anything else that is suitable for status updates.
205 * \param CurrentStatus Current status of the system, from the Joystick_StatusCodes_t enum
207 void UpdateStatus(uint8_t CurrentStatus
)
209 uint8_t LEDMask
= LEDS_NO_LEDS
;
211 /* Set the LED mask to the appropriate LED mask based on the given status code */
212 switch (CurrentStatus
)
214 case Status_USBNotReady
:
215 LEDMask
= (LEDS_LED1
);
217 case Status_USBEnumerating
:
218 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
220 case Status_USBReady
:
221 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
225 /* Set the board LEDs to the new LED mask */
226 LEDs_SetAllLEDs(LEDMask
);
229 /** Function to manage HID report generation and transmission to the host. */
230 TASK(USB_Joystick_Report
)
232 /* Check if the USB System is connected to a Host */
235 /* Select the Joystick Report Endpoint */
236 Endpoint_SelectEndpoint(JOYSTICK_EPNUM
);
238 /* Check to see if the host is ready for another packet */
239 if (Endpoint_IsINReady())
241 USB_JoystickReport_Data_t JoystickReportData
;
243 /* Create the next HID report to send to the host */
244 GetNextReport(&JoystickReportData
);
246 /* Write Joystick Report Data */
247 Endpoint_Write_Stream_LE(&JoystickReportData
, sizeof(JoystickReportData
));
249 /* Finalize the stream transfer to send the last packet */
252 /* Clear the report data afterwards */
253 memset(&JoystickReportData
, 0, sizeof(JoystickReportData
));