Commit of new class abstraction APIs for all device demos other than the MIDI demo...
[pub/USBasp.git] / Demos / Device / DualCDC / DualCDC.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 #include "DualCDC.h"
32
33 USB_ClassInfo_CDC_t VirtualSerial1_CDC_Interface =
34 {
35 .ControlInterfaceNumber = 0,
36
37 .DataINEndpointNumber = CDC1_TX_EPNUM,
38 .DataINEndpointSize = CDC_TXRX_EPSIZE,
39
40 .DataOUTEndpointNumber = CDC1_RX_EPNUM,
41 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
42
43 .NotificationEndpointNumber = CDC1_NOTIFICATION_EPNUM,
44 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
45 };
46
47 USB_ClassInfo_CDC_t VirtualSerial2_CDC_Interface =
48 {
49 .ControlInterfaceNumber = 0,
50
51 .DataINEndpointNumber = CDC2_TX_EPNUM,
52 .DataINEndpointSize = CDC_TXRX_EPSIZE,
53
54 .DataOUTEndpointNumber = CDC2_RX_EPNUM,
55 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
56
57 .NotificationEndpointNumber = CDC2_NOTIFICATION_EPNUM,
58 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
59 };
60
61 int main(void)
62 {
63 SetupHardware();
64
65 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
66
67 for (;;)
68 {
69 CheckJoystickMovement();
70
71 uint16_t BytesToDiscard = USB_CDC_BytesReceived(&VirtualSerial1_CDC_Interface);
72 while (BytesToDiscard--)
73 USB_CDC_ReceiveByte(&VirtualSerial1_CDC_Interface);
74
75 uint16_t BytesToEcho = USB_CDC_BytesReceived(&VirtualSerial2_CDC_Interface);
76 while (BytesToEcho--)
77 USB_CDC_SendByte(&VirtualSerial2_CDC_Interface, USB_CDC_ReceiveByte(&VirtualSerial2_CDC_Interface));
78
79 USB_CDC_USBTask(&VirtualSerial1_CDC_Interface);
80 USB_CDC_USBTask(&VirtualSerial2_CDC_Interface);
81 USB_USBTask();
82 }
83 }
84
85 void SetupHardware(void)
86 {
87 /* Disable watchdog if enabled by bootloader/fuses */
88 MCUSR &= ~(1 << WDRF);
89 wdt_disable();
90
91 /* Disable clock division */
92 clock_prescale_set(clock_div_1);
93
94 /* Hardware Initialization */
95 Joystick_Init();
96 LEDs_Init();
97 USB_Init();
98 }
99
100 void CheckJoystickMovement(void)
101 {
102 uint8_t JoyStatus_LCL = Joystick_GetStatus();
103 char* ReportString = NULL;
104 static bool ActionSent = false;
105
106 char* JoystickStrings[] =
107 {
108 "Joystick Up\r\n",
109 "Joystick Down\r\n",
110 "Joystick Left\r\n",
111 "Joystick Right\r\n",
112 "Joystick Pressed\r\n",
113 };
114
115 if (JoyStatus_LCL & JOY_UP)
116 ReportString = JoystickStrings[0];
117 else if (JoyStatus_LCL & JOY_DOWN)
118 ReportString = JoystickStrings[1];
119 else if (JoyStatus_LCL & JOY_LEFT)
120 ReportString = JoystickStrings[2];
121 else if (JoyStatus_LCL & JOY_RIGHT)
122 ReportString = JoystickStrings[3];
123 else if (JoyStatus_LCL & JOY_PRESS)
124 ReportString = JoystickStrings[4];
125 else
126 ActionSent = false;
127
128 if ((ReportString != NULL) && (ActionSent == false))
129 {
130 ActionSent = true;
131
132 USB_CDC_SendString(&VirtualSerial1_CDC_Interface, ReportString, strlen(ReportString));
133 }
134 }
135
136 void EVENT_USB_Connect(void)
137 {
138 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
139 }
140
141 void EVENT_USB_Disconnect(void)
142 {
143 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
144 }
145
146 void EVENT_USB_ConfigurationChanged(void)
147 {
148 LEDs_SetAllLEDs(LEDMASK_USB_READY);
149
150 if (!(USB_CDC_ConfigureEndpoints(&VirtualSerial1_CDC_Interface)))
151 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
152
153 if (!(USB_CDC_ConfigureEndpoints(&VirtualSerial2_CDC_Interface)))
154 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
155 }
156
157 void EVENT_USB_UnhandledControlPacket(void)
158 {
159 USB_CDC_ProcessControlPacket(&VirtualSerial1_CDC_Interface);
160 USB_CDC_ProcessControlPacket(&VirtualSerial2_CDC_Interface);
161 }