A GUI (graphical user interface) library for processing.
ControlP5 cp5; void setup() { cp5 = new ControlP5(this); }
cp5.addTextlabel("color_label") .setText("Change Color:" ) .setPosition(10, 10) .setColorValue(0x000000) .setFont(createFont("Georgia",12)) ;
cp5.addButton("Red") .setPosition(10, 25) .setSize(100, 19) ;
Callback (I):
public void controlEvent(ControlEvent event) { if (event.getName().equals("Red")) { // Do something here: } }
Callback (II):
void Red() { // Do something here: }
cp5.addSlider("RotateY") .setPosition(10,240) .setSize(200,15) .setRange(-180,180) .setValue(0) ;
Callback (I):
public void controlEvent(ControlEvent event) { if (event.getName().equals("RotateY")) { // Get the slider value: float rotateVal = event.controller().value(): } }
Callback (II):
void RotateY(float val) { // Do something here: }
background(255); // Set the background to white color noStroke(); // Set stroke (no stroke) fill(255, 200, 0); // Set the filled color rect(posX, 40, 50, 50, 8); // Draw a rect
PImage testImage; // NOTE: source should be placed under ./data testImage = loadImage("test.jpeg"); size(200, 200); background(255); // image(PImage, posX, posY, width, height) image(testImage, 10, 10, 190, 190);
void setup() { // Init stuff here: } void draw() { // Loop Loop Loop }
// mouse position: (mouseX, mouseY) void mousePressed() {} void mouseReleased() {} void mouseDragged() {}
public void keyPressed() { switch (keyCode) { case RIGHT: // RIGHT key pressed break; case LEFT: // LEFT key pressed break; case UP: // UP key pressed break; case DOWN: // DOWN key pressed break; default: println(key); break; } }
Coordinate
Rendering Mode
// Using P3D instead of the default JAVA2D size(400, 400, P3D)
// Box box(size); box(w_x, w_y, w_z); // Sphere sphere(r); sphereDetail(numOfVertex); // min value = 3;
translate(150, 150, 100); pushMatrix(); // Push the current matrix into the stack noFill(); rotateY(PI/1.5); rotateX(map(mouseY, 0, height, PI, -PI)); box(50); popMatrix(); // Pop out the previous (before rotation) matrix rotateY(map(mouseX, 0, width, -PI, PI)); fill(255, 0, 0); box(30);
import processing.serial.*; // Import the serial library Serial port; void setup() { // List all the available serial ports: println(Serial.list()); port = new Serial(this, Serial.list()[0], 9600); }
Processing
void draw() { while(port.available() > 0) { int data = port.read(); println(data); // Do something } }
Arduino
void loop() { int switchState = digitalRead(switch); if (switchState == HIGH) { Serial.write(1); } else { Serial.write(0); } }
Processing
if (switchVal == true) { port.write(1); } else { port.write(0); } }
Arduino
if (Serial.available() > 0) { readData = Serial.read(); } if (readData == 1) { digitalWrite(led, HIGH); delay(1000); digitalWrite(led, LOW); delay(1000); } else { digitalWrite(led, LOW); }
*Note: Can only run on v1.5.1
/* import the required libs */ import processing.serial.*; import cc.arduino.*; Arduino arduino; int ledPin = 13; int swtPin = 4; void setup() { // Setup the Arduino: println(Arduino.list()); arduino = new Arduino(this, Arduino.list()[0], 57600); // Setup the PINs: arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.pinMode(swtPin, Arduino.INPUT); }
// Digital Read: int swtState = arduino.digitalRead(swtPin); if (swtState == Arduino.HIGH) { // Digital Write: arduino.digitalWrite(ledPin, Arduino.HIGH); delay(1000); arduino.digitalWrite(ledPin, Arduino.LOW); delay(1000); } else { arduino.digitalWrite(ledPin, Arduino.LOW); }