Processing is a programming language built upon yet another language called Java, which was developed primarily to be a network-oriented language. It was developed to work over the Internet and is, therefore, a good medium for transferring data or invoking graphics on remote computers. So far, all the graphics examples you’ve seen demonstrated were designed to work on a local machine. But what if you want to run something on a remote machine? For example, moving the mouse on one computer and seeing its movement on the screen of another computer.
The process of communication in Processing or Java is based on the client- server protocol: one computer plays the role of the client who asks for informa- tion and another computer plays the role of the server, which provides information in response to the client’s request. The roles can be reversed, that is, both can send/receive information, but normally the server holds the information and
has the capacity to serve it to multiple clients through an identifiable address.
Specific to the World Wide Web, a web server is the computer program that serves requested HTML pages or files. A web client is the program that requests information from the server. A web browser is a client that requests HTML files from web servers.
Every computer that is connected to the Internet has an address, in order to be identified. This address is called an IP (Internet Protocol) address and is a 32-bit number. The IP address is usually expressed as four decimal numbers, each representing 8 bits, separated by periods. The format is “network.network.
network.local”. The number version of the IP address is represented by a name or series of names called the domain name. Here’s an example:
130.5.5.25
Each number must be between 0 and 255 (i.e., a byte). The IP address 127.0.0.1 is the address of the local machine itself. Each of the decimal numbers represents a string of four binary digits. Thus, the above IP address really is this string of 0s and 1s:
10000010.00000101.00000101.00011001
As you can see, periods are inserted between each 8-bit sequence just as was done for the decimal version of the IP address. To establish a server in Processing is simple: you construct a server through the following statement:
Server MyServer = new Server(this, 5200);
A Server is a Processing class that waits for request over a network. The number 5200 is the port where the server is listening. Following is the code for establishing a server. The purpose of this server is to send to the client its mouse’s x and y coordinates.
1 import processing.net.*; //get the net library 2
3 Server MyServer; //define a server 4 void setup(){
5 size(300,300);
6 MyServer = new Server(this, 5200);
7 }
8 void draw(){
9 point(mouseX,mouseY); //draw a point 10 MyServer.write(mouseX); //send x
11 MyServer.write(mouseY); //send y 12 }
In the first line of code, you import all the commands included in the net library of processing. Then you define a Server object called MyServer, which creates a server in line 6. You then draw points on the screen and use the server to send data out to the Internet (i.e., to whomever is receiving it), using the write() methods seen in lines 10 and 11.
To establish a client in Processing is also simple construct a server through the following statement:
Client MyClient = new Client(this, “127.0.0.1”, 5200);
A Client is a Processing class that sends requests over a network. To establish a client, you need to pass the address and port of the server to connect to. (Note that if you do not know the IP address’s number you can use the ip() method of the client’s class. It returns the IP address of the client as a string. As default value, we pass the address “127.0.0.1,” which refers to the local computer itself).
The number 5200 is the port where the server is listening. If you know the IP address of the server, you can type it here instead of the local number. To find the address of a machine you go to the command line prompt (in Windows), and type the command ipconfig:
Figure 10-10: The ipconfig command
Following is the code for establishing a server. The purpose of this client is to receive the mouse’s x and y coordinates of the server.
1 import processing.net.*; //get the net library 2
3 Client MyClient; //define a client 4 void setup(){
5 size(300,300);
6 MyClient = new Client(this, “127.0.0.1”, 5200);
7 }
8 void draw(){
9 if(MyClient.available()>0){ //if data are available 10 int x = MyClient.read(); //get the x-coordinate 11 int y = MyClient.read(); //get the y-coordinate 12 rect(x,y,5,5); //draw a rect at xy 13 println(x+”, “+y); //print out coordinates 14 }
15 }
In principle, the client code is almost the opposite of the server code. Here, you receive data from a connected server. To do that, first you create a client using the Client() command that takes as parameters the parent object (indi- cated by the “this” expression), the server’s IP address, and the specific port.
In this case, you are using the IP address 127.0.0.1, which is the computer you are working on. In line 9 of the code you use the available() command to detect whether or not the server is sending data. If it is true (i.e., if it is not 0), then start to read the data values as they come in. In this case, you know that data comes in pairs, so you sequentially assign the odd data as x and the even as y. In more complicated cases, you may want to export an indicator on the server side to be used by the client as an identifier of what or how many data values are read in. For example, the server could be sending the string “x = 10”
so the client would split the string based on the = symbol to determine that 10 is indeed the x coordinate.
To run a client/server program, you need to first run the server code to make sure that the server is up and waiting for clients. Then you run the client. As the server is waiting for connections, a client sends a request that is received by the server and the communication process begins. Once the first connection is established from a new client, a continuous communication stream is established by repeatedly sending/receiving information, allowing data to flow back and forth. In the example, the server is sending x and y mouse coordinates, and the client is seeing drawn on the screen small 5 × 5 rectangles that show the coor- dinates sent by the server.
The number 5000 stands for the port where the data will be sent or received.
You can have multiple ports at the same time, since you can have multiple com- munications with other servers at the same time. In this case, you are using only one port, that is, the one numbered 5000.
Once you establish the connection with the server, you create an input stream and receive the data, that is, the mouseX and mouseY mouse coordinates.
Remember that in the server’s code, we used the write() method, which uses the output stream to send out data:
MyServer.write(mouseX);
MyServer.write(mouseY);
On the client’s side a similar process is going on, except there you loop con- tinuously, receiving data. The client needs to be in a constant state of waiting because it does not know when a request may come in. This is done through the line:
if(MyClient.available()>0)
which becomes true only if the server sends positive data values. Once an input stream is established, you read the data through the statements:
int x = MyClient.read();
int y = MyClient.read();
Input integers are assigned to the x and y variables that are used in the rect() method to draw a rectangle. The result of this process is shown in Figure 10-11.
Figure 10-11: A client-server real-time interaction using graphics
For more information on the Java network objects and methods, look at the official Java site at http://java.sun.com.
Summary
You have been introduced to the process of writing out and reading in files.
Specifically, you have learned about the basic structure of two common graphics file formats called DXF and VRML. So, now you are able to import and export DXF and VRML files. This is an important task, because it allows you to interact with many computer graphics applications in their file format.
Exercises
1. Find information about a file format called RIB. Write the import/export methods that would handle .rib files.
2. Without the expand() and append() array methods, how would you modify the openDXF_3DFACE() method to read DXF files? (The problem you are faced with is that you do not know in advance the amount of data you will find in the DXF file.)
3. Find more information about the STL (Stereo Lithography) file format.
Write the method that would allow to import and STL files.
4. Following are the contents of a text file called “positions.txt”:
Write the code that will open the text file and draw on the screen a series of 10 × 10 rectangles each located at the file’s data coordinates.
5. Write code that will open a text file and shuffle its contents as shown in the following figure:
275 Physical computing is a term used to denote the use of physical devices to carry out computational processes. In its simplest form, it involves circuits with sen- sors and actuators driven by microcontroller boards.
Sensors are devices that sense information from the immediate environment, such as photocells that sense light intensity, thermistors that sense temperature, microphones that sense sound variations, and many more. One general clas- sification of sensors is as either analog or digital, that is, whether they return a spectrum of variations or an on/off status. For example, a pushbutton is an on/
off sensor that can sense whether it is being pressed or not. In contrast, a dial can reflect a spectrum of variations, depending on the rotation of the dial.
Actuators are devices that produce an action in their immediate environment, such as an LED (light emitting diode) a device that emits light, a motor that produces motion, or a speaker that vibrates sound. As in the case of sensors, one general categorization is as either analog or digital. For instance, an LED is a device that can either be on or off, whereas a motor can turn with variable degrees of speed or power output.
A microcontroller board is a computing device that allows, apart from arithmetic and logical operations, the input or output of information coming from sensors or actuators. In this chapter, we will examine the Arduino microcontroller, which runs a language similar to Processing. The objective is to mix Processing code with Arduino commands in order to extend the possibilities into physical computing. In this chapter, we will introduce the basics of electrical circuits and
11
Physical Computing
then introduce the Arduino microcontroller and its programming language.
The purpose is to show how to use, connect, and control devices in terms of responses, feedback, and multiple feedback systems.