]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Demos/Device/ClassDriver/VirtualSerialMouse/VirtualSerialMouse.c
Merge pull request #1601 from dlaroe/master
[qmk_firmware.git] / lib / lufa / Demos / Device / ClassDriver / VirtualSerialMouse / VirtualSerialMouse.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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         = INTERFACE_ID_CDC_CCI,
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                = INTERFACE_ID_Mouse,
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 #if (ARCH == ARCH_AVR8)
120         /* Disable watchdog if enabled by bootloader/fuses */
121         MCUSR &= ~(1 << WDRF);
122         wdt_disable();
123
124         /* Disable clock division */
125         clock_prescale_set(clock_div_1);
126 #elif (ARCH == ARCH_XMEGA)
127         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
128         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
129         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
130
131         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
132         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
133         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
134
135         PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
136 #endif
137
138         /* Hardware Initialization */
139         Joystick_Init();
140         LEDs_Init();
141         USB_Init();
142 }
143
144 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
145 void CheckJoystickMovement(void)
146 {
147         uint8_t     JoyStatus_LCL = Joystick_GetStatus();
148         char*       ReportString  = NULL;
149         static bool ActionSent    = false;
150
151         if (JoyStatus_LCL & JOY_UP)
152           ReportString = "Joystick Up\r\n";
153         else if (JoyStatus_LCL & JOY_DOWN)
154           ReportString = "Joystick Down\r\n";
155         else if (JoyStatus_LCL & JOY_LEFT)
156           ReportString = "Joystick Left\r\n";
157         else if (JoyStatus_LCL & JOY_RIGHT)
158           ReportString = "Joystick Right\r\n";
159         else if (JoyStatus_LCL & JOY_PRESS)
160           ReportString = "Joystick Pressed\r\n";
161         else
162           ActionSent = false;
163
164         if ((ReportString != NULL) && (ActionSent == false))
165         {
166                 ActionSent = true;
167
168                 CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
169         }
170 }
171
172 /** Event handler for the library USB Connection event. */
173 void EVENT_USB_Device_Connect(void)
174 {
175         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
176 }
177
178 /** Event handler for the library USB Disconnection event. */
179 void EVENT_USB_Device_Disconnect(void)
180 {
181         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
182 }
183
184 /** Event handler for the library USB Configuration Changed event. */
185 void EVENT_USB_Device_ConfigurationChanged(void)
186 {
187         bool ConfigSuccess = true;
188
189         ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
190         ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
191
192         USB_Device_EnableSOFEvents();
193
194         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
195 }
196
197 /** Event handler for the library USB Control Request reception event. */
198 void EVENT_USB_Device_ControlRequest(void)
199 {
200         CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
201         HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
202 }
203
204 /** Event handler for the USB device Start Of Frame event. */
205 void EVENT_USB_Device_StartOfFrame(void)
206 {
207         HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
208 }
209
210 /** HID class driver callback function for the creation of HID reports to the host.
211  *
212  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
213  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
214  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
215  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored
216  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent)
217  *
218  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
219  */
220 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
221                                          uint8_t* const ReportID,
222                                          const uint8_t ReportType,
223                                          void* ReportData,
224                                          uint16_t* const ReportSize)
225 {
226         USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
227
228         uint8_t JoyStatus_LCL    = Joystick_GetStatus();
229         uint8_t ButtonStatus_LCL = Buttons_GetStatus();
230
231         if (JoyStatus_LCL & JOY_UP)
232           MouseReport->Y = -1;
233         else if (JoyStatus_LCL & JOY_DOWN)
234           MouseReport->Y =  1;
235
236         if (JoyStatus_LCL & JOY_LEFT)
237           MouseReport->X = -1;
238         else if (JoyStatus_LCL & JOY_RIGHT)
239           MouseReport->X =  1;
240
241         if (JoyStatus_LCL & JOY_PRESS)
242           MouseReport->Button |= (1 << 0);
243
244         if (ButtonStatus_LCL & BUTTONS_BUTTON1)
245           MouseReport->Button |= (1 << 1);
246
247         *ReportSize = sizeof(USB_MouseReport_Data_t);
248         return true;
249 }
250
251 /** HID class driver callback function for the processing of HID reports from the host.
252  *
253  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
254  *  \param[in] ReportID    Report ID of the received report from the host
255  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
256  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored
257  *  \param[in] ReportSize  Size in bytes of the received HID report
258  */
259 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
260                                           const uint8_t ReportID,
261                                           const uint8_t ReportType,
262                                           const void* ReportData,
263                                           const uint16_t ReportSize)
264 {
265         // Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
266 }
267
268 /** CDC class driver callback function the processing of changes to the virtual
269  *  control lines sent from the host..
270  *
271  *  \param[in] CDCInterfaceInfo  Pointer to the CDC class interface configuration structure being referenced
272  */
273 void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo)
274 {
275         /* You can get changes to the virtual CDC lines in this callback; a common
276            use-case is to use the Data Terminal Ready (DTR) flag to enable and
277            disable CDC communications in your application when set to avoid the
278            application blocking while waiting for a host to become ready and read
279            in the pending data from the USB endpoints.
280         */
281         bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0;
282 }