import controlP5.*;
import processing.serial.*;

ControlP5 cp5;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

float links;
float rechts;
Slider slLinks;
Slider slRechts;

void setup() {
  size(450, 300);
  frameRate(100);
  background(0);
  
  cp5 = new ControlP5(this);

  slLinks = cp5.addSlider("links")
     .setRange((float)-1, (float)1)
     .setValue(0)
     .setPosition(10,10)
     .setSize(400,20)
     ;

  
  slRechts = cp5.addSlider("rechts")
     .setRange((float)-1,(float)1)
     .setValue(0)
     .setPosition(10,40)
     .setSize(400,20);
     
  cp5.addButton("stop")
      .setPosition(10, 80)
      .setSize(150, 150);

    
  myPort = new Serial(this, "/dev/ttyUSB0", 115200);
}

void stop() {
  slLinks.setValue(0);
  slRechts.setValue(0);
}

void draw() {
  
  int bits = Float.floatToIntBits(rechts);
  byte[] bytes = new byte[8];
  bytes[0] = (byte)(bits & 0xff);
  bytes[1] = (byte)((bits >> 8) & 0xff);
  bytes[2] = (byte)((bits >> 16) & 0xff);
  bytes[3] = (byte)((bits >> 24) & 0xff);
  
  bits = Float.floatToIntBits(links);
  bytes[4] = (byte)(bits & 0xff);
  bytes[5] = (byte)((bits >> 8) & 0xff);
  bytes[6] = (byte)((bits >> 16) & 0xff);
  bytes[7] = (byte)((bits >> 24) & 0xff);
  
  myPort.write(bytes);              // send an H to indicate mouse is over square
  
  
  String inBuffer = myPort.readString();   
  if (inBuffer != null) {
    println(inBuffer);
  }
  
}