]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2012 Jun Wako <wakojun@gmail.com> | |
3 | * This file is based on LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse. | |
4 | */ | |
5 | ||
6 | /* | |
7 | LUFA Library | |
8 | Copyright (C) Dean Camera, 2012. | |
9 | ||
10 | dean [at] fourwalledcubicle [dot] com | |
11 | www.lufa-lib.org | |
12 | */ | |
13 | ||
14 | /* | |
15 | Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com) | |
16 | Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com) | |
17 | ||
18 | Permission to use, copy, modify, distribute, and sell this | |
19 | software and its documentation for any purpose is hereby granted | |
20 | without fee, provided that the above copyright notice appear in | |
21 | all copies and that both that the copyright notice and this | |
22 | permission notice and warranty disclaimer appear in supporting | |
23 | documentation, and that the name of the author not be used in | |
24 | advertising or publicity pertaining to distribution of the | |
25 | software without specific, written prior permission. | |
26 | ||
27 | The author disclaim all warranties with regard to this | |
28 | software, including all implied warranties of merchantability | |
29 | and fitness. In no event shall the author be liable for any | |
30 | special, indirect or consequential damages or any damages | |
31 | whatsoever resulting from loss of use, data or profits, whether | |
32 | in an action of contract, negligence or other tortious action, | |
33 | arising out of or in connection with the use or performance of | |
34 | this software. | |
35 | */ | |
36 | ||
37 | #include "report.h" | |
38 | #include "host.h" | |
39 | #include "host_driver.h" | |
40 | #include "keyboard.h" | |
41 | #include "lufa.h" | |
42 | ||
43 | static uint8_t keyboard_led_stats = 0; | |
44 | report_keyboard_t keyboard_report_sent; | |
45 | report_mouse_t mouse_report_sent; | |
46 | ||
47 | /* Host driver */ | |
48 | static uint8_t keyboard_leds(void); | |
49 | static void send_keyboard(report_keyboard_t *report); | |
50 | static void send_mouse(report_mouse_t *report); | |
51 | static void send_system(uint16_t data); | |
52 | static void send_consumer(uint16_t data); | |
53 | static host_driver_t lufa_driver = { | |
54 | keyboard_leds, | |
55 | send_keyboard, | |
56 | send_mouse, | |
57 | send_system, | |
58 | send_consumer | |
59 | }; | |
60 | ||
61 | ||
62 | int main(void) | |
63 | { | |
64 | SetupHardware(); | |
65 | sei(); | |
66 | ||
67 | keyboard_init(); | |
68 | host_set_driver(&lufa_driver); | |
69 | while (1) { | |
70 | keyboard_proc(); | |
71 | Keyboard_HID_Task(); | |
72 | USB_USBTask(); | |
73 | } | |
74 | } | |
75 | ||
76 | /** Configures the board hardware and chip peripherals for the demo's functionality. */ | |
77 | void SetupHardware(void) | |
78 | { | |
79 | /* Disable watchdog if enabled by bootloader/fuses */ | |
80 | MCUSR &= ~(1 << WDRF); | |
81 | wdt_disable(); | |
82 | ||
83 | /* Disable clock division */ | |
84 | clock_prescale_set(clock_div_1); | |
85 | ||
86 | USB_Init(); | |
87 | } | |
88 | ||
89 | /** Event handler for the USB_Connect event. */ | |
90 | void EVENT_USB_Device_Connect(void) | |
91 | { | |
92 | } | |
93 | ||
94 | /** Event handler for the USB_Disconnect event. */ | |
95 | void EVENT_USB_Device_Disconnect(void) | |
96 | { | |
97 | } | |
98 | ||
99 | /** Event handler for the USB_ConfigurationChanged event. | |
100 | * This is fired when the host sets the current configuration of the USB device after enumeration. | |
101 | */ | |
102 | void EVENT_USB_Device_ConfigurationChanged(void) | |
103 | { | |
104 | bool ConfigSuccess = true; | |
105 | ||
106 | /* Setup Keyboard HID Report Endpoints */ | |
107 | ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | |
108 | HID_EPSIZE, ENDPOINT_BANK_SINGLE); | |
109 | ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, | |
110 | HID_EPSIZE, ENDPOINT_BANK_SINGLE); | |
111 | ||
112 | /* Setup Mouse HID Report Endpoint */ | |
113 | ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | |
114 | HID_EPSIZE, ENDPOINT_BANK_SINGLE); | |
115 | } | |
116 | ||
117 | /** Event handler for the USB_ControlRequest event. | |
118 | * This is fired before passing along unhandled control requests to the library for processing internally. | |
119 | */ | |
120 | void EVENT_USB_Device_ControlRequest(void) | |
121 | { | |
122 | uint8_t* ReportData; | |
123 | uint8_t ReportSize; | |
124 | ||
125 | /* Handle HID Class specific requests */ | |
126 | switch (USB_ControlRequest.bRequest) | |
127 | { | |
128 | case HID_REQ_GetReport: | |
129 | if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) | |
130 | { | |
131 | Endpoint_ClearSETUP(); | |
132 | ||
133 | /* Determine if it is the mouse or the keyboard data that is being requested */ | |
134 | if (!(USB_ControlRequest.wIndex)) | |
135 | { | |
136 | ReportData = (uint8_t*)&keyboard_report_sent; | |
137 | ReportSize = sizeof(keyboard_report_sent); | |
138 | } | |
139 | else | |
140 | { | |
141 | ReportData = (uint8_t*)&mouse_report_sent; | |
142 | ReportSize = sizeof(mouse_report_sent); | |
143 | } | |
144 | ||
145 | /* Write the report data to the control endpoint */ | |
146 | Endpoint_Write_Control_Stream_LE(ReportData, ReportSize); | |
147 | Endpoint_ClearOUT(); | |
148 | } | |
149 | ||
150 | break; | |
151 | case HID_REQ_SetReport: | |
152 | if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) | |
153 | { | |
154 | Endpoint_ClearSETUP(); | |
155 | ||
156 | /* Wait until the LED report has been sent by the host */ | |
157 | while (!(Endpoint_IsOUTReceived())) | |
158 | { | |
159 | if (USB_DeviceState == DEVICE_STATE_Unattached) | |
160 | return; | |
161 | } | |
162 | ||
163 | /* Read in the LED report from the host */ | |
164 | keyboard_led_stats = Endpoint_Read_8(); | |
165 | ||
166 | Endpoint_ClearOUT(); | |
167 | Endpoint_ClearStatusStage(); | |
168 | } | |
169 | ||
170 | break; | |
171 | } | |
172 | } | |
173 | ||
174 | /** Keyboard task. | |
175 | * This processes host LED status reports sent to the device via the keyboard OUT reporting endpoint. | |
176 | */ | |
177 | void Keyboard_HID_Task(void) | |
178 | { | |
179 | /* Select the Keyboard LED Report Endpoint */ | |
180 | Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM); | |
181 | ||
182 | /* Check if Keyboard LED Endpoint Ready for Read/Write */ | |
183 | if (Endpoint_IsReadWriteAllowed()) | |
184 | { | |
185 | /* Read in the LED report from the host */ | |
186 | keyboard_led_stats = Endpoint_Read_8(); | |
187 | ||
188 | /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ | |
189 | Endpoint_ClearOUT(); | |
190 | } | |
191 | } | |
192 | ||
193 | ||
194 | /******************************************************************************* | |
195 | * Host driver | |
196 | ******************************************************************************/ | |
197 | static uint8_t keyboard_leds(void) | |
198 | { | |
199 | return keyboard_led_stats; | |
200 | } | |
201 | ||
202 | static void send_keyboard(report_keyboard_t *report) | |
203 | { | |
204 | // TODO: handle NKRO report | |
205 | /* Select the Keyboard Report Endpoint */ | |
206 | Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM); | |
207 | ||
208 | /* Check if Keyboard Endpoint Ready for Read/Write */ | |
209 | if (Endpoint_IsReadWriteAllowed()) | |
210 | { | |
211 | /* Write Keyboard Report Data */ | |
212 | Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL); | |
213 | ||
214 | /* Finalize the stream transfer to send the last packet */ | |
215 | Endpoint_ClearIN(); | |
216 | } | |
217 | keyboard_report_sent = *report; | |
218 | } | |
219 | ||
220 | static void send_mouse(report_mouse_t *report) | |
221 | { | |
222 | /* Select the Mouse Report Endpoint */ | |
223 | Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); | |
224 | ||
225 | /* Check if Mouse Endpoint Ready for Read/Write */ | |
226 | if (Endpoint_IsReadWriteAllowed()) | |
227 | { | |
228 | /* Write Mouse Report Data */ | |
229 | Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL); | |
230 | ||
231 | /* Finalize the stream transfer to send the last packet */ | |
232 | Endpoint_ClearIN(); | |
233 | } | |
234 | mouse_report_sent = *report; | |
235 | } | |
236 | ||
237 | static void send_system(uint16_t data) | |
238 | { | |
239 | } | |
240 | ||
241 | static void send_consumer(uint16_t data) | |
242 | { | |
243 | } |