123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- import processing.serial.*;
- import hypermedia.net.*;
- import controlP5.*;
- ControlP5 cp5;
- DropdownList serialddl;
- DropdownList baudddl;
- Textlabel arduinoLabel;
- Textlabel UDPLabel;
- Textlabel incomingPacket;
- Button startButton;
- Button stopButton;
- Textfield ipAddressField;
- Textfield incomingPortField;
- Textfield outgoingPortField;
- void setupGUI() {
-
- cp5 = new ControlP5(this);
-
- startButton = cp5.addButton("START")
- .setPosition(200, 200)
- .setSize(200, 19)
- ;
-
- stopButton = cp5.addButton("STOP")
- .setPosition(200, 200)
- .setSize(200, 19)
- ;
- stopButton.hide();
-
- serialddl = cp5.addDropdownList("SerialPort")
- .setPosition(50, 100)
- .setSize(200, 200)
- ;
- serialddl.setItemHeight(20);
- serialddl.setBarHeight(15);
- serialddl.setCaptionLabel("SELECT ARDUINO SERIAL PORT");
-
-
-
-
- String SerialList[] = Serial.list();
- for (int i=0;i<SerialList.length;i++) {
- String portName = SerialList[i];
- serialddl.addItem(portName, i);
- }
-
-
- baudddl = cp5.addDropdownList("BaudRate")
- .setPosition(50, 50)
- .setSize(200, 200)
- ;
- baudddl.setItemHeight(20);
- baudddl.setBarHeight(15);
- baudddl.setCaptionLabel("SELECT THE BAUD RATE");
-
-
-
-
- for (int i=0;i<serialRateStrings.length;i++) {
- String baudString = serialRateStrings[i];
- baudddl.addItem(baudString, i);
- }
-
-
- ipAddressField = cp5.addTextfield("IP address")
- .setPosition(300, 30)
- .setAutoClear(false)
- .setText(ipAddress)
- ;
- incomingPortField = cp5.addTextfield("Incoming Port Number")
- .setPosition(300, 80)
- .setAutoClear(false)
- .setText(str(inPort))
- ;
- outgoingPortField = cp5.addTextfield("Outgoing Port Number")
- .setPosition(300, 130)
- .setAutoClear(false)
- .setText(str(outPort))
- ;
-
- arduinoLabel = cp5.addTextlabel("arduinoLabel")
- .setText("Serial")
- .setPosition(50, 10)
- .setColorValue(0xffffff00)
- .setFont(createFont("SansSerif", 11))
- ;
- UDPLabel = cp5.addTextlabel("UDPLabel")
- .setText("UDP")
- .setPosition(300, 10)
- .setColorValue(0xffffff00)
- .setFont(createFont("SansSerif", 11))
- ;
- incomingPacket = cp5.addTextlabel("incomingPacketLabel")
- .setText("Incoming Packet")
- .setPosition(210, 100)
- .setColorValue(0xffffff00)
- .setFont(createFont("SansSerif", 10))
- ;
- incomingPacket.hide();
- }
- void controlEvent(ControlEvent theEvent) {
- String eventName = theEvent.getName();
- if (theEvent.isGroup()) {
- if (eventName == "SerialPort") {
-
- serialListNumber = int(theEvent.getValue());
- }
- else if (eventName == "BaudRate") {
- int index = int(theEvent.getValue());
- baud = Integer.parseInt(serialRateStrings[index]);
- }
- else {
- }
- }
- else if (theEvent.isAssignableFrom(Textfield.class)) {
- if (eventName == "IP address") {
- ipAddressField.setFocus(false);
- ipAddress = theEvent.getStringValue();
- }
- else if (eventName == "Incoming Port Number") {
- incomingPortField.setFocus(false);
- inPort = Integer.parseInt(theEvent.getStringValue());
- }
- else if (eventName == "Outgoing Port Number") {
- outgoingPortField.setFocus(false);
- outPort = Integer.parseInt(theEvent.getStringValue());
- }
- }
- }
- boolean applicationRunning = false;
- public void START(int theValue) {
- setupUDP();
- setupSerial();
- hideControls();
- applicationRunning = true;
- }
- void hideControls() {
- serialddl.hide();
- baudddl.hide();
- startButton.hide();
- outgoingPortField.hide();
- incomingPortField.hide();
- ipAddressField.hide();
- incomingPacket.show();
-
- stopButton.show();
- }
- void showControls() {
- serialddl.show();
- baudddl.show();
- startButton.show();
- outgoingPortField.show();
- incomingPortField.show();
- ipAddressField.show();
- incomingPacket.hide();
-
- stopButton.hide();
- }
- public void STOP() {
- stopSerial();
- stopUDP();
- showControls();
- applicationRunning = false;
- }
- Serial serial;
- String[] serialRateStrings = {
-
- "19200", "28800", "38400", "57600", "115200", "230400", "345600", "460800"
- };
- int baud = 460800;
- int serialListNumber = 0;
- ArrayList<Byte> serialBuffer = new ArrayList<Byte>();
- void setupSerial() {
- serial = new Serial(this, Serial.list()[serialListNumber], baud);
- }
- void stopSerial() {
- serial.stop();
- }
- void serialEvent(Serial serial) {
-
-
- while (serial.available () > 0) {
- slipDecode(byte(serial.read()));
- }
- }
- void SerialSendToUDP() {
- byte [] buffer = new byte[serialBuffer.size()];
-
- for (int i = 0; i < serialBuffer.size(); i++) {
- buffer[i] = serialBuffer.get(i);
- }
-
- UDPSendBuffer(buffer);
-
- serialBuffer.clear();
-
- drawIncomingSerial();
- }
- void serialSend(byte[] data) {
-
- for (int i = 0; i < data.length; i++){
- slipEncode(data[i]);
- }
-
- serial.write(eot);
- }
- byte eot = byte(192);
- byte slipesc = byte(219);
- byte slipescend = byte(220);
- byte slipescesc = byte(221);
- byte previousByte;
- void slipDecode(byte incoming) {
- byte previous = previousByte;
- previousByte = incoming;
-
- if (previous == slipesc) {
-
- if (incoming==slipescend) {
- serialBuffer.add(eot);
- }
- else if (incoming==slipescesc) {
- serialBuffer.add(slipesc);
- }
- }
- else if (incoming==eot) {
-
-
- SerialSendToUDP();
- }
- else {
- serialBuffer.add(incoming);
- }
- }
- void slipEncode(byte incoming) {
- if(incoming == eot){
- serial.write(slipesc);
- serial.write(slipescend);
- } else if(incoming==slipesc) {
- serial.write(slipesc);
- serial.write(slipescesc);
- } else {
- serial.write(incoming);
- }
- }
- UDP udp;
- int inPort = 9000;
- int outPort = 10001;
- String ipAddress = "192.168.0.12";
- void setupUDP() {
- udp = new UDP( this, inPort );
- udp.log( true );
- udp.listen( true );
- }
- void stopUDP() {
- udp.close();
- }
- void UDPSendBuffer(byte[] data) {
- udp.send( data, ipAddress, outPort );
- }
- void receive( byte[] data) {
- drawIncomingUDP();
-
- serialSend(data);
- }
- void setup() {
-
- size(550, 250, P3D);
- frameRate(30);
- setupGUI();
- }
- void draw() {
- background(128);
- if (applicationRunning) {
- drawIncomingPackets();
- }
- }
- int lastSerialPacket = 0;
- int lastUDPPacket = 0;
- void drawIncomingPackets() {
-
- fill(0);
- rect(75, 50, 100, 100);
-
- rect(325, 50, 100, 100);
- int now = millis();
- int lightDuration = 75;
- if (now - lastSerialPacket < lightDuration) {
- fill(255);
- rect(85, 60, 80, 80);
- }
- if (now - lastUDPPacket < lightDuration) {
- fill(255);
- rect(335, 60, 80, 80);
- }
- }
- void drawIncomingSerial() {
- lastSerialPacket = millis();
- }
- void drawIncomingUDP() {
- lastUDPPacket = millis();
- }
|