/elec/audio-switcher

To get this branch, use:
bzr branch http://bzr.ed.am/elec/audio-switcher
1 by Tim Marston
initial commit
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 VirtualSerial demo. This file contains the main tasks of
34
 *  the demo and is responsible for the initial application hardware configuration.
35
 */
36
37
#include "VirtualSerial.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
				.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
/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
70
 *  used like any regular character stream in the C APIs
71
 */
72
static FILE USBSerialStream;
73
74
75
#define BUTTON 0b10000000
76
#define LEDB 0b00100000
77
78
void init()
79
{
80
	// set PD7 (button) to input
81
	DDRD &= ~BUTTON;
82
83
	// enable pull-up on PD7 (button)
84
	PORTD |= BUTTON;
85
86
	// set PD5 (ledb) to output
87
	DDRD |= LEDB;
88
}
89
90
void loop()
91
{
92
	static bool sent = false;
93
94
	// if PD7 is low, we have a press!
95
	if( PIND & BUTTON )
96
	{
97
		if( !sent )
98
		{
99
			sent = true;
100
			fputs( "press\r\n", &USBSerialStream );
101
		}
102
103
		// set led
104
		PORTD |= LEDB;
105
	}
106
	else
107
	{
108
		sent = false;
109
110
		// set led
111
		PORTD &= ~LEDB;
112
	}
113
}
114
115
116
/** Main program entry point. This routine contains the overall program flow, including initial
117
 *  setup of all components and the main program loop.
118
 */
119
int main(void)
120
{
121
	SetupHardware();
122
123
	/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
124
	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
125
126
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
127
	sei();
128
129
	for (;;)
130
	{
131
		loop();
132
133
		/* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
134
		CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
135
136
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
137
		USB_USBTask();
138
	}
139
}
140
141
/** Configures the board hardware and chip peripherals for the demo's functionality. */
142
void SetupHardware(void)
143
{
144
	/* Disable watchdog if enabled by bootloader/fuses */
145
	MCUSR &= ~(1 << WDRF);
146
	wdt_disable();
147
148
	/* Disable clock division */
149
	clock_prescale_set(clock_div_1);
150
151
	/* Hardware Initialization */
152
	LEDs_Init();
153
	USB_Init();
154
155
	init();	
156
}
157
158
/** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
159
#if 0
160
void CheckJoystickMovement(void)
161
{
162
	uint8_t     JoyStatus_LCL = Joystick_GetStatus();
163
	char*       ReportString  = NULL;
164
	static bool ActionSent    = false;
165
166
	if (JoyStatus_LCL & JOY_UP)
167
	  ReportString = "Joystick Up\r\n";
168
	else if (JoyStatus_LCL & JOY_DOWN)
169
	  ReportString = "Joystick Down\r\n";
170
	else if (JoyStatus_LCL & JOY_LEFT)
171
	  ReportString = "Joystick Left\r\n";
172
	else if (JoyStatus_LCL & JOY_RIGHT)
173
	  ReportString = "Joystick Right\r\n";
174
	else if (JoyStatus_LCL & JOY_PRESS)
175
	  ReportString = "Joystick Pressed\r\n";
176
	else
177
	  ActionSent = false;
178
179
	if ((ReportString != NULL) && (ActionSent == false))
180
	{
181
		ActionSent = true;
182
183
		/* Write the string to the virtual COM port via the created character stream */
184
		fputs(ReportString, &USBSerialStream);
185
186
		/* Alternatively, without the stream: */
187
		// CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
188
	}
189
}
190
#endif
191
192
/** Event handler for the library USB Connection event. */
193
void EVENT_USB_Device_Connect(void)
194
{
195
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
196
}
197
198
/** Event handler for the library USB Disconnection event. */
199
void EVENT_USB_Device_Disconnect(void)
200
{
201
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
202
}
203
204
/** Event handler for the library USB Configuration Changed event. */
205
void EVENT_USB_Device_ConfigurationChanged(void)
206
{
207
	bool ConfigSuccess = true;
208
209
	ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
210
211
	LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
212
}
213
214
/** Event handler for the library USB Control Request reception event. */
215
void EVENT_USB_Device_ControlRequest(void)
216
{
217
	CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
218
}
219