VirtualSerial.c 5.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
             LUFA Library
     Copyright (C) Dean Camera, 2009.
              
  dean [at] fourwalledcubicle [dot] com
      www.fourwalledcubicle.com
*/

/*
  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, and distribute this software
  and its documentation for any purpose and without fee is hereby
  granted, provided that the above copyright notice appear in all
  copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

31
32
/** \file
 *
33
 *  Main source file for the VirtualSerial demo. This file contains the main tasks of
34
35
36
 *  the demo and is responsible for the initial application hardware configuration.
 */
 
37
#include "VirtualSerial.h"
38

39
40
41
42
/** LUFA CDC Class driver interface configuration and state information. This structure is
 *  passed to all CDC Class driver functions, so that multiple instances of the same class
 *  within a device can be differentiated from one another.
 */
43
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
44
	{
45
46
		.Config =
			{
47
				.ControlInterfaceNumber         = 0,
48

49
50
51
				.DataINEndpointNumber           = CDC_TX_EPNUM,
				.DataINEndpointSize             = CDC_TXRX_EPSIZE,
				.DataINEndpointDoubleBank       = false,
52

53
54
55
				.DataOUTEndpointNumber          = CDC_RX_EPNUM,
				.DataOUTEndpointSize            = CDC_TXRX_EPSIZE,
				.DataOUTEndpointDoubleBank      = false,
56

57
58
59
				.NotificationEndpointNumber     = CDC_NOTIFICATION_EPNUM,
				.NotificationEndpointSize       = CDC_NOTIFICATION_EPSIZE,
				.NotificationEndpointDoubleBank = false,
60
			},
61
	};
62

63
64
/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
 *  used like any regular character stream in the C APIs
65
 */
66
static FILE USBSerialStream;
67

68
69
70
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
71
72
73
74
int main(void)
{
	SetupHardware();
	
75
76
77
	/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);

78
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
79

80
81
82
	for (;;)
	{
		CheckJoystickMovement();
83
		 
84
		/* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
85
		while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
86
		  CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
87

88
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
89
90
91
		USB_USBTask();
	}
}
92

93
/** Configures the board hardware and chip peripherals for the demo's functionality. */
94
void SetupHardware(void)
95
96
97
98
99
{
	/* Disable watchdog if enabled by bootloader/fuses */
	MCUSR &= ~(1 << WDRF);
	wdt_disable();

100
101
	/* Disable clock division */
	clock_prescale_set(clock_div_1);
102
103
104
105
106
107
108

	/* Hardware Initialization */
	Joystick_Init();
	LEDs_Init();
	USB_Init();
}

109
/** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
110
void CheckJoystickMovement(void)
111
{
112
113
	uint8_t     JoyStatus_LCL = Joystick_GetStatus();
	char*       ReportString  = NULL;
114
	static bool ActionSent    = false;
115
116
	
	if (JoyStatus_LCL & JOY_UP)
117
	  ReportString = "Joystick Up\r\n";
118
	else if (JoyStatus_LCL & JOY_DOWN)
119
	  ReportString = "Joystick Down\r\n";
120
	else if (JoyStatus_LCL & JOY_LEFT)
121
	  ReportString = "Joystick Left\r\n";
122
	else if (JoyStatus_LCL & JOY_RIGHT)
123
	  ReportString = "Joystick Right\r\n";
124
	else if (JoyStatus_LCL & JOY_PRESS)
125
	  ReportString = "Joystick Pressed\r\n";
126
127
128
129
	else
	  ActionSent = false;
	  
	if ((ReportString != NULL) && (ActionSent == false))
130
131
	{
		ActionSent = true;
132

Dean Camera's avatar
Dean Camera committed
133
		/* Write the string to the virtual COM port via the created character stream */
134
135
		fputs(ReportString, &USBSerialStream);

Dean Camera's avatar
Dean Camera committed
136
		/* Alternatively, without the stream: */
137
		// CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString));
138
139
	}
}
140

141
/** Event handler for the library USB Connection event. */
142
void EVENT_USB_Device_Connect(void)
143
144
145
{
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}
146

147
/** Event handler for the library USB Disconnection event. */
148
void EVENT_USB_Device_Disconnect(void)
149
150
151
{
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}
152

153
/** Event handler for the library USB Configuration Changed event. */
154
void EVENT_USB_Device_ConfigurationChanged(void)
155
156
{
	LEDs_SetAllLEDs(LEDMASK_USB_READY);
157

158
	if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))
159
160
	  LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}
161

162
163
/** Event handler for the library USB Unhandled Control Request event. */
void EVENT_USB_Device_UnhandledControlRequest(void)
164
{
165
	CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
166
}