|
CLASS DOCUMENTS
REPORTS & ASSIGNMENTS
CLASS CONTENT
USING THIS SITE
registered authors login here You are: (logout) For more on PMWiki, see pmwiki.org |
Arduino Xport To Processing Testerby jamie allen on Feb 11, 2007
/*
socket_to_Cobox
By Tom Igoe
combed over for sensorWorkshop by jamie allen - feb, 2006
Connects Xport and keystroke ASCII values once connected.
The xport is programmed to send a continuous 'A' value
and then echo any characters entered into the Processing window
*/
// variables for the socket connection:
Socket coboxSocket = null;
BufferedReader netInput = null; // used to read text from the Cobox
BufferedReader netInput2 = null; // used to read text from the Cobox
String netString1 = ""; // converts netInput to a string
String netString2 = ""; // converts netInput to a string
String coboxAddress = "192.168.1.103"; // fill in your Xport's IP address
PrintWriter remote = null; // used to send text to the Cobox
int remotePort = 10001; // fill in your Xport's receiving port
boolean isConnected = false; // whether we have a good connection or not
//variables for the keyboard and display:
String inputString = "Hello"; // input from the keyboard
String displayString1 = "";
String displayString2 = "";
PFont metaBold; // our display font
void setup()
{
// The font "Meta-Bold.vlw.gz" must be in the
// current sketch's data directory in order to load:
size(400, 300);
background(0,0,64);
metaBold = loadFont("FortunaDot-18.vlw");
textFont(metaBold, 18);
text(inputString, 20, 50);
displayString1 = "Trying to connect to " + coboxAddress;
text(displayString1, 20, 100);
}
void draw()
{
// open a connection to the Cobox. try once a second until connected:
while (isConnected == false) {
isConnected = connectToXport();
try { Thread.sleep(1000); } catch (InterruptedException interrupted) { }
}
try {
// update the screen:
background(0,0,64);
// update what we get from the keyboard:
text(inputString, 20, 50);
// if we're connected, update what we get from the net:
if (isConnected == true) {
if (netInput.ready() == true) {
netString1 = netInput.readLine();
netString2 = netInput.readLine();
}
}
// if the netString is longer than two bytes, then display it:
if (netString1.length() > 2 ) {
displayString1 = netString1;
}
if (netString2.length() > 2 ) {
displayString2 = netString2;
}
text(displayString1, 20, 100);
text(displayString2, 20, 150);
} catch (IOException e) { println(e);}
}
boolean connectToXport()
{
// flag to return whether we're connected or not.
// if the connection try below fails, it'll return false:
boolean gotConnected = false;
try {
// open a socket to a Cobox Micro:
coboxSocket = new Socket(coboxAddress, remotePort);
// once we have a connection, let the user know:
netString1 = "Connection accepted";
delay(1000);
// set up a bufferedReader to get stuff from the Cobox:
netInput = new BufferedReader(new InputStreamReader(coboxSocket.getInputStream()));
// set up a printWriter to send stuff to the Cobox:
remote = new PrintWriter(coboxSocket.getOutputStream());
remote.flush();
//update the state of the connection:
gotConnected = true;
} catch(IOException e) {netString1 = "Status: " + e;}
return gotConnected;
}
void keyPressed()
{
//int keyValue = Integer.parseInt("" + key);
// update the message onscreen:
inputString = str(key);
// if we're connected to the XPORT,
// then send the key string to it:
if (isConnected == true) {
remote.write(inputString);
remote.flush();
}
}
/*
//THIS IS ARDUINO CODE FOR THE ABOVE EXAMPLE
//It sends a continuous 'A', as well as echoing anything you type into
//the Processing Window created above
//connection mode on XPORT: 11000100 = C4
int potPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int incomingByte = 65; ///
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
//digitalWrite(ledPin, HIGH); // sets the LED off
//val = analogRead(potPin); // read the value from the sensor
//Serial.print("Raw Value: ");
//Serial.println(val, DEC); // print as an ASCII-encoded decimal
Serial.print("I sent: ");
Serial.println(65, BYTE);
delay(200);
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, BYTE);
}
}
*/
|