Converted device mode low-level demos to schedulerless.
[pub/USBasp.git] / Demos / Device / LowLevel / 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 /** \file
33 *
34 * Main source file for the KeyboardMouse demo. This file contains the main tasks of the demo and
35 * is responsible for the initial application hardware configuration.
36 */
37
38 #include "KeyboardMouse.h"
39
40 /* Global Variables */
41 /** Global structure to hold the current keyboard interface HID report, for transmission to the host */
42 USB_KeyboardReport_Data_t KeyboardReportData;
43
44 /** Global structure to hold the current mouse interface HID report, for transmission to the host */
45 USB_MouseReport_Data_t MouseReportData;
46
47 /** Main program entry point. This routine configures the hardware required by the application, then
48 * starts the scheduler to run the USB management task.
49 */
50 int main(void)
51 {
52 SetupHardware();
53
54 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
55
56 for (;;)
57 {
58 Keyboard_HID_Task();
59 Mouse_HID_Task();
60 USB_USBTask();
61 }
62 }
63
64 /** Configures the board hardware and chip peripherals for the demo's functionality. */
65 void SetupHardware(void)
66 {
67 /* Disable watchdog if enabled by bootloader/fuses */
68 MCUSR &= ~(1 << WDRF);
69 wdt_disable();
70
71 /* Disable clock division */
72 clock_prescale_set(clock_div_1);
73
74 /* Hardware Initialization */
75 Joystick_Init();
76 LEDs_Init();
77 USB_Init();
78 }
79
80 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
81 * starts the library USB task to begin the enumeration and USB management process.
82 */
83 void EVENT_USB_Connect(void)
84 {
85 /* Indicate USB enumerating */
86 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
87 }
88
89 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
90 * the status LEDs and stops the USB management task.
91 */
92 void EVENT_USB_Disconnect(void)
93 {
94 /* Indicate USB not ready */
95 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
96 }
97
98 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
99 * of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
100 */
101 void EVENT_USB_ConfigurationChanged(void)
102 {
103 /* Setup Keyboard Report Endpoint */
104 Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT,
105 ENDPOINT_DIR_IN, HID_EPSIZE,
106 ENDPOINT_BANK_SINGLE);
107
108 /* Setup Keyboard LED Report Endpoint */
109 Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM, EP_TYPE_INTERRUPT,
110 ENDPOINT_DIR_OUT, HID_EPSIZE,
111 ENDPOINT_BANK_SINGLE);
112
113 /* Setup Mouse Report Endpoint */
114 Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT,
115 ENDPOINT_DIR_IN, HID_EPSIZE,
116 ENDPOINT_BANK_SINGLE);
117
118 /* Indicate USB connected and ready */
119 LEDs_SetAllLEDs(LEDMASK_USB_READY);
120 }
121
122 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
123 * control requests that are not handled internally by the USB library (including the HID commands, which are
124 * all issued via the control endpoint), so that they can be handled appropriately for the application.
125 */
126 void EVENT_USB_UnhandledControlPacket(void)
127 {
128 uint8_t* ReportData;
129 uint8_t ReportSize;
130
131 /* Handle HID Class specific requests */
132 switch (USB_ControlRequest.bRequest)
133 {
134 case REQ_GetReport:
135 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
136 {
137 Endpoint_ClearSETUP();
138
139 /* Determine if it is the mouse or the keyboard data that is being requested */
140 if (!(USB_ControlRequest.wIndex))
141 {
142 ReportData = (uint8_t*)&KeyboardReportData;
143 ReportSize = sizeof(KeyboardReportData);
144 }
145 else
146 {
147 ReportData = (uint8_t*)&MouseReportData;
148 ReportSize = sizeof(MouseReportData);
149 }
150
151 /* Write the report data to the control endpoint */
152 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
153
154 /* Clear the report data afterwards */
155 memset(ReportData, 0, ReportSize);
156
157 /* Finalize the stream transfer to send the last packet or clear the host abort */
158 Endpoint_ClearOUT();
159 }
160
161 break;
162 case REQ_SetReport:
163 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
164 {
165 Endpoint_ClearSETUP();
166
167 /* Wait until the LED report has been sent by the host */
168 while (!(Endpoint_IsOUTReceived()));
169
170 /* Read in the LED report from the host */
171 uint8_t LEDStatus = Endpoint_Read_Byte();
172 uint8_t LEDMask = LEDS_LED2;
173
174 if (LEDStatus & 0x01) // NUM Lock
175 LEDMask |= LEDS_LED1;
176
177 if (LEDStatus & 0x02) // CAPS Lock
178 LEDMask |= LEDS_LED3;
179
180 if (LEDStatus & 0x04) // SCROLL Lock
181 LEDMask |= LEDS_LED4;
182
183 /* Set the status LEDs to the current HID LED status */
184 LEDs_SetAllLEDs(LEDMask);
185
186 /* Clear the endpoint data */
187 Endpoint_ClearOUT();
188
189 /* Acknowledge status stage */
190 while (!(Endpoint_IsINReady()));
191 Endpoint_ClearIN();
192 }
193
194 break;
195 }
196 }
197
198 /** Keyboard task. This generates the next keyboard HID report for the host, and transmits it via the
199 * keyboard IN endpoint when the host is ready for more data. Additionally, it processes host LED status
200 * reports sent to the device via the keyboard OUT reporting endpoint.
201 */
202 void Keyboard_HID_Task(void)
203 {
204 uint8_t JoyStatus_LCL = Joystick_GetStatus();
205
206 /* Check if board button is not pressed, if so mouse mode enabled */
207 if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
208 {
209 if (JoyStatus_LCL & JOY_UP)
210 KeyboardReportData.KeyCode[0] = 0x04; // A
211 else if (JoyStatus_LCL & JOY_DOWN)
212 KeyboardReportData.KeyCode[0] = 0x05; // B
213
214 if (JoyStatus_LCL & JOY_LEFT)
215 KeyboardReportData.KeyCode[0] = 0x06; // C
216 else if (JoyStatus_LCL & JOY_RIGHT)
217 KeyboardReportData.KeyCode[0] = 0x07; // D
218
219 if (JoyStatus_LCL & JOY_PRESS)
220 KeyboardReportData.KeyCode[0] = 0x08; // E
221 }
222
223 /* Check if the USB system is connected to a host and report protocol mode is enabled */
224 if (USB_IsConnected)
225 {
226 /* Select the Keyboard Report Endpoint */
227 Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
228
229 /* Check if Keyboard Endpoint Ready for Read/Write */
230 if (Endpoint_IsReadWriteAllowed())
231 {
232 /* Write Keyboard Report Data */
233 Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
234
235 /* Finalize the stream transfer to send the last packet */
236 Endpoint_ClearIN();
237
238 /* Clear the report data afterwards */
239 memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));
240 }
241
242 /* Select the Keyboard LED Report Endpoint */
243 Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);
244
245 /* Check if Keyboard LED Endpoint Ready for Read/Write */
246 if (Endpoint_IsReadWriteAllowed())
247 {
248 /* Read in the LED report from the host */
249 uint8_t LEDStatus = Endpoint_Read_Byte();
250 uint8_t LEDMask = LEDS_LED2;
251
252 if (LEDStatus & 0x01) // NUM Lock
253 LEDMask |= LEDS_LED1;
254
255 if (LEDStatus & 0x02) // CAPS Lock
256 LEDMask |= LEDS_LED3;
257
258 if (LEDStatus & 0x04) // SCROLL Lock
259 LEDMask |= LEDS_LED4;
260
261 /* Set the status LEDs to the current Keyboard LED status */
262 LEDs_SetAllLEDs(LEDMask);
263
264 /* Handshake the OUT Endpoint - clear endpoint and ready for next report */
265 Endpoint_ClearOUT();
266 }
267 }
268 }
269
270 /** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
271 * mouse IN endpoint when the host is ready for more data.
272 */
273 void Mouse_HID_Task(void)
274 {
275 uint8_t JoyStatus_LCL = Joystick_GetStatus();
276
277 /* Check if board button is pressed, if so mouse mode enabled */
278 if (Buttons_GetStatus() & BUTTONS_BUTTON1)
279 {
280 if (JoyStatus_LCL & JOY_UP)
281 MouseReportData.Y = 1;
282 else if (JoyStatus_LCL & JOY_DOWN)
283 MouseReportData.Y = -1;
284
285 if (JoyStatus_LCL & JOY_RIGHT)
286 MouseReportData.X = 1;
287 else if (JoyStatus_LCL & JOY_LEFT)
288 MouseReportData.X = -1;
289
290 if (JoyStatus_LCL & JOY_PRESS)
291 MouseReportData.Button = (1 << 0);
292 }
293
294 /* Check if the USB system is connected to a host and report protocol mode is enabled */
295 if (USB_IsConnected)
296 {
297 /* Select the Mouse Report Endpoint */
298 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
299
300 /* Check if Mouse Endpoint Ready for Read/Write */
301 if (Endpoint_IsReadWriteAllowed())
302 {
303 /* Write Mouse Report Data */
304 Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));
305
306 /* Finalize the stream transfer to send the last packet */
307 Endpoint_ClearIN();
308
309 /* Clear the report data afterwards */
310 memset(&MouseReportData, 0, sizeof(MouseReportData));
311 }
312 }
313 }