/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
 *
4 by Tim Marston
added debouncing, button press detection and relay state toggling
41
 *  Main source file for the VirtualSerial demo. This file contains the main
42
 *  tasks of the demo and is responsible for the initial application hardware
43
 *  configuration.
1 by Tim Marston
initial commit
44
 */
45
46
#include "VirtualSerial.h"
3 by Tim Marston
added timer utilities (wiring.c) from arduino library
47
#include "wiring.h"
1 by Tim Marston
initial commit
48
4 by Tim Marston
added debouncing, button press detection and relay state toggling
49
/** LUFA CDC Class driver interface configuration and state information. This
50
 *  structure is passed to all CDC Class driver functions, so that multiple
51
 *  instances of the same class within a device can be differentiated from one
52
 *  another.
1 by Tim Marston
initial commit
53
 */
54
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
55
	{
56
		.Config =
57
			{
58
				.ControlInterfaceNumber   = 0,
59
				.DataINEndpoint           =
60
					{
61
						.Address          = CDC_TX_EPADDR,
62
						.Size             = CDC_TXRX_EPSIZE,
63
						.Banks            = 1,
64
					},
65
				.DataOUTEndpoint =
66
					{
67
						.Address          = CDC_RX_EPADDR,
68
						.Size             = CDC_TXRX_EPSIZE,
69
						.Banks            = 1,
70
					},
71
				.NotificationEndpoint =
72
					{
73
						.Address          = CDC_NOTIFICATION_EPADDR,
74
						.Size             = CDC_NOTIFICATION_EPSIZE,
75
						.Banks            = 1,
76
					},
77
			},
78
	};
79
4 by Tim Marston
added debouncing, button press detection and relay state toggling
80
/** Standard file stream for the CDC interface when set up, so that the virtual
81
 *  CDC COM port can be used like any regular character stream in the C APIs
1 by Tim Marston
initial commit
82
 */
83
static FILE USBSerialStream;
84
85
86
#define BUTTON 0b10000000
87
#define LEDB 0b00100000
4 by Tim Marston
added debouncing, button press detection and relay state toggling
88
#define RELAY 0b00000010
1 by Tim Marston
initial commit
89
90
void init()
91
{
4 by Tim Marston
added debouncing, button press detection and relay state toggling
92
	// set PD7 (button) to input and enable pull-up
1 by Tim Marston
initial commit
93
	DDRD &= ~BUTTON;
94
	PORTD |= BUTTON;
95
96
	// set PD5 (ledb) to output
97
	DDRD |= LEDB;
4 by Tim Marston
added debouncing, button press detection and relay state toggling
98
99
	// set PB1 (relay) to output
100
	DDRB |= RELAY;
1 by Tim Marston
initial commit
101
}
102
5 by Tim Marston
added serial buffer reading, command handling and rewrote button press
103
#define BUFFERLEN 10
104
1 by Tim Marston
initial commit
105
void loop()
106
{
4 by Tim Marston
added debouncing, button press detection and relay state toggling
107
	// debounce button (PD7)
108
	static int button_state = 0;
109
	static int saved_button_state = 0;
110
	static int saved_since = 0;
111
	unsigned long now = millis();
112
	unsigned long real_button_state = PIND & BUTTON;
5 by Tim Marston
added serial buffer reading, command handling and rewrote button press
113
	bool button_state_changed = false;
4 by Tim Marston
added debouncing, button press detection and relay state toggling
114
	if( saved_button_state != real_button_state ) {
115
		saved_button_state = real_button_state;
116
		saved_since = now;
117
	}
118
	else if( saved_button_state != button_state &&
119
			 now - saved_since > 25 )
120
	{
121
		button_state = saved_button_state;
5 by Tim Marston
added serial buffer reading, command handling and rewrote button press
122
		button_state_changed = true;
123
	}
124
125
	// read buffer from serial
126
	static int buffer_pos = 0;
127
	static char buffer[ BUFFERLEN ] = "";
128
	int got = getc( &USBSerialStream );
129
	bool got_command = false;
130
	switch( got ) {
131
	case EOF:
132
	case 0: break;
133
	case '\r': 
134
	case '\n': got_command = true; break;
135
	case ' ':
136
	case '\t':
137
		if( !buffer_pos ) break;
138
		// fall through
139
	default:
140
		if( buffer_pos < ( BUFFERLEN - 1 ) )
141
			buffer[ buffer_pos++ ] = got;
142
		buffer[ buffer_pos ] = 0;
143
	}
144
4 by Tim Marston
added debouncing, button press detection and relay state toggling
145
	static bool relay_state = false;
5 by Tim Marston
added serial buffer reading, command handling and rewrote button press
146
	bool toggle_relay = false;
147
148
	// handle command
149
	if( got_command )
150
	{
151
		// command: turn relay off
152
		if( !strcmp( buffer, "0" ) ) {
153
			if( relay_state ) toggle_relay = true;
154
		}
155
156
		// command: turn relay on
157
		else if( !strcmp( buffer, "1" ) ) {
158
			if( !relay_state ) toggle_relay = true;
159
		}
160
161
		// command: report relay state
162
		else if( !strcmp( buffer, "?" ) ) {
163
			fputs( relay_state? "1\r\n" : "0\r\n", &USBSerialStream );
164
		}
165
166
		// command: toggle relay state
167
		else if( !strcmp( buffer, "." ) ) {
168
			toggle_relay = true;
169
		}
170
171
		// clear command
172
		buffer_pos = buffer[ 0 ] = 0;
173
	}
174
175
	// handle button press?
176
	if( button_state_changed && !button_state )
177
	{
178
		toggle_relay = true;
179
	}
180
181
	// toggle the relay?
182
	if( toggle_relay )
4 by Tim Marston
added debouncing, button press detection and relay state toggling
183
	{
184
		relay_state = !relay_state;
185
186
		// switch relay
187
		if( relay_state )
188
			PORTB |= RELAY;
189
		else
190
			PORTB &= ~RELAY;
191
192
		// switch LED
193
		if( relay_state )
194
			PORTD &= ~LEDB;
195
		else
196
			PORTD |= LEDB;
1 by Tim Marston
initial commit
197
	}
198
}
199
200
4 by Tim Marston
added debouncing, button press detection and relay state toggling
201
/** Main program entry point. This routine contains the overall program flow,
202
 *  including initial setup of all components and the main program loop.
1 by Tim Marston
initial commit
203
 */
204
int main(void)
205
{
206
	SetupHardware();
207
4 by Tim Marston
added debouncing, button press detection and relay state toggling
208
	/* Create a regular character stream for the interface so that it can be
209
	 * used with the stdio.h functions */
1 by Tim Marston
initial commit
210
	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
211
212
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
213
	sei();
214
215
	for (;;)
216
	{
217
		loop();
218
219
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
220
		USB_USBTask();
221
	}
222
}
223
4 by Tim Marston
added debouncing, button press detection and relay state toggling
224
/** Configures the board hardware and chip peripherals for the demo's
225
 * functionality. */
1 by Tim Marston
initial commit
226
void SetupHardware(void)
227
{
228
	/* Disable watchdog if enabled by bootloader/fuses */
229
	MCUSR &= ~(1 << WDRF);
230
	wdt_disable();
231
232
	/* Disable clock division */
233
	clock_prescale_set(clock_div_1);
234
235
	/* Hardware Initialization */
236
	LEDs_Init();
237
	USB_Init();
238
5 by Tim Marston
added serial buffer reading, command handling and rewrote button press
239
	// my init
3 by Tim Marston
added timer utilities (wiring.c) from arduino library
240
	init();
241
242
	// set up arduino wiring.c
243
	init_wiring_c();
244
}
1 by Tim Marston
initial commit
245
246
/** Event handler for the library USB Connection event. */
247
void EVENT_USB_Device_Connect(void)
248
{
249
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
250
}
251
252
/** Event handler for the library USB Disconnection event. */
253
void EVENT_USB_Device_Disconnect(void)
254
{
255
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
256
}
257
258
/** Event handler for the library USB Configuration Changed event. */
259
void EVENT_USB_Device_ConfigurationChanged(void)
260
{
261
	bool ConfigSuccess = true;
262
263
	ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
264
265
	LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
266
}
267
268
/** Event handler for the library USB Control Request reception event. */
269
void EVENT_USB_Device_ControlRequest(void)
270
{
271
	CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
272
}
273