/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-28 23:51:05 UTC
  • Revision ID: tim@ed.am-20121128235105-t82czxu8kbp3av2j
updated notes, and added control shell script

Show diffs side-by-side

added added

removed removed

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