Add new tag for the LUFA-120219-BETA release.
[pub/USBasp.git] / Demos / Device / ClassDriver / VirtualSerialMouse / VirtualSerialMouse.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 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 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
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 static 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 = 2,
75
76 .ReportINEndpointNumber = MOUSE_EPNUM,
77 .ReportINEndpointSize = MOUSE_EPSIZE,
78 .ReportINEndpointDoubleBank = false,
79
80 .PrevReportINBuffer = PrevMouseHIDReportBuffer,
81 .PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
82 },
83 };
84
85
86 /** Main program entry point. This routine contains the overall program flow, including initial
87 * setup of all components and the main program loop.
88 */
89 int main(void)
90 {
91 SetupHardware();
92
93 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
94 sei();
95
96 for (;;)
97 {
98 CheckJoystickMovement();
99
100 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
101 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
102
103 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
104 HID_Device_USBTask(&Mouse_HID_Interface);
105 USB_USBTask();
106 }
107 }
108
109 /** Configures the board hardware and chip peripherals for the demo's functionality. */
110 void SetupHardware(void)
111 {
112 /* Disable watchdog if enabled by bootloader/fuses */
113 MCUSR &= ~(1 << WDRF);
114 wdt_disable();
115
116 /* Disable clock division */
117 clock_prescale_set(clock_div_1);
118
119 /* Hardware Initialization */
120 Joystick_Init();
121 LEDs_Init();
122 USB_Init();
123 }
124
125 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
126 void CheckJoystickMovement(void)
127 {
128 uint8_t JoyStatus_LCL = Joystick_GetStatus();
129 char* ReportString = NULL;
130 static bool ActionSent = false;
131
132 if (JoyStatus_LCL & JOY_UP)
133 ReportString = "Joystick Up\r\n";
134 else if (JoyStatus_LCL & JOY_DOWN)
135 ReportString = "Joystick Down\r\n";
136 else if (JoyStatus_LCL & JOY_LEFT)
137 ReportString = "Joystick Left\r\n";
138 else if (JoyStatus_LCL & JOY_RIGHT)
139 ReportString = "Joystick Right\r\n";
140 else if (JoyStatus_LCL & JOY_PRESS)
141 ReportString = "Joystick Pressed\r\n";
142 else
143 ActionSent = false;
144
145 if ((ReportString != NULL) && (ActionSent == false))
146 {
147 ActionSent = true;
148
149 CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
150 }
151 }
152
153 /** Event handler for the library USB Connection event. */
154 void EVENT_USB_Device_Connect(void)
155 {
156 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
157 }
158
159 /** Event handler for the library USB Disconnection event. */
160 void EVENT_USB_Device_Disconnect(void)
161 {
162 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
163 }
164
165 /** Event handler for the library USB Configuration Changed event. */
166 void EVENT_USB_Device_ConfigurationChanged(void)
167 {
168 bool ConfigSuccess = true;
169
170 ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
171 ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
172
173 USB_Device_EnableSOFEvents();
174
175 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
176 }
177
178 /** Event handler for the library USB Control Request reception event. */
179 void EVENT_USB_Device_ControlRequest(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[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
196 * \param[out] ReportData Pointer to a buffer where the created report should be stored
197 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
198 *
199 * \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent
200 */
201 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
202 uint8_t* const ReportID,
203 const uint8_t ReportType,
204 void* ReportData,
205 uint16_t* const ReportSize)
206 {
207 USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
208
209 uint8_t JoyStatus_LCL = Joystick_GetStatus();
210 uint8_t ButtonStatus_LCL = Buttons_GetStatus();
211
212 if (JoyStatus_LCL & JOY_UP)
213 MouseReport->Y = -1;
214 else if (JoyStatus_LCL & JOY_DOWN)
215 MouseReport->Y = 1;
216
217 if (JoyStatus_LCL & JOY_LEFT)
218 MouseReport->X = -1;
219 else if (JoyStatus_LCL & JOY_RIGHT)
220 MouseReport->X = 1;
221
222 if (JoyStatus_LCL & JOY_PRESS)
223 MouseReport->Button |= (1 << 0);
224
225 if (ButtonStatus_LCL & BUTTONS_BUTTON1)
226 MouseReport->Button |= (1 << 1);
227
228 *ReportSize = sizeof(USB_MouseReport_Data_t);
229 return true;
230 }
231
232 /** HID class driver callback function for the processing of HID reports from the host.
233 *
234 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
235 * \param[in] ReportID Report ID of the received report from the host
236 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
237 * \param[in] ReportData Pointer to a buffer where the received report has been stored
238 * \param[in] ReportSize Size in bytes of the received HID report
239 */
240 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
241 const uint8_t ReportID,
242 const uint8_t ReportType,
243 const void* ReportData,
244 const uint16_t ReportSize)
245 {
246 // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
247 }
248