/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

1
1
/*
2
 
             LUFA Library
3
 
     Copyright (C) Dean Camera, 2012.
4
 
 
5
 
  dean [at] fourwalledcubicle [dot] com
6
 
           www.lufa-lib.org
 
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
 
7
15
*/
8
16
 
9
17
/*
30
38
 
31
39
/** \file
32
40
 *
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.
 
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.
35
44
 */
36
45
 
37
46
#include "VirtualSerial.h"
 
47
#include "wiring.h"
38
48
 
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.
 
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.
42
53
 */
43
54
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
44
55
        {
66
77
                        },
67
78
        };
68
79
 
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
 
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
71
82
 */
72
83
static FILE USBSerialStream;
73
84
 
74
85
 
75
86
#define BUTTON 0b10000000
76
87
#define LEDB 0b00100000
 
88
#define RELAY 0b00000010
77
89
 
78
90
void init()
79
91
{
80
 
        // set PD7 (button) to input
 
92
        // set PD7 (button) to input and enable pull-up
81
93
        DDRD &= ~BUTTON;
82
 
 
83
 
        // enable pull-up on PD7 (button)
84
94
        PORTD |= BUTTON;
85
95
 
86
96
        // set PD5 (ledb) to output
87
97
        DDRD |= LEDB;
 
98
 
 
99
        // set PB1 (relay) to output
 
100
        DDRB |= RELAY;
88
101
}
89
102
 
90
103
void loop()
91
104
{
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;
 
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;
112
148
        }
113
149
}
114
150
 
115
151
 
116
 
/** Main program entry point. This routine contains the overall program flow, including initial
117
 
 *  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.
118
154
 */
119
155
int main(void)
120
156
{
121
157
        SetupHardware();
122
158
 
123
 
        /* 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 */
124
161
        CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
125
162
 
126
163
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
130
167
        {
131
168
                loop();
132
169
 
133
 
                /* 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 */
134
172
                CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
135
173
 
136
174
                CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
138
176
        }
139
177
}
140
178
 
141
 
/** 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. */
142
181
void SetupHardware(void)
143
182
{
144
183
        /* Disable watchdog if enabled by bootloader/fuses */
152
191
        LEDs_Init();
153
192
        USB_Init();
154
193
 
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
 
194
        // do my init
 
195
        init();
 
196
 
 
197
        // set up arduino wiring.c
 
198
        init_wiring_c();
 
199
}
191
200
 
192
201
/** Event handler for the library USB Connection event. */
193
202
void EVENT_USB_Device_Connect(void)