/elec/audio-switcher

To get this branch, use:
bzr branch http://bzr.ed.am/elec/audio-switcher
1 by Tim Marston
initial commit
1
/*
3 by Tim Marston
added timer utilities (wiring.c) from arduino library
2
3
  AUDIO SWITCHER
4
5
  Based on a serial device demonstration from the LUFA project
6
  (www.lufa-lib.org).  LUFA is Copyright (C) Dean Camera, 2012.
7
8
  Changes made to the serial device demonstration program are
9
  Copyright (C) Tim Marston, 2012.
10
11
  For more information:
12
    * see README, or
13
    * visit http://ed.am/
14
1 by Tim Marston
initial commit
15
*/
16
17
/*
18
  Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)
19
20
  Permission to use, copy, modify, distribute, and sell this
21
  software and its documentation for any purpose is hereby granted
22
  without fee, provided that the above copyright notice appear in
23
  all copies and that both that the copyright notice and this
24
  permission notice and warranty disclaimer appear in supporting
25
  documentation, and that the name of the author not be used in
26
  advertising or publicity pertaining to distribution of the
27
  software without specific, written prior permission.
28
29
  The author disclaim all warranties with regard to this
30
  software, including all implied warranties of merchantability
31
  and fitness.  In no event shall the author be liable for any
32
  special, indirect or consequential damages or any damages
33
  whatsoever resulting from loss of use, data or profits, whether
34
  in an action of contract, negligence or other tortious action,
35
  arising out of or in connection with the use or performance of
36
  this software.
37
*/
38
39
/** \file
40
 *
41
 *  Main source file for the VirtualSerial demo. This file contains the main tasks of
42
 *  the demo and is responsible for the initial application hardware configuration.
43
 */
44
45
#include "VirtualSerial.h"
3 by Tim Marston
added timer utilities (wiring.c) from arduino library
46
#include "wiring.h"
1 by Tim Marston
initial commit
47
48
/** LUFA CDC Class driver interface configuration and state information. This structure is
49
 *  passed to all CDC Class driver functions, so that multiple instances of the same class
50
 *  within a device can be differentiated from one another.
51
 */
52
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
53
	{
54
		.Config =
55
			{
56
				.ControlInterfaceNumber   = 0,
57
				.DataINEndpoint           =
58
					{
59
						.Address          = CDC_TX_EPADDR,
60
						.Size             = CDC_TXRX_EPSIZE,
61
						.Banks            = 1,
62
					},
63
				.DataOUTEndpoint =
64
					{
65
						.Address          = CDC_RX_EPADDR,
66
						.Size             = CDC_TXRX_EPSIZE,
67
						.Banks            = 1,
68
					},
69
				.NotificationEndpoint =
70
					{
71
						.Address          = CDC_NOTIFICATION_EPADDR,
72
						.Size             = CDC_NOTIFICATION_EPSIZE,
73
						.Banks            = 1,
74
					},
75
			},
76
	};
77
78
/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
79
 *  used like any regular character stream in the C APIs
80
 */
81
static FILE USBSerialStream;
82
83
84
#define BUTTON 0b10000000
85
#define LEDB 0b00100000
86
87
void init()
88
{
89
	// set PD7 (button) to input
90
	DDRD &= ~BUTTON;
91
92
	// enable pull-up on PD7 (button)
93
	PORTD |= BUTTON;
94
95
	// set PD5 (ledb) to output
96
	DDRD |= LEDB;
97
}
98
99
void loop()
100
{
101
	static bool sent = false;
102
103
	// if PD7 is low, we have a press!
104
	if( PIND & BUTTON )
105
	{
106
		if( !sent )
107
		{
108
			sent = true;
3 by Tim Marston
added timer utilities (wiring.c) from arduino library
109
			fprintf( &USBSerialStream, "%lu\r\n", millis() );
110
//			fputs( "press\r\n", &USBSerialStream );
1 by Tim Marston
initial commit
111
		}
112
113
		// set led
114
		PORTD |= LEDB;
115
	}
116
	else
117
	{
118
		sent = false;
119
120
		// set led
121
		PORTD &= ~LEDB;
122
	}
123
}
124
125
126
/** Main program entry point. This routine contains the overall program flow, including initial
127
 *  setup of all components and the main program loop.
128
 */
129
int main(void)
130
{
131
	SetupHardware();
132
133
	/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
134
	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
135
136
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
137
	sei();
138
139
	for (;;)
140
	{
141
		loop();
142
143
		/* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
144
		CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
145
146
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
147
		USB_USBTask();
148
	}
149
}
150
151
/** Configures the board hardware and chip peripherals for the demo's functionality. */
152
void SetupHardware(void)
153
{
154
	/* Disable watchdog if enabled by bootloader/fuses */
155
	MCUSR &= ~(1 << WDRF);
156
	wdt_disable();
157
158
	/* Disable clock division */
159
	clock_prescale_set(clock_div_1);
160
161
	/* Hardware Initialization */
162
	LEDs_Init();
163
	USB_Init();
164
3 by Tim Marston
added timer utilities (wiring.c) from arduino library
165
	// do my init
166
	init();
167
168
	// set up arduino wiring.c
169
	init_wiring_c();
170
}
1 by Tim Marston
initial commit
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 &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
190
191
	LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
192
}
193
194
/** Event handler for the library USB Control Request reception event. */
195
void EVENT_USB_Device_ControlRequest(void)
196
{
197
	CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
198
}
199