Removed new Start of Frame event from the library; performance suffered far too much...
[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 .ReportINBufferSize = 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 .ReportINBufferSize = 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 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
91 OCR0A = ((F_CPU / 64) / 1000);
92 TCCR0A = (1 << WGM01);
93 TCCR0B = ((1 << CS01) | (1 << CS00));
94 TIMSK0 = (1 << OCIE0A);
95 }
96
97 void EVENT_USB_Connect(void)
98 {
99 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
100 }
101
102 void EVENT_USB_Disconnect(void)
103 {
104 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
105 }
106
107 void EVENT_USB_ConfigurationChanged(void)
108 {
109 LEDs_SetAllLEDs(LEDMASK_USB_READY);
110
111 if (!(USB_HID_ConfigureEndpoints(&Keyboard_HID_Interface)))
112 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
113
114 if (!(USB_HID_ConfigureEndpoints(&Mouse_HID_Interface)))
115 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
116 }
117
118 void EVENT_USB_UnhandledControlPacket(void)
119 {
120 USB_HID_ProcessControlPacket(&Keyboard_HID_Interface);
121 USB_HID_ProcessControlPacket(&Mouse_HID_Interface);
122 }
123
124 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
125 {
126 if (Keyboard_HID_Interface.IdleMSRemaining)
127 Keyboard_HID_Interface.IdleMSRemaining--;
128
129 if (Mouse_HID_Interface.IdleMSRemaining)
130 Mouse_HID_Interface.IdleMSRemaining--;
131 }
132
133 uint16_t CALLBACK_USB_HID_CreateNextHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData)
134 {
135 uint8_t JoyStatus_LCL = Joystick_GetStatus();
136 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
137
138 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
139 {
140 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
141
142 /* If first board button not being held down, no keyboard report */
143 if (!(ButtonStatus_LCL & BUTTONS_BUTTON1))
144 return 0;
145
146 if (JoyStatus_LCL & JOY_UP)
147 KeyboardReport->KeyCode[0] = 0x04; // A
148 else if (JoyStatus_LCL & JOY_DOWN)
149 KeyboardReport->KeyCode[0] = 0x05; // B
150
151 if (JoyStatus_LCL & JOY_LEFT)
152 KeyboardReport->KeyCode[0] = 0x06; // C
153 else if (JoyStatus_LCL & JOY_RIGHT)
154 KeyboardReport->KeyCode[0] = 0x07; // D
155
156 if (JoyStatus_LCL & JOY_PRESS)
157 KeyboardReport->KeyCode[0] = 0x08; // E
158
159 return sizeof(USB_KeyboardReport_Data_t);
160 }
161 else
162 {
163 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
164
165 /* If first board button being held down, no mouse report */
166 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
167 return 0;
168
169 if (JoyStatus_LCL & JOY_UP)
170 MouseReport->Y = -1;
171 else if (JoyStatus_LCL & JOY_DOWN)
172 MouseReport->Y = 1;
173
174 if (JoyStatus_LCL & JOY_RIGHT)
175 MouseReport->X = 1;
176 else if (JoyStatus_LCL & JOY_LEFT)
177 MouseReport->X = -1;
178
179 if (JoyStatus_LCL & JOY_PRESS)
180 MouseReport->Button = (1 << 0);
181
182 return sizeof(USB_MouseReport_Data_t);
183 }
184 }
185
186 void CALLBACK_USB_HID_ProcessReceivedHIDReport(USB_ClassInfo_HID_t* HIDInterfaceInfo, void* ReportData, uint16_t ReportSize)
187 {
188 if (HIDInterfaceInfo == &Keyboard_HID_Interface)
189 {
190 uint8_t LEDMask = LEDS_NO_LEDS;
191 uint8_t* LEDReport = (uint8_t*)ReportData;
192
193 if (*LEDReport & 0x01) // NUM Lock
194 LEDMask |= LEDS_LED1;
195
196 if (*LEDReport & 0x02) // CAPS Lock
197 LEDMask |= LEDS_LED3;
198
199 if (*LEDReport & 0x04) // SCROLL Lock
200 LEDMask |= LEDS_LED4;
201
202 LEDs_SetAllLEDs(LEDMask);
203 }
204 }