|
ICM / NotesWeek6
Intro to Computational Media, Week 6Modified from Danny Rozin's ICM notes Libraries
Some Built in Libraries
To use a built -in library, You must first import it into your sketch by using : Sketch -> Import Library-> Library name
import processing.libraryName.*; As libraries are classes, we need to have a variable of that type to hold : Serial ourSerial;
ourSerial = new Serial(this, 9600);
x = ourSerial.read(); Contributed libraries are written by 3rd party people and are not included in the Processing installation. You can still find them in the Libraries section of processing.org Some useful libraries include : ESS and Sonia for sound, proXML for networking, JMyron for video. To add a library to your sketch:
Serial CommunicationSerial communication is the most common way computers communicate with external devices Serial communication is sometimes used inside other protocols such as USB (Universal Serial Bus), Fire Wire etc.
Asynchronous serial communication is the lowest level of communication, the data is turned into 1 and 0 and sent over a wire to the other device as pulses of electricity. In order for asynchronous communications to work between two devices, they must agree on a few things :
Finding your serial PortEven before you initialize your serial object with new, you can use the class to get a list of available ports on your computer: ourPorts= Serial.list();
To open the port and initialize the object you call new ourSerial = new Serial(this, port, speed);
WritingJust like in files, writing is easier... Serial.write(data);
Reading
Many options for reading the incoming stream:
A few more, look up in reference Before you read the buffer it is good to see if there is anything waiting there for you:
while (ourSerial.available() > 0) {
int inByte = ourSerial.read();
println(inByte);
}
If you don’t want to constantly call available() to see if there is something waiting for you, you can use the serialEvent() callBack function. void serialEvent (Serial P){} will be called whenever enough bytes are waiting in the buffer Serial ourSerial = new Serial(this, Serial.list()[0]);
ourSerial .buffer (3);
void serialEvent(Serial p) {
String inString = (ourSerial.readString());
println(inString );
}
VIDEOMost computers come with cameras or with ports that allow easy connection to a camera (FireWire, USB). The video library uses QuickTime on all platforms. On Mac QuickTime is native- on PC you need to install winVDIG from http://www.shiffman.net/vdig/WinVDIG_105.exe WinVDIG is notoriously difficult to work with. Because Processing uses quicktime for its movie Library, PC users often have trouble with this library.
To open the camera and initialize the object you call new: ourCapture = new Capture(this, width, height, FPS);
The capture object is actually a PImage object, so you can display it and access it like any image. But you have to make sure it is refreshed with new pixel values to keep the video moving. To do that you use read(): ourCapture = new Capture(this, 320, 240, 30);
void draw(){
ourCapture.read();
image(ourCapture,0,0)
}
In order not to waste too much time reading frames when the camera does not have a new frame ready yet, you can use available() ourCapture = new Capture(this, 320, 240, 30);
void draw(){
if (ourCapture.available()){
ourCapture.read();
image(ourCapture,0,0);
}
}
A different approach to knowing when a frame is ready is using the callback captureEvent() ourCapture = new Capture(this, 320, 240, 30); //in setup
void draw(){
image(ourCapture,0,0);
}
void captureEvent(Capture c) {
ourCapture.read();
}
|