Refactor out the common VSIX XSLT params.
[pub/USBasp.git] / Demos / Device / LowLevel / Joystick / Joystick.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 disclaims 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 /** Main program entry point. This routine configures the hardware required by the application, then
40 * enters a loop to run the application tasks in sequence.
41 */
42 int main(void)
43 {
44 SetupHardware();
45
46 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
47 GlobalInterruptEnable();
48
49 for (;;)
50 {
51 HID_Task();
52 USB_USBTask();
53 }
54 }
55
56 /** Configures the board hardware and chip peripherals for the demo's functionality. */
57 void SetupHardware(void)
58 {
59 /* Disable watchdog if enabled by bootloader/fuses */
60 MCUSR &= ~(1 << WDRF);
61 wdt_disable();
62
63 /* Disable clock division */
64 clock_prescale_set(clock_div_1);
65
66 /* Hardware Initialization */
67 Joystick_Init();
68 LEDs_Init();
69 Buttons_Init();
70 USB_Init();
71 }
72
73 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
74 * starts the library USB task to begin the enumeration and USB management process.
75 */
76 void EVENT_USB_Device_Connect(void)
77 {
78 /* Indicate USB enumerating */
79 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
80 }
81
82 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
83 * the status LEDs and stops the USB management and joystick reporting tasks.
84 */
85 void EVENT_USB_Device_Disconnect(void)
86 {
87 /* Indicate USB not ready */
88 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
89 }
90
91 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
92 * of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.
93 */
94 void EVENT_USB_Device_ConfigurationChanged(void)
95 {
96 bool ConfigSuccess = true;
97
98 /* Setup HID Report Endpoint */
99 ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
100
101 /* Indicate endpoint configuration success or failure */
102 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
103 }
104
105 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
106 * the device from the USB host before passing along unhandled control requests to the library for processing
107 * internally.
108 */
109 void EVENT_USB_Device_ControlRequest(void)
110 {
111 /* Handle HID Class specific requests */
112 switch (USB_ControlRequest.bRequest)
113 {
114 case HID_REQ_GetReport:
115 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
116 {
117 USB_JoystickReport_Data_t JoystickReportData;
118
119 /* Create the next HID report to send to the host */
120 GetNextReport(&JoystickReportData);
121
122 Endpoint_ClearSETUP();
123
124 /* Write the report data to the control endpoint */
125 Endpoint_Write_Control_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
126 Endpoint_ClearOUT();
127 }
128
129 break;
130 }
131 }
132
133 /** Fills the given HID report data structure with the next HID report to send to the host.
134 *
135 * \param[out] ReportData Pointer to a HID report data structure to be filled
136 *
137 * \return Boolean \c true if the new report differs from the last report, \c false otherwise
138 */
139 bool GetNextReport(USB_JoystickReport_Data_t* const ReportData)
140 {
141 static uint8_t PrevJoyStatus = 0;
142 static uint8_t PrevButtonStatus = 0;
143 uint8_t JoyStatus_LCL = Joystick_GetStatus();
144 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
145 bool InputChanged = false;
146
147 /* Clear the report contents */
148 memset(ReportData, 0, sizeof(USB_JoystickReport_Data_t));
149
150 if (JoyStatus_LCL & JOY_UP)
151 ReportData->Y = -100;
152 else if (JoyStatus_LCL & JOY_DOWN)
153 ReportData->Y = 100;
154
155 if (JoyStatus_LCL & JOY_LEFT)
156 ReportData->X = -100;
157 else if (JoyStatus_LCL & JOY_RIGHT)
158 ReportData->X = 100;
159
160 if (JoyStatus_LCL & JOY_PRESS)
161 ReportData->Button |= (1 << 1);
162
163 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
164 ReportData->Button |= (1 << 0);
165
166 /* Check if the new report is different to the previous report */
167 InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(PrevButtonStatus ^ ButtonStatus_LCL);
168
169 /* Save the current joystick status for later comparison */
170 PrevJoyStatus = JoyStatus_LCL;
171 PrevButtonStatus = ButtonStatus_LCL;
172
173 /* Return whether the new report is different to the previous report or not */
174 return InputChanged;
175 }
176
177 /** Function to manage HID report generation and transmission to the host. */
178 void HID_Task(void)
179 {
180 /* Device must be connected and configured for the task to run */
181 if (USB_DeviceState != DEVICE_STATE_Configured)
182 return;
183
184 /* Select the Joystick Report Endpoint */
185 Endpoint_SelectEndpoint(JOYSTICK_EPADDR);
186
187 /* Check to see if the host is ready for another packet */
188 if (Endpoint_IsINReady())
189 {
190 USB_JoystickReport_Data_t JoystickReportData;
191
192 /* Create the next HID report to send to the host */
193 GetNextReport(&JoystickReportData);
194
195 /* Write Joystick Report Data */
196 Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData), NULL);
197
198 /* Finalize the stream transfer to send the last packet */
199 Endpoint_ClearIN();
200
201 /* Clear the report data afterwards */
202 memset(&JoystickReportData, 0, sizeof(JoystickReportData));
203 }
204 }
205