]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/LowLevel/Joystick/Joystick.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / LowLevel / Joystick / Joystick.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 #if (ARCH == ARCH_AVR8)
60         /* Disable watchdog if enabled by bootloader/fuses */
61         MCUSR &= ~(1 << WDRF);
62         wdt_disable();
63
64         /* Disable clock division */
65         clock_prescale_set(clock_div_1);
66 #elif (ARCH == ARCH_XMEGA)
67         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
68         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
69         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
70
71         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
72         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
73         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
74
75         PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
76 #endif
77
78         /* Hardware Initialization */
79         Joystick_Init();
80         LEDs_Init();
81         Buttons_Init();
82         USB_Init();
83 }
84
85 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
86  *  starts the library USB task to begin the enumeration and USB management process.
87  */
88 void EVENT_USB_Device_Connect(void)
89 {
90         /* Indicate USB enumerating */
91         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
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 void EVENT_USB_Device_Disconnect(void)
98 {
99         /* Indicate USB not ready */
100         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
101 }
102
103 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
104  *  of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.
105  */
106 void EVENT_USB_Device_ConfigurationChanged(void)
107 {
108         bool ConfigSuccess = true;
109
110         /* Setup HID Report Endpoint */
111         ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
112
113         /* Indicate endpoint configuration success or failure */
114         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
115 }
116
117 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
118  *  the device from the USB host before passing along unhandled control requests to the library for processing
119  *  internally.
120  */
121 void EVENT_USB_Device_ControlRequest(void)
122 {
123         /* Handle HID Class specific requests */
124         switch (USB_ControlRequest.bRequest)
125         {
126                 case HID_REQ_GetReport:
127                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
128                         {
129                                 USB_JoystickReport_Data_t JoystickReportData;
130
131                                 /* Create the next HID report to send to the host */
132                                 GetNextReport(&JoystickReportData);
133
134                                 Endpoint_ClearSETUP();
135
136                                 /* Write the report data to the control endpoint */
137                                 Endpoint_Write_Control_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));
138                                 Endpoint_ClearOUT();
139                         }
140
141                         break;
142         }
143 }
144
145 /** Fills the given HID report data structure with the next HID report to send to the host.
146  *
147  *  \param[out] ReportData  Pointer to a HID report data structure to be filled
148  *
149  *  \return Boolean \c true if the new report differs from the last report, \c false otherwise
150  */
151 bool GetNextReport(USB_JoystickReport_Data_t* const ReportData)
152 {
153         static uint8_t PrevJoyStatus    = 0;
154         static uint8_t PrevButtonStatus = 0;
155         uint8_t        JoyStatus_LCL    = Joystick_GetStatus();
156         uint8_t        ButtonStatus_LCL = Buttons_GetStatus();
157         bool           InputChanged     = false;
158
159         /* Clear the report contents */
160         memset(ReportData, 0, sizeof(USB_JoystickReport_Data_t));
161
162         if (JoyStatus_LCL & JOY_UP)
163           ReportData->Y = -100;
164         else if (JoyStatus_LCL & JOY_DOWN)
165           ReportData->Y =  100;
166
167         if (JoyStatus_LCL & JOY_LEFT)
168           ReportData->X = -100;
169         else if (JoyStatus_LCL & JOY_RIGHT)
170           ReportData->X =  100;
171
172         if (JoyStatus_LCL & JOY_PRESS)
173           ReportData->Button |= (1 << 1);
174
175         if (ButtonStatus_LCL & BUTTONS_BUTTON1)
176           ReportData->Button |= (1 << 0);
177
178         /* Check if the new report is different to the previous report */
179         InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(PrevButtonStatus ^ ButtonStatus_LCL);
180
181         /* Save the current joystick status for later comparison */
182         PrevJoyStatus    = JoyStatus_LCL;
183         PrevButtonStatus = ButtonStatus_LCL;
184
185         /* Return whether the new report is different to the previous report or not */
186         return InputChanged;
187 }
188
189 /** Function to manage HID report generation and transmission to the host. */
190 void HID_Task(void)
191 {
192         /* Device must be connected and configured for the task to run */
193         if (USB_DeviceState != DEVICE_STATE_Configured)
194           return;
195
196         /* Select the Joystick Report Endpoint */
197         Endpoint_SelectEndpoint(JOYSTICK_EPADDR);
198
199         /* Check to see if the host is ready for another packet */
200         if (Endpoint_IsINReady())
201         {
202                 USB_JoystickReport_Data_t JoystickReportData;
203
204                 /* Create the next HID report to send to the host */
205                 GetNextReport(&JoystickReportData);
206
207                 /* Write Joystick Report Data */
208                 Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData), NULL);
209
210                 /* Finalize the stream transfer to send the last packet */
211                 Endpoint_ClearIN();
212
213                 /* Clear the report data afterwards */
214                 memset(&JoystickReportData, 0, sizeof(JoystickReportData));
215         }
216 }
217