/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

100
100
        DDRB |= RELAY;
101
101
}
102
102
 
103
 
#define BUFFERLEN 10
104
 
 
105
103
void loop()
106
104
{
107
105
        // debounce button (PD7)
110
108
        static int saved_since = 0;
111
109
        unsigned long now = millis();
112
110
        unsigned long real_button_state = PIND & BUTTON;
113
 
        bool button_state_changed = false;
114
111
        if( saved_button_state != real_button_state ) {
115
112
                saved_button_state = real_button_state;
116
113
                saved_since = now;
119
116
                         now - saved_since > 25 )
120
117
        {
121
118
                button_state = saved_button_state;
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
 
 
 
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
145
132
        static bool relay_state = false;
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 )
 
133
        if( toggle )
183
134
        {
184
135
                relay_state = !relay_state;
185
136
 
216
167
        {
217
168
                loop();
218
169
 
 
170
                /* Must throw away unused bytes from the host, or it will lock up while
 
171
                 * waiting for the device */
 
172
                CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
 
173
 
219
174
                CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
220
175
                USB_USBTask();
221
176
        }
236
191
        LEDs_Init();
237
192
        USB_Init();
238
193
 
239
 
        // my init
 
194
        // do my init
240
195
        init();
241
196
 
242
197
        // set up arduino wiring.c