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 |
||
103 |
void loop() |
|
104 |
{ |
|
4
by Tim Marston
added debouncing, button press detection and relay state toggling |
105 |
// debounce button (PD7) |
106 |
static int button_state = 0; |
|
107 |
static int saved_button_state = 0; |
|
108 |
static int saved_since = 0; |
|
109 |
unsigned long now = millis(); |
|
110 |
unsigned long real_button_state = PIND & BUTTON; |
|
111 |
if( saved_button_state != real_button_state ) { |
|
112 |
saved_button_state = real_button_state; |
|
113 |
saved_since = now; |
|
114 |
} |
|
115 |
else if( saved_button_state != button_state && |
|
116 |
now - saved_since > 25 ) |
|
117 |
{ |
|
118 |
button_state = saved_button_state; |
|
119 |
} |
|
120 |
||
121 |
// on rising edge of press, set toggle |
|
122 |
static int last_button_state = 0; |
|
123 |
bool toggle = false; |
|
124 |
if( last_button_state != button_state ) |
|
125 |
{ |
|
126 |
last_button_state = button_state; |
|
127 |
if( !button_state ) toggle = true; |
|
128 |
} |
|
129 |
||
130 |
||
131 |
// toggle the relay |
|
132 |
static bool relay_state = false; |
|
133 |
if( toggle ) |
|
134 |
{ |
|
135 |
relay_state = !relay_state; |
|
136 |
||
137 |
// switch relay |
|
138 |
if( relay_state ) |
|
139 |
PORTB |= RELAY; |
|
140 |
else |
|
141 |
PORTB &= ~RELAY; |
|
142 |
||
143 |
// switch LED |
|
144 |
if( relay_state ) |
|
145 |
PORTD &= ~LEDB; |
|
146 |
else |
|
147 |
PORTD |= LEDB; |
|
1
by Tim Marston
initial commit |
148 |
} |
149 |
} |
|
150 |
||
151 |
||
4
by Tim Marston
added debouncing, button press detection and relay state toggling |
152 |
/** Main program entry point. This routine contains the overall program flow, |
153 |
* including initial setup of all components and the main program loop. |
|
1
by Tim Marston
initial commit |
154 |
*/ |
155 |
int main(void) |
|
156 |
{ |
|
157 |
SetupHardware(); |
|
158 |
||
4
by Tim Marston
added debouncing, button press detection and relay state toggling |
159 |
/* Create a regular character stream for the interface so that it can be |
160 |
* used with the stdio.h functions */ |
|
1
by Tim Marston
initial commit |
161 |
CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream); |
162 |
||
163 |
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); |
|
164 |
sei(); |
|
165 |
||
166 |
for (;;) |
|
167 |
{ |
|
168 |
loop(); |
|
169 |
||
4
by Tim Marston
added debouncing, button press detection and relay state toggling |
170 |
/* Must throw away unused bytes from the host, or it will lock up while |
171 |
* waiting for the device */ |
|
1
by Tim Marston
initial commit |
172 |
CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); |
173 |
||
174 |
CDC_Device_USBTask(&VirtualSerial_CDC_Interface); |
|
175 |
USB_USBTask(); |
|
176 |
} |
|
177 |
} |
|
178 |
||
4
by Tim Marston
added debouncing, button press detection and relay state toggling |
179 |
/** Configures the board hardware and chip peripherals for the demo's |
180 |
* functionality. */ |
|
1
by Tim Marston
initial commit |
181 |
void SetupHardware(void) |
182 |
{ |
|
183 |
/* Disable watchdog if enabled by bootloader/fuses */ |
|
184 |
MCUSR &= ~(1 << WDRF); |
|
185 |
wdt_disable(); |
|
186 |
||
187 |
/* Disable clock division */ |
|
188 |
clock_prescale_set(clock_div_1); |
|
189 |
||
190 |
/* Hardware Initialization */ |
|
191 |
LEDs_Init(); |
|
192 |
USB_Init(); |
|
193 |
||
3
by Tim Marston
added timer utilities (wiring.c) from arduino library |
194 |
// do my init |
195 |
init(); |
|
196 |
||
197 |
// set up arduino wiring.c |
|
198 |
init_wiring_c(); |
|
199 |
} |
|
1
by Tim Marston
initial commit |
200 |
|
201 |
/** Event handler for the library USB Connection event. */ |
|
202 |
void EVENT_USB_Device_Connect(void) |
|
203 |
{ |
|
204 |
LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); |
|
205 |
} |
|
206 |
||
207 |
/** Event handler for the library USB Disconnection event. */ |
|
208 |
void EVENT_USB_Device_Disconnect(void) |
|
209 |
{ |
|
210 |
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); |
|
211 |
} |
|
212 |
||
213 |
/** Event handler for the library USB Configuration Changed event. */ |
|
214 |
void EVENT_USB_Device_ConfigurationChanged(void) |
|
215 |
{ |
|
216 |
bool ConfigSuccess = true; |
|
217 |
||
218 |
ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); |
|
219 |
||
220 |
LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); |
|
221 |
} |
|
222 |
||
223 |
/** Event handler for the library USB Control Request reception event. */ |
|
224 |
void EVENT_USB_Device_ControlRequest(void) |
|
225 |
{ |
|
226 |
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); |
|
227 |
} |
|
228 |