Update copyright year to 2013.
[pub/USBasp.git] / Demos / Device / ClassDriver / VirtualSerialMouse / VirtualSerialMouse.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 disclaims 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 VirtualSerialMouse demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "VirtualSerialMouse.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 .DataINEndpoint =
49 {
50 .Address = CDC_TX_EPADDR,
51 .Size = CDC_TXRX_EPSIZE,
52 .Banks = 1,
53 },
54 .DataOUTEndpoint =
55 {
56 .Address = CDC_RX_EPADDR,
57 .Size = CDC_TXRX_EPSIZE,
58 .Banks = 1,
59 },
60 .NotificationEndpoint =
61 {
62 .Address = CDC_NOTIFICATION_EPADDR,
63 .Size = CDC_NOTIFICATION_EPSIZE,
64 .Banks = 1,
65 },
66 },
67 };
68
69 /** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
70 static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_MouseReport_Data_t)];
71
72 /** LUFA HID Class driver interface configuration and state information. This structure is
73 * passed to all HID Class driver functions, so that multiple instances of the same class
74 * within a device can be differentiated from one another.
75 */
76 USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
77 {
78 .Config =
79 {
80 .InterfaceNumber = 2,
81 .ReportINEndpoint =
82 {
83 .Address = MOUSE_EPADDR,
84 .Size = MOUSE_EPSIZE,
85 .Banks = 1,
86 },
87 .PrevReportINBuffer = PrevMouseHIDReportBuffer,
88 .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
89 },
90 };
91
92
93 /** Main program entry point. This routine contains the overall program flow, including initial
94 * setup of all components and the main program loop.
95 */
96 int main(void)
97 {
98 SetupHardware();
99
100 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
101 GlobalInterruptEnable();
102
103 for (;;)
104 {
105 CheckJoystickMovement();
106
107 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
108 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
109
110 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
111 HID_Device_USBTask(&Mouse_HID_Interface);
112 USB_USBTask();
113 }
114 }
115
116 /** Configures the board hardware and chip peripherals for the demo's functionality. */
117 void SetupHardware(void)
118 {
119 /* Disable watchdog if enabled by bootloader/fuses */
120 MCUSR &= ~(1 << WDRF);
121 wdt_disable();
122
123 /* Disable clock division */
124 clock_prescale_set(clock_div_1);
125
126 /* Hardware Initialization */
127 Joystick_Init();
128 LEDs_Init();
129 USB_Init();
130 }
131
132 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
133 void CheckJoystickMovement(void)
134 {
135 uint8_t JoyStatus_LCL = Joystick_GetStatus();
136 char* ReportString = NULL;
137 static bool ActionSent = false;
138
139 if (JoyStatus_LCL & JOY_UP)
140 ReportString = "Joystick Up\r\n";
141 else if (JoyStatus_LCL & JOY_DOWN)
142 ReportString = "Joystick Down\r\n";
143 else if (JoyStatus_LCL & JOY_LEFT)
144 ReportString = "Joystick Left\r\n";
145 else if (JoyStatus_LCL & JOY_RIGHT)
146 ReportString = "Joystick Right\r\n";
147 else if (JoyStatus_LCL & JOY_PRESS)
148 ReportString = "Joystick Pressed\r\n";
149 else
150 ActionSent = false;
151
152 if ((ReportString != NULL) && (ActionSent == false))
153 {
154 ActionSent = true;
155
156 CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
157 }
158 }
159
160 /** Event handler for the library USB Connection event. */
161 void EVENT_USB_Device_Connect(void)
162 {
163 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
164 }
165
166 /** Event handler for the library USB Disconnection event. */
167 void EVENT_USB_Device_Disconnect(void)
168 {
169 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
170 }
171
172 /** Event handler for the library USB Configuration Changed event. */
173 void EVENT_USB_Device_ConfigurationChanged(void)
174 {
175 bool ConfigSuccess = true;
176
177 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
178 ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
179
180 USB_Device_EnableSOFEvents();
181
182 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
183 }
184
185 /** Event handler for the library USB Control Request reception event. */
186 void EVENT_USB_Device_ControlRequest(void)
187 {
188 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
189 HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
190 }
191
192 /** Event handler for the USB device Start Of Frame event. */
193 void EVENT_USB_Device_StartOfFrame(void)
194 {
195 HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
196 }
197
198 /** HID class driver callback function for the creation of HID reports to the host.
199 *
200 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
201 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
202 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
203 * \param[out] ReportData Pointer to a buffer where the created report should be stored
204 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
205 *
206 * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
207 */
208 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
209 uint8_t* const ReportID,
210 const uint8_t ReportType,
211 void* ReportData,
212 uint16_t* const ReportSize)
213 {
214 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
215
216 uint8_t JoyStatus_LCL = Joystick_GetStatus();
217 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
218
219 if (JoyStatus_LCL & JOY_UP)
220 MouseReport->Y = -1;
221 else if (JoyStatus_LCL & JOY_DOWN)
222 MouseReport->Y = 1;
223
224 if (JoyStatus_LCL & JOY_LEFT)
225 MouseReport->X = -1;
226 else if (JoyStatus_LCL & JOY_RIGHT)
227 MouseReport->X = 1;
228
229 if (JoyStatus_LCL & JOY_PRESS)
230 MouseReport->Button |= (1 << 0);
231
232 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
233 MouseReport->Button |= (1 << 1);
234
235 *ReportSize = sizeof(USB_MouseReport_Data_t);
236 return true;
237 }
238
239 /** HID class driver callback function for the processing of HID reports from the host.
240 *
241 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
242 * \param[in] ReportID Report ID of the received report from the host
243 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
244 * \param[in] ReportData Pointer to a buffer where the received report has been stored
245 * \param[in] ReportSize Size in bytes of the received HID report
246 */
247 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
248 const uint8_t ReportID,
249 const uint8_t ReportType,
250 const void* ReportData,
251 const uint16_t ReportSize)
252 {
253 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
254 }
255