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