Commit of new class abstraction APIs for all device demos other than the MIDI demo...
[pub/USBasp.git] / Demos / Device / KeyboardMouse / KeyboardMouse.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 Copyright 2009 Denver Gingerich (denver [at] ossguy [dot] com)
12
13 Permission to use, copy, modify, and distribute this software
14 and its documentation for any purpose and without fee is hereby
15 granted, provided that the above copyright notice appear in all
16 copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaim all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 #include "KeyboardMouse.h"
33
34 USB_ClassInfo_HID_t Keyboard_HID_Interface =
35 {
36 .InterfaceNumber = 0,
37
38 .ReportINEndpointNumber = KEYBOARD_IN_EPNUM,
39 .ReportINEndpointSize = HID_EPSIZE,
40
41 .ReportOUTEndpointNumber = KEYBOARD_OUT_EPNUM,
42 .ReportOUTEndpointSize = HID_EPSIZE,
43
44 .ReportBufferSize = sizeof(USB_KeyboardReport_Data_t),
45
46 .IdleCount = 500,
47 };
48
49 USB_ClassInfo_HID_t Mouse_HID_Interface =
50 {
51 .InterfaceNumber = 0,
52
53 .ReportINEndpointNumber = MOUSE_IN_EPNUM,
54 .ReportINEndpointSize = HID_EPSIZE,
55
56 .ReportBufferSize = sizeof(USB_MouseReport_Data_t),
57
58 .ReportOUTEndpointNumber = 0,
59 .ReportOUTEndpointSize = 0,
60 };
61
62 int main(void)
63 {
64 SetupHardware();
65
66 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
67
68 for (;;)
69 {
70 USB_HID_USBTask(&Keyboard_HID_Interface);
71 USB_HID_USBTask(&Mouse_HID_Interface);
72 USB_USBTask();
73 }
74 }
75
76 void SetupHardware()
77 {
78 /* Disable watchdog if enabled by bootloader/fuses */
79 MCUSR &= ~(1 << WDRF);
80 wdt_disable();
81
82 /* Disable clock division */
83 clock_prescale_set(clock_div_1);
84
85 /* Hardware Initialization */
86 Joystick_Init();
87 LEDs_Init();
88 USB_Init();
89 }
90
91 void EVENT_USB_Connect(void)
92 {
93 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
94 }
95
96 void EVENT_USB_Disconnect(void)
97 {
98 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
99 }
100
101 void EVENT_USB_ConfigurationChanged(void)
102 {
103 LEDs_SetAllLEDs(LEDMASK_USB_READY);
104
105 if (!(USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface)))
106 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
107
108 if (!(USB_HID_ConfigureEndpoints(&Mouse_HID_Interface)))
109 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
110 }
111
112 void EVENT_USB_UnhandledControlPacket(void)
113 {
114 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface);
115 USB_HID_ProcessControlPacket(&Mouse_HID_Interface);
116 }
117
118 void EVENT_USB_StartOfFrame(void)
119 {
120 USB_HID_RegisterStartOfFrame(&Keyboard_HID_Interface);
121 USB_HID_RegisterStartOfFrame(&Mouse_HID_Interface);
122 }
123
124 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData)
125 {
126 uint8_t JoyStatus_LCL = Joystick_GetStatus();
127 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
128
129 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
130 {
131 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
132
133 /* If first board button not being held down, no keyboard report */
134 if (!(ButtonStatus_LCL & BUTTONS_BUTTON1))
135 return 0;
136
137 if (JoyStatus_LCL & JOY_UP)
138 KeyboardReport->KeyCode[0] = 0x04; // A
139 else if (JoyStatus_LCL & JOY_DOWN)
140 KeyboardReport->KeyCode[0] = 0x05; // B
141
142 if (JoyStatus_LCL & JOY_LEFT)
143 KeyboardReport->KeyCode[0] = 0x06; // C
144 else if (JoyStatus_LCL & JOY_RIGHT)
145 KeyboardReport->KeyCode[0] = 0x07; // D
146
147 if (JoyStatus_LCL & JOY_PRESS)
148 KeyboardReport->KeyCode[0] = 0x08; // E
149
150 return sizeof(USB_KeyboardReport_Data_t);
151 }
152 else
153 {
154 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
155
156 /* If first board button being held down, no mouse report */
157 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
158 return 0;
159
160 if (JoyStatus_LCL & JOY_UP)
161 MouseReport->Y = -1;
162 else if (JoyStatus_LCL & JOY_DOWN)
163 MouseReport->Y = 1;
164
165 if (JoyStatus_LCL & JOY_RIGHT)
166 MouseReport->X = 1;
167 else if (JoyStatus_LCL & JOY_LEFT)
168 MouseReport->X = -1;
169
170 if (JoyStatus_LCL & JOY_PRESS)
171 MouseReport->Button = (1 << 0);
172
173 return sizeof(USB_MouseReport_Data_t);
174 }
175 }
176
177 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData, uint16_t ReportSize)
178 {
179 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
180 {
181 uint8_t LEDMask = LEDS_NO_LEDS;
182 uint8_t* LEDReport = (uint8_t*)ReportData;
183
184 if (*LEDReport & 0x01) // NUM Lock
185 LEDMask |= LEDS_LED1;
186
187 if (*LEDReport & 0x02) // CAPS Lock
188 LEDMask |= LEDS_LED3;
189
190 if (*LEDReport & 0x04) // SCROLL Lock
191 LEDMask |= LEDS_LED4;
192
193 LEDs_SetAllLEDs(LEDMask);
194 }
195 }