Skip to content

Commit

Permalink
Added a simple Arduino example
Browse files Browse the repository at this point in the history
  • Loading branch information
siggiorn committed Jun 25, 2013
1 parent a92ace5 commit f3764a2
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions java/src/edu/mit/media/prg/arduino/ArduinoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package edu.mit.media.prg.arduino;

import java.nio.ByteBuffer;

import processing.core.PApplet;
import processing.serial.Serial;

/**
* @author siggi
* @date Jun 25, 2013
*/
public class ArduinoTest extends PApplet implements SerialPacketHandler {

ArduinoBufferedSerial arduino;
ByteBuffer sendBuffer = ByteBuffer.allocate(256);

public ArduinoTest(){
System.out.println("Available serial ports:");
String[] ports = Serial.list();
for (String port : ports) {
System.out.println(" - "+port);
}

/*
* Create serial object using Processing's serial library, choosing first serial port listed (change to what you like)
*/
PSerialDevice serial = new PSerialDevice(this, ports[0], 9600);

/*
* Create arduino object, register ourselves as a handler for incoming data
*/
arduino = new ArduinoBufferedSerial(serial, this);
}

@Override
public void setup() {
size(300, 300);
}

@Override
public void draw() {
/*
* We need to update the arduino object regularly, this is where received packets are processed etc.
*/
arduino.update();
}

/*
* Every now and then we can send an arbitrary packet to the Arduino
*/
@Override
public void keyTyped() {
sendBuffer.clear();
sendBuffer.putInt(10); // Note that int means 32-bit in Java, 16-bit in Arduino (Java int is called long in Arduino)
sendBuffer.putFloat(3.14f);
arduino.sendBuffer( sendBuffer );
}

/*
* And finally here we can read the incoming data
*/
public void handleSerialPacket(ByteBuffer bb, int length) {
System.out.println("Received packet of size: "+length);
}
}

0 comments on commit f3764a2

Please sign in to comment.