Commit of new class abstraction APIs for all device demos other than the MIDI demo...
[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 #include "Joystick.h"
32
33 USB_ClassInfo_HID_t Joystick_HID_Interface =
34 {
35 .InterfaceNumber = 0,
36
37 .ReportINEndpointNumber = JOYSTICK_EPNUM,
38 .ReportINEndpointSize = JOYSTICK_EPSIZE,
39
40 .ReportBufferSize = sizeof(USB_JoystickReport_Data_t),
41
42 .UsingReportProtocol = true,
43 };
44
45 int main(void)
46 {
47 SetupHardware();
48
49 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
50
51 for (;;)
52 {
53 USB_HID_USBTask(&Joystick_HID_Interface);
54 USB_USBTask();
55 }
56 }
57
58 void SetupHardware(void)
59 {
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
67 /* Hardware Initialization */
68 Joystick_Init();
69 LEDs_Init();
70 Buttons_Init();
71 USB_Init();
72 }
73
74 void EVENT_USB_Connect(void)
75 {
76 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
77 }
78
79 void EVENT_USB_Disconnect(void)
80 {
81 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
82 }
83
84 void EVENT_USB_ConfigurationChanged(void)
85 {
86 LEDs_SetAllLEDs(LEDMASK_USB_READY);
87
88 if (!(USB_HID_ConfigureEndpoints(&Joystick_HID_Interface)))
89 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
90 }
91
92 void EVENT_USB_UnhandledControlPacket(void)
93 {
94 USB_HID_ProcessControlPacket(&Joystick_HID_Interface);
95 }
96
97 void EVENT_USB_StartOfFrame(void)
98 {
99 USB_HID_RegisterStartOfFrame(&Joystick_HID_Interface);
100 }
101
102 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData)
103 {
104 USB_JoystickReport_Data_t* JoystickReport = (USB_JoystickReport_Data_t*)ReportData;
105
106 uint8_t JoyStatus_LCL = Joystick_GetStatus();
107 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
108
109 if (JoyStatus_LCL & JOY_UP)
110 JoystickReport->Y = -100;
111 else if (JoyStatus_LCL & JOY_DOWN)
112 JoystickReport->Y = 100;
113
114 if (JoyStatus_LCL & JOY_RIGHT)
115 JoystickReport->X = 100;
116 else if (JoyStatus_LCL & JOY_LEFT)
117 JoystickReport->X = -100;
118
119 if (JoyStatus_LCL & JOY_PRESS)
120 JoystickReport->Button = (1 << 1);
121
122 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
123 JoystickReport->Button |= (1 << 0);
124
125 return sizeof(USB_JoystickReport_Data_t);
126 }
127
128 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData, uint16_t ReportSize)
129 {
130 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
131 }