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
37
46
#include "VirtualSerial.h"
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
43
54
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
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
72
83
static FILE USBSerialStream;
75
86
#define BUTTON 0b10000000
76
87
#define LEDB 0b00100000
88
#define RELAY 0b00000010
80
// set PD7 (button) to input
92
// set PD7 (button) to input and enable pull-up
83
// enable pull-up on PD7 (button)
86
96
// set PD5 (ledb) to output
99
// set PB1 (relay) to output
92
static bool sent = false;
94
// if PD7 is low, we have a press!
100
fputs( "press\r\n", &USBSerialStream );
107
// debounce button (PD7)
108
static int button_state = 0;
109
static int saved_button_state = 0;
110
static int saved_since = 0;
111
unsigned long now = millis();
112
unsigned long real_button_state = PIND & BUTTON;
113
bool button_state_changed = false;
114
if( saved_button_state != real_button_state ) {
115
saved_button_state = real_button_state;
118
else if( saved_button_state != button_state &&
119
now - saved_since > 25 )
121
button_state = saved_button_state;
122
button_state_changed = true;
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;
134
case '\n': got_command = true; break;
137
if( !buffer_pos ) break;
140
if( buffer_pos < ( BUFFERLEN - 1 ) )
141
buffer[ buffer_pos++ ] = got;
142
buffer[ buffer_pos ] = 0;
145
static bool relay_state = false;
146
bool toggle_relay = false;
151
// command: turn relay off
152
if( !strcmp( buffer, "0" ) ) {
153
if( relay_state ) toggle_relay = true;
156
// command: turn relay on
157
else if( !strcmp( buffer, "1" ) ) {
158
if( !relay_state ) toggle_relay = true;
161
// command: report relay state
162
else if( !strcmp( buffer, "?" ) ) {
163
fputs( relay_state? "1\r\n" : "0\r\n", &USBSerialStream );
166
// command: toggle relay state
167
else if( !strcmp( buffer, "." ) ) {
172
buffer_pos = buffer[ 0 ] = 0;
175
// handle button press?
176
if( button_state_changed && !button_state )
184
relay_state = !relay_state;
116
/** Main program entry point. This routine contains the overall program flow, including initial
117
* setup of all components and the main program loop.
201
/** Main program entry point. This routine contains the overall program flow,
202
* including initial setup of all components and the main program loop.
123
/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
208
/* Create a regular character stream for the interface so that it can be
209
* used with the stdio.h functions */
124
210
CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
126
212
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
158
/** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
160
void CheckJoystickMovement(void)
162
uint8_t JoyStatus_LCL = Joystick_GetStatus();
163
char* ReportString = NULL;
164
static bool ActionSent = false;
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";
179
if ((ReportString != NULL) && (ActionSent == false))
183
/* Write the string to the virtual COM port via the created character stream */
184
fputs(ReportString, &USBSerialStream);
186
/* Alternatively, without the stream: */
187
// CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
242
// set up arduino wiring.c
192
246
/** Event handler for the library USB Connection event. */
193
247
void EVENT_USB_Device_Connect(void)