Cleanups to the MassStorage Device demos, and the MassStorage Device Class driver.
[pub/USBasp.git] / Demos / Device / ClassDriver / CDCMouse / CDCMouse.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 CDCMouse demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "CDCMouse.h"
38
39 /** LUFA CDC Class driver interface configuration and state information. This structure is
40 * passed to all CDC Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
42 */
43 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
44 {
45 .Config =
46 {
47 .ControlInterfaceNumber = 0,
48
49 .DataINEndpointNumber = CDC_TX_EPNUM,
50 .DataINEndpointSize = CDC_TXRX_EPSIZE,
51 .DataINEndpointDoubleBank = false,
52
53 .DataOUTEndpointNumber = CDC_RX_EPNUM,
54 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
55 .DataOUTEndpointDoubleBank = false,
56
57 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
58 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
59 .NotificationEndpointDoubleBank = false,
60 },
61 };
62
63 /** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
64 uint8_t PrevMouseHIDReportBuffer[sizeof(USB_MouseReport_Data_t)];
65
66 /** LUFA HID Class driver interface configuration and state information. This structure is
67 * passed to all HID Class driver functions, so that multiple instances of the same class
68 * within a device can be differentiated from one another.
69 */
70 USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
71 {
72 .Config =
73 {
74 .InterfaceNumber = 0,
75
76 .ReportINEndpointNumber = MOUSE_EPNUM,
77 .ReportINEndpointSize = MOUSE_EPSIZE,
78 .ReportINEndpointDoubleBank = false,
79
80 .PrevReportINBuffer = PrevMouseHIDReportBuffer,
81 .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
82 },
83 };
84
85 /** Main program entry point. This routine contains the overall program flow, including initial
86 * setup of all components and the main program loop.
87 */
88 int main(void)
89 {
90 SetupHardware();
91
92 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
93
94 for (;;)
95 {
96 CheckJoystickMovement();
97
98 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
99 while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
100 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
101
102 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
103 HID_Device_USBTask(&Mouse_HID_Interface);
104 USB_USBTask();
105 }
106 }
107
108 /** Configures the board hardware and chip peripherals for the demo's functionality. */
109 void SetupHardware(void)
110 {
111 /* Disable watchdog if enabled by bootloader/fuses */
112 MCUSR &= ~(1 << WDRF);
113 wdt_disable();
114
115 /* Disable clock division */
116 clock_prescale_set(clock_div_1);
117
118 /* Hardware Initialization */
119 Joystick_Init();
120 LEDs_Init();
121 USB_Init();
122 }
123
124 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
125 void CheckJoystickMovement(void)
126 {
127 uint8_t JoyStatus_LCL = Joystick_GetStatus();
128 char* ReportString = NULL;
129 static bool ActionSent = false;
130
131 if (JoyStatus_LCL & JOY_UP)
132 ReportString = "Joystick Up\r\n";
133 else if (JoyStatus_LCL & JOY_DOWN)
134 ReportString = "Joystick Down\r\n";
135 else if (JoyStatus_LCL & JOY_LEFT)
136 ReportString = "Joystick Left\r\n";
137 else if (JoyStatus_LCL & JOY_RIGHT)
138 ReportString = "Joystick Right\r\n";
139 else if (JoyStatus_LCL & JOY_PRESS)
140 ReportString = "Joystick Pressed\r\n";
141 else
142 ActionSent = false;
143
144 if ((ReportString != NULL) && (ActionSent == false))
145 {
146 ActionSent = true;
147
148 CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString));
149 }
150 }
151
152 /** Event handler for the library USB Connection event. */
153 void EVENT_USB_Device_Connect(void)
154 {
155 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
156 }
157
158 /** Event handler for the library USB Disconnection event. */
159 void EVENT_USB_Device_Disconnect(void)
160 {
161 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
162 }
163
164 /** Event handler for the library USB Configuration Changed event. */
165 void EVENT_USB_Device_ConfigurationChanged(void)
166 {
167 LEDs_SetAllLEDs(LEDMASK_USB_READY);
168
169 if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))
170 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
171
172 if (!(HID_Device_ConfigureEndpoints(&Mouse_HID_Interface)))
173 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
174
175 USB_Device_EnableSOFEvents();
176 }
177
178 /** Event handler for the library USB Unhandled Control Request event. */
179 void EVENT_USB_Device_UnhandledControlRequest(void)
180 {
181 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
182 HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
183 }
184
185 /** Event handler for the USB device Start Of Frame event. */
186 void EVENT_USB_Device_StartOfFrame(void)
187 {
188 HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
189 }
190
191 /** HID class driver callback function for the creation of HID reports to the host.
192 *
193 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
194 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
195 * \param[out] ReportData Pointer to a buffer where the created report should be stored
196 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent
197 *
198 * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
199 */
200 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID,
201 void* ReportData, uint16_t* ReportSize)
202 {
203 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
204
205 uint8_t JoyStatus_LCL = Joystick_GetStatus();
206 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
207
208 if (JoyStatus_LCL & JOY_UP)
209 MouseReport->Y = -1;
210 else if (JoyStatus_LCL & JOY_DOWN)
211 MouseReport->Y = 1;
212
213 if (JoyStatus_LCL & JOY_LEFT)
214 MouseReport->X = -1;
215 else if (JoyStatus_LCL & JOY_RIGHT)
216 MouseReport->X = 1;
217
218 if (JoyStatus_LCL & JOY_PRESS)
219 MouseReport->Button = (1 << 0);
220
221 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
222 MouseReport->Button |= (1 << 1);
223
224 *ReportSize = sizeof(USB_MouseReport_Data_t);
225 return true;
226 }
227
228 /** HID class driver callback function for the processing of HID reports from the host.
229 *
230 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
231 * \param[in] ReportID Report ID of the received report from the host
232 * \param[in] ReportData Pointer to a buffer where the created report has been stored
233 * \param[in] ReportSize Size in bytes of the received HID report
234 */
235 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, const uint8_t ReportID,
236 const void* ReportData, const uint16_t ReportSize)
237 {
238 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
239 }