Incomplete non-functional BluetoothHost demo removed until it has reached a stable...
[pub/USBasp.git] / Demos / Mouse / Mouse.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 /** \file
32 *
33 * Main source file for the Mouse demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "Mouse.h"
38
39 /* Project Tags, for reading out using the ButtLoad project */
40 BUTTLOADTAG(ProjName, "LUFA Mouse App");
41 BUTTLOADTAG(BuildTime, __TIME__);
42 BUTTLOADTAG(BuildDate, __DATE__);
43 BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);
44
45 /* Scheduler Task List */
46 TASK_LIST
47 {
48 { Task: USB_USBTask , TaskStatus: TASK_STOP },
49 { Task: USB_Mouse_Report , TaskStatus: TASK_STOP },
50 };
51
52 /* Global Variables */
53 /** Indicates what report mode the host has requested, true for normal HID reporting mode, false for special boot
54 * protocol reporting mode.
55 */
56 bool UsingReportProtocol = true;
57
58 /** Current Idle period. This is set by the host via a Set Idle HID class request to silence the device's reports
59 * for either the entire idle duration, or until the report status changes (e.g. the user moves the mouse).
60 */
61 uint8_t IdleCount = 0;
62
63 /** Current Idle period remaining. When the IdleCount value is set, this tracks the remaining number of idle
64 * milliseconds. This is seperate to the IdleCount timer and is incremented and compared as the host may request
65 * the current idle period via a Get Idle HID class request, thus its value must be preserved.
66 */
67 uint16_t IdleMSRemaining = 0;
68
69
70 /** Main program entry point. This routine configures the hardware required by the application, then
71 * starts the scheduler to run the application tasks.
72 */
73 int main(void)
74 {
75 /* Disable watchdog if enabled by bootloader/fuses */
76 MCUSR &= ~(1 << WDRF);
77 wdt_disable();
78
79 /* Disable clock division */
80 clock_prescale_set(clock_div_1);
81
82 /* Hardware Initialization */
83 Joystick_Init();
84 LEDs_Init();
85 HWB_Init();
86
87 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
88 OCR0A = 0x7D;
89 TCCR0A = (1 << WGM01);
90 TCCR0B = ((1 << CS01) | (1 << CS00));
91 TIMSK0 = (1 << OCIE0A);
92
93 /* Indicate USB not ready */
94 UpdateStatus(Status_USBNotReady);
95
96 /* Initialize Scheduler so that it can be used */
97 Scheduler_Init();
98
99 /* Initialize USB Subsystem */
100 USB_Init();
101
102 /* Scheduling - routine never returns, so put this last in the main function */
103 Scheduler_Start();
104 }
105
106 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
107 * starts the library USB task to begin the enumeration and USB management process.
108 */
109 EVENT_HANDLER(USB_Connect)
110 {
111 /* Start USB management task */
112 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
113
114 /* Indicate USB enumerating */
115 UpdateStatus(Status_USBEnumerating);
116 }
117
118 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
119 * the status LEDs and stops the USB management and Mouse reporting tasks.
120 */
121 EVENT_HANDLER(USB_Disconnect)
122 {
123 /* Stop running mouse reporting and USB management tasks */
124 Scheduler_SetTaskMode(USB_Mouse_Report, TASK_STOP);
125 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
126
127 /* Indicate USB not ready */
128 UpdateStatus(Status_USBNotReady);
129 }
130
131 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
132 * of the USB device after enumeration - the device endpoints are configured and the mouse reporting task started.
133 */
134 EVENT_HANDLER(USB_ConfigurationChanged)
135 {
136 /* Setup Mouse Report Endpoint */
137 Endpoint_ConfigureEndpoint(MOUSE_EPNUM, EP_TYPE_INTERRUPT,
138 ENDPOINT_DIR_IN, MOUSE_EPSIZE,
139 ENDPOINT_BANK_SINGLE);
140
141 /* Indicate USB connected and ready */
142 UpdateStatus(Status_USBReady);
143
144 /* Default to report protocol on connect */
145 UsingReportProtocol = true;
146
147 /* Start running mouse reporting task */
148 Scheduler_SetTaskMode(USB_Mouse_Report, TASK_RUN);
149 }
150
151 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
152 * control requests that are not handled internally by the USB library (including the HID commands, which are
153 * all issued via the control endpoint), so that they can be handled appropriately for the application.
154 */
155 EVENT_HANDLER(USB_UnhandledControlPacket)
156 {
157 /* Handle HID Class specific requests */
158 switch (bRequest)
159 {
160 case REQ_GetReport:
161 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
162 {
163 USB_MouseReport_Data_t MouseReportData;
164
165 /* Create the next mouse report for transmission to the host */
166 GetNextReport(&MouseReportData);
167
168 /* Ignore report type and ID number value */
169 Endpoint_Discard_Word();
170
171 /* Ignore unused Interface number value */
172 Endpoint_Discard_Word();
173
174 /* Read in the number of bytes in the report to send to the host */
175 uint16_t wLength = Endpoint_Read_Word_LE();
176
177 /* If trying to send more bytes than exist to the host, clamp the value at the report size */
178 if (wLength > sizeof(MouseReportData))
179 wLength = sizeof(MouseReportData);
180
181 Endpoint_ClearSetupReceived();
182
183 /* Write the report data to the control endpoint */
184 Endpoint_Write_Control_Stream_LE(&MouseReportData, wLength);
185
186 /* Clear the report data afterwards */
187 memset(&MouseReportData, 0, sizeof(MouseReportData));
188
189 /* Finalize the stream transfer to send the last packet or clear the host abort */
190 Endpoint_ClearSetupOUT();
191 }
192
193 break;
194 case REQ_GetProtocol:
195 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
196 {
197 Endpoint_ClearSetupReceived();
198
199 /* Write the current protocol flag to the host */
200 Endpoint_Write_Byte(UsingReportProtocol);
201
202 /* Send the flag to the host */
203 Endpoint_ClearSetupIN();
204
205 /* Acknowledge status stage */
206 while (!(Endpoint_IsSetupOUTReceived()));
207 Endpoint_ClearSetupOUT();
208 }
209
210 break;
211 case REQ_SetProtocol:
212 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
213 {
214 /* Read in the wValue parameter containing the new protocol mode */
215 uint16_t wValue = Endpoint_Read_Word_LE();
216
217 Endpoint_ClearSetupReceived();
218
219 /* Set or clear the flag depending on what the host indicates that the current Protocol should be */
220 UsingReportProtocol = (wValue != 0x0000);
221
222 /* Acknowledge status stage */
223 while (!(Endpoint_IsSetupINReady()));
224 Endpoint_ClearSetupIN();
225 }
226
227 break;
228 case REQ_SetIdle:
229 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
230 {
231 /* Read in the wValue parameter containing the idle period */
232 uint16_t wValue = Endpoint_Read_Word_LE();
233
234 Endpoint_ClearSetupReceived();
235
236 /* Get idle period in MSB */
237 IdleCount = (wValue >> 8);
238
239 /* Acknowledge status stage */
240 while (!(Endpoint_IsSetupINReady()));
241 Endpoint_ClearSetupIN();
242 }
243
244 break;
245 case REQ_GetIdle:
246 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
247 {
248 Endpoint_ClearSetupReceived();
249
250 /* Write the current idle duration to the host */
251 Endpoint_Write_Byte(IdleCount);
252
253 /* Send the flag to the host */
254 Endpoint_ClearSetupIN();
255
256 /* Acknowledge status stage */
257 while (!(Endpoint_IsSetupOUTReceived()));
258 Endpoint_ClearSetupOUT();
259 }
260
261 break;
262 }
263 }
264
265 /** ISR for the timer 0 compare vector. This ISR fires once each millisecond, and increments the
266 * scheduler elapsed idle period counter when the host has set an idle period.
267 */
268 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
269 {
270 /* One millisecond has elapsed, decrement the idle time remaining counter if it has not already elapsed */
271 if (IdleMSRemaining)
272 IdleMSRemaining--;
273 }
274
275 /** Fills the given HID report data structure with the next HID report to send to the host.
276 *
277 * \param ReportData Pointer to a HID report data structure to be filled
278 *
279 * \return Boolean true if the new report differs from the last report, false otherwise
280 */
281 bool GetNextReport(USB_MouseReport_Data_t* ReportData)
282 {
283 static uint8_t PrevJoyStatus = 0;
284 static bool PrevHWBStatus = false;
285 uint8_t JoyStatus_LCL = Joystick_GetStatus();
286 bool InputChanged = false;
287
288 /* Clear the report contents */
289 memset(ReportData, 0, sizeof(USB_MouseReport_Data_t));
290
291 if (JoyStatus_LCL & JOY_UP)
292 ReportData->Y = -1;
293 else if (JoyStatus_LCL & JOY_DOWN)
294 ReportData->Y = 1;
295
296 if (JoyStatus_LCL & JOY_RIGHT)
297 ReportData->X = 1;
298 else if (JoyStatus_LCL & JOY_LEFT)
299 ReportData->X = -1;
300
301 if (JoyStatus_LCL & JOY_PRESS)
302 ReportData->Button = (1 << 0);
303
304 if (HWB_GetStatus())
305 ReportData->Button |= (1 << 1);
306
307 /* Check if the new report is different to the previous report */
308 InputChanged = ((uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(HWB_GetStatus() ^ PrevHWBStatus));
309
310 /* Save the current joystick and HWB status for later comparison */
311 PrevJoyStatus = JoyStatus_LCL;
312 PrevHWBStatus = HWB_GetStatus();
313
314 /* Return whether the new report is different to the previous report or not */
315 return InputChanged;
316 }
317
318 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
319 * log to a serial port, or anything else that is suitable for status updates.
320 *
321 * \param CurrentStatus Current status of the system, from the Mouse_StatusCodes_t enum
322 */
323 void UpdateStatus(uint8_t CurrentStatus)
324 {
325 uint8_t LEDMask = LEDS_NO_LEDS;
326
327 /* Set the LED mask to the appropriate LED mask based on the given status code */
328 switch (CurrentStatus)
329 {
330 case Status_USBNotReady:
331 LEDMask = (LEDS_LED1);
332 break;
333 case Status_USBEnumerating:
334 LEDMask = (LEDS_LED1 | LEDS_LED2);
335 break;
336 case Status_USBReady:
337 LEDMask = (LEDS_LED2 | LEDS_LED4);
338 break;
339 }
340
341 /* Set the board LEDs to the new LED mask */
342 LEDs_SetAllLEDs(LEDMask);
343 }
344
345 /** Task to manage HID report generation and transmission to the host, when in report mode. */
346 TASK(USB_Mouse_Report)
347 {
348 USB_MouseReport_Data_t MouseReportData;
349 bool SendReport = true;
350
351 /* Create the next mouse report for transmission to the host */
352 GetNextReport(&MouseReportData);
353
354 /* Check if the idle period is set*/
355 if (IdleCount)
356 {
357 /* Determine if the idle period has elapsed */
358 if (!(IdleMSRemaining))
359 {
360 /* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */
361 IdleMSRemaining = (IdleCount << 2);
362 }
363 else
364 {
365 /* Idle period not elapsed, indicate that a report must not be sent */
366 SendReport = false;
367 }
368 }
369
370 /* Check if the USB system is connected to a host */
371 if (USB_IsConnected)
372 {
373 /* Select the Mouse Report Endpoint */
374 Endpoint_SelectEndpoint(MOUSE_EPNUM);
375
376 /* Check if Mouse Endpoint Ready for Read/Write and if we should send a new report */
377 if (Endpoint_ReadWriteAllowed() && SendReport)
378 {
379 /* Write Mouse Report Data */
380 Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));
381
382 /* Finalize the stream transfer to send the last packet */
383 Endpoint_ClearCurrentBank();
384 }
385 }
386 }