Page 155 - Hacking Roomba
P. 155
136 Part II — Fun Things to Do
Using RoombaComm in Processing
Processing can use libraries created by third parties. There are no restrictions on who can create
a Processing library, and there are few restrictions on what a Processing library can do. Most
importantly, Processing libraries can contain native code. Although RoombaComm is 100 percent
Java, it uses the RXTX serial library, which does contain native code.
Packaging RoombaComm
RoombaComm the API library is just a collection of Java class files in a directory. This is fine
for development and testing, but when deploying code for use elsewhere, it helps to have some
sort of packaging system to treat the collection as a unit. With Java, that packaging system is a
JAR file. And with Processing, the packaging system is a zipped directory containing JAR files.
Creating a Java Archive
Before RoombaComm can be used as a Processing library, it must first be made into a Java
Archive (JAR) file. A JAR file is created with the jar command and its use is similar to the
zip or tar command-line programs. To create a JAR file for the RoombaComm code, get a
command-line prompt and in the directory above the roombacomm directory type:
% jar -cfm roombacomm.jar packaging/Manifest.txt roombacomm
That command creates the JAR file roombacomm.jar using the manifest file Manifest.txt
and packages up the roombacomm directory. A manifest is an optional part of a JAR file that
specifies any configuration or meta-information about the JAR file. In this case, it specifies that
it needs the RXTXComm.jar file.
The preceding command is encapsulated in a script called build-jar.sh located in the root
directory of the RoombaComm software.
A JAR file is just a ZIP/PKZIP-formatted file. It can be inspected with zip/unzip or any
program that can read ZIP files. The jar program is essentially a cross-platform version of
zip that also includes some extra options to process manifest files. Some developers like to
include the dependent JAR files into the main JAR file. This makes for easy distribution, but it
also makes updates of the dependent JAR files difficult. Since Processing allows multiple JAR
files in its library, roombacomm.jar contains only the RoombaComm code, and the RXTX
library is kept as its own JAR file.
Making a Processing Library
With RoombaComm packaged as a JAR file, a Processing library can be created. In the
Processing root directory, there is a sub-directory called libraries. All libraries go here, both
ones that come with Processing and any third-party libraries you install. Processing expects
libraries to conform to a directory standard and have certain files present. When RoombaComm
is installed in Processing, the directory structure looks like Listing 7-2. There are several
libraries already in Processing, like for OpenGL or network access.