Rewritten event system to remove all macros, to make user code clearer.
[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 /* Scheduler Task List */
40 TASK_LIST
41 {
42 { .Task = USB_USBTask , .TaskStatus = TASK_STOP },
43 { .Task = USB_Joystick_Report , .TaskStatus = TASK_STOP },
44 };
45
46 /** Main program entry point. This routine configures the hardware required by the application, then
47 * starts the scheduler to run the application tasks.
48 */
49 int main(void)
50 {
51 /* Disable watchdog if enabled by bootloader/fuses */
52 MCUSR &= ~(1 << WDRF);
53 wdt_disable();
54
55 /* Disable clock division */
56 clock_prescale_set(clock_div_1);
57
58 /* Hardware Initialization */
59 Joystick_Init();
60 LEDs_Init();
61 Buttons_Init();
62
63 /* Indicate USB not ready */
64 UpdateStatus(Status_USBNotReady);
65
66 /* Initialize Scheduler so that it can be used */
67 Scheduler_Init();
68
69 /* Initialize USB Subsystem */
70 USB_Init();
71
72 /* Scheduling - routine never returns, so put this last in the main function */
73 Scheduler_Start();
74 }
75
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.
78 */
79 void EVENT_USB_Connect(void)
80 {
81 /* Start USB management task */
82 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
83
84 /* Indicate USB enumerating */
85 UpdateStatus(Status_USBEnumerating);
86 }
87
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.
90 */
91 void EVENT_USB_Disconnect(void)
92 {
93 /* Stop running joystick reporting and USB management tasks */
94 Scheduler_SetTaskMode(USB_Joystick_Report, TASK_STOP);
95 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
96
97 /* Indicate USB not ready */
98 UpdateStatus(Status_USBNotReady);
99 }
100
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.
103 */
104 void EVENT_USB_ConfigurationChanged(void)
105 {
106 /* Setup Joystick Report Endpoint */
107 Endpoint_ConfigureEndpoint(JOYSTICK_EPNUM, EP_TYPE_INTERRUPT,
108 ENDPOINT_DIR_IN, JOYSTICK_EPSIZE,
109 ENDPOINT_BANK_SINGLE);
110
111 /* Indicate USB connected and ready */
112 UpdateStatus(Status_USBReady);
113
114 /* Start joystick reporting task */
115 Scheduler_SetTaskMode(USB_Joystick_Report, TASK_RUN);
116 }
117
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.
121 */
122 void EVENT_USB_UnhandledControlPacket(void)
123 {
124 /* Handle HID Class specific requests */
125 switch (USB_ControlRequest.bRequest)
126 {
127 case REQ_GetReport:
128 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
129 {
130 USB_JoystickReport_Data_t JoystickReportData;
131
132 Endpoint_ClearSETUP();
133
134 /* Create the next HID report to send to the host */
135 GetNextReport(&JoystickReportData);
136
137 /* Write the report data to the control endpoint */
138 Endpoint_Write_Control_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
139
140 /* Finalize the stream transfer to send the last packet or clear the host abort */
141 Endpoint_ClearOUT();
142 }
143
144 break;
145 }
146 }
147
148 /** Fills the given HID report data structure with the next HID report to send to the host.
149 *
150 * \param ReportData Pointer to a HID report data structure to be filled
151 *
152 * \return Boolean true if the new report differs from the last report, false otherwise
153 */
154 bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
155 {
156 static uint8_t PrevJoyStatus = 0;
157 static uint8_t PrevButtonStatus = 0;
158 uint8_t JoyStatus_LCL = Joystick_GetStatus();
159 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
160 bool InputChanged = false;
161
162 /* Clear the report contents */
163 memset(ReportData, 0, sizeof(USB_JoystickReport_Data_t));
164
165 if (JoyStatus_LCL & JOY_UP)
166 ReportData->Y = -100;
167 else if (JoyStatus_LCL & JOY_DOWN)
168 ReportData->Y = 100;
169
170 if (JoyStatus_LCL & JOY_RIGHT)
171 ReportData->X = 100;
172 else if (JoyStatus_LCL & JOY_LEFT)
173 ReportData->X = -100;
174
175 if (JoyStatus_LCL & JOY_PRESS)
176 ReportData->Button = (1 << 1);
177
178 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
179 ReportData->Button |= (1 << 0);
180
181 /* Check if the new report is different to the previous report */
182 InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(PrevButtonStatus ^ ButtonStatus_LCL);
183
184 /* Save the current joystick status for later comparison */
185 PrevJoyStatus = JoyStatus_LCL;
186 PrevButtonStatus = ButtonStatus_LCL;
187
188 /* Return whether the new report is different to the previous report or not */
189 return InputChanged;
190 }
191
192 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
193 * log to a serial port, or anything else that is suitable for status updates.
194 *
195 * \param CurrentStatus Current status of the system, from the Joystick_StatusCodes_t enum
196 */
197 void UpdateStatus(uint8_t CurrentStatus)
198 {
199 uint8_t LEDMask = LEDS_NO_LEDS;
200
201 /* Set the LED mask to the appropriate LED mask based on the given status code */
202 switch (CurrentStatus)
203 {
204 case Status_USBNotReady:
205 LEDMask = (LEDS_LED1);
206 break;
207 case Status_USBEnumerating:
208 LEDMask = (LEDS_LED1 | LEDS_LED2);
209 break;
210 case Status_USBReady:
211 LEDMask = (LEDS_LED2 | LEDS_LED4);
212 break;
213 }
214
215 /* Set the board LEDs to the new LED mask */
216 LEDs_SetAllLEDs(LEDMask);
217 }
218
219 /** Function to manage HID report generation and transmission to the host. */
220 TASK(USB_Joystick_Report)
221 {
222 /* Check if the USB System is connected to a Host */
223 if (USB_IsConnected)
224 {
225 /* Select the Joystick Report Endpoint */
226 Endpoint_SelectEndpoint(JOYSTICK_EPNUM);
227
228 /* Check to see if the host is ready for another packet */
229 if (Endpoint_IsINReady())
230 {
231 USB_JoystickReport_Data_t JoystickReportData;
232
233 /* Create the next HID report to send to the host */
234 GetNextReport(&JoystickReportData);
235
236 /* Write Joystick Report Data */
237 Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
238
239 /* Finalize the stream transfer to send the last packet */
240 Endpoint_ClearIN();
241
242 /* Clear the report data afterwards */
243 memset(&JoystickReportData, 0, sizeof(JoystickReportData));
244 }
245 }
246 }