ProcessingFastPickup



ProcessingFastPickup

0 0


ProcessingFastPickup


On Github benjamin99 / ProcessingFastPickup

Processing

http://processing.org/

What is Processing?

Open Source Java Based Arduino

ControlP5

A GUI (graphical user interface) library for processing.

Link: http://www.sojamo.de/libraries/controlP5

Install

Setup ControlP5

ControlP5 cp5;

void setup() {
    cp5 = new ControlP5(this);
}
		

Add Controls: Text Label

cp5.addTextlabel("color_label")
   .setText("Change Color:" )
   .setPosition(10, 10)
   .setColorValue(0x000000)
   .setFont(createFont("Georgia",12))
   ;
		

Add Controls: Button

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:
}
		

Add Controls: Slider

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:
}
		

Draw Functions

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
		

Image

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);   		
		

Setup & Draw

void setup() {
// Init stuff here:
}

void draw() {
// Loop Loop Loop 
}
		

Mouse Event

// mouse position: (mouseX, mouseY)
void mousePressed()  {} 
void mouseReleased() {}
void mouseDragged()  {}
		

Key Event

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;
	}		
}
		

3D: Basics

Coordinate

Rendering Mode

// Using P3D instead of the default JAVA2D	
size(400, 400, P3D)		
		

3D: Object

// Box
box(size);
box(w_x, w_y, w_z);

// Sphere
sphere(r);
sphereDetail(numOfVertex); // min value = 3;

		

3D: Transform

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);
		

Arduino

Testing Circuit

Serial Setup

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);
}
		

Serial Communication (input)

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);
	}
}
		

Serial Communication (output)

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);
}			
		

Arduino Library

http://playground.arduino.cc/interfacing/processing

*Note: Can only run on v1.5.1

Arduino Lib: Setup

/* 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);  
}
		

Arduino Lib: Controls

// 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);
}