/elec/audio-switcher

To get this branch, use:
bzr branch http://bzr.ed.am/elec/audio-switcher

« back to all changes in this revision

Viewing changes to src/VirtualSerial.c

  • Committer: Tim Marston
  • Date: 2012-11-23 00:00:34 UTC
  • Revision ID: tim@ed.am-20121123000034-386kbpshw5i7m8wi
added debouncing, button press detection and relay state toggling

Show diffs side-by-side

added added

removed removed

38
38
 
39
39
/** \file
40
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.
 
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.
43
44
 */
44
45
 
45
46
#include "VirtualSerial.h"
46
47
#include "wiring.h"
47
48
 
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.
 
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.
51
53
 */
52
54
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
53
55
        {
75
77
                        },
76
78
        };
77
79
 
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
/** 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
80
82
 */
81
83
static FILE USBSerialStream;
82
84
 
83
85
 
84
86
#define BUTTON 0b10000000
85
87
#define LEDB 0b00100000
 
88
#define RELAY 0b00000010
86
89
 
87
90
void init()
88
91
{
89
 
        // set PD7 (button) to input
 
92
        // set PD7 (button) to input and enable pull-up
90
93
        DDRD &= ~BUTTON;
91
 
 
92
 
        // enable pull-up on PD7 (button)
93
94
        PORTD |= BUTTON;
94
95
 
95
96
        // set PD5 (ledb) to output
96
97
        DDRD |= LEDB;
 
98
 
 
99
        // set PB1 (relay) to output
 
100
        DDRB |= RELAY;
97
101
}
98
102
 
99
103
void loop()
100
104
{
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;
109
 
                        fprintf( &USBSerialStream, "%lu\r\n", millis() );
110
 
//                      fputs( "press\r\n", &USBSerialStream );
111
 
                }
112
 
 
113
 
                // set led
114
 
                PORTD |= LEDB;
115
 
        }
116
 
        else
117
 
        {
118
 
                sent = false;
119
 
 
120
 
                // set led
121
 
                PORTD &= ~LEDB;
 
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;
122
148
        }
123
149
}
124
150
 
125
151
 
126
 
/** Main program entry point. This routine contains the overall program flow, including initial
127
 
 *  setup of all components and the main program loop.
 
152
/** Main program entry point. This routine contains the overall program flow,
 
153
 *  including initial setup of all components and the main program loop.
128
154
 */
129
155
int main(void)
130
156
{
131
157
        SetupHardware();
132
158
 
133
 
        /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
 
159
        /* Create a regular character stream for the interface so that it can be
 
160
         * used with the stdio.h functions */
134
161
        CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
135
162
 
136
163
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
140
167
        {
141
168
                loop();
142
169
 
143
 
                /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
 
170
                /* Must throw away unused bytes from the host, or it will lock up while
 
171
                 * waiting for the device */
144
172
                CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
145
173
 
146
174
                CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
148
176
        }
149
177
}
150
178
 
151
 
/** Configures the board hardware and chip peripherals for the demo's functionality. */
 
179
/** Configures the board hardware and chip peripherals for the demo's
 
180
 * functionality. */
152
181
void SetupHardware(void)
153
182
{
154
183
        /* Disable watchdog if enabled by bootloader/fuses */