|
InputDevices© Justin Couch 2000An input device consists of the lowest level interface with the data source. It processes the raw input and fills in the sensor information. An input device can provide information to the sensors in one of three fashions:
Generally speaking, you will need some sort of custom device driver for every
input device and more often than not, that involves native code. For some
environments, you may need to do no more than query the operating system
specific driver capabilities. Under Win32, you may only need a thing layer to
the
No matter what the source of the device information, you will need to
implement the
Writing InputDevicesAn input device deals with several different needs at once. On one hand it must take the input from the real piece of hardware (or software) and on the other, it must supply information on demand to the runtime environment. With the way that Java3D deals with devices, so long as you implement theInputDevice interface, it doesn't really care how you structure
your code. In our simple example it is only one file, but for more complex
cases you may need a whole separate package as well as native code.
Before we start on writing an actual device, we need to quickly look at how
it gets added to the environment. This may appear a strange on the surface,
but it will help explain a few of the things that we will do later. As we
mentioned in the previous paragraph, so long as the interface is implemented,
we can do what we like with the rest of the code. Also, back in
Now, getting back to the implementation of the device, there are three things that the code needs to achieve:
In the Because we might have more than one instance of the device loaded into the application, the constructor takes an integer parameter to tell it what instance number it is:
private static final String FILE_NAME = "position_inputX.dat";
private String config_file;
public FileInputDevice(int deviceNumber)
{
char[] index_char =
Integer.toString(deviceNumber).toCharArray();
config_file = FILE_NAME.replace('X', index_char[0]);
}
This just takes the standard pattern of the input file name and replaces X
for the instance number. The obvious restriction is that there can be a
maximum of only ten different devices (0-9).
At the point where Java 3D is ready to use your input device, it calls the
public boolean initialize()
{
boolean init = false;
try
{
InputStream is =
ClassLoader.getSystemResourceAsStream(config_file);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
StreamTokenizer strtok = new StreamTokenizer(reader);
strtok.commentChar('#');
strtok.eolIsSignificant(false);
strtok.parseNumbers();
...
}
return init;
}
Note that the method returns a boolean that indicates if the process actually
worked. If it returns false then Java 3D won't use that input device. Hence
the reason for the init variable.
Last on the list of code to implement is processing the input from the device when it is needed. When the pollAndProcessInput method is called, it is Java 3D telling your class to grab a new lot of input from the real device and make it available to be read. You don't have to produce data at any other time if you don't want to. Because we are interpolating the data, we don't pre-calculate what the outputs may be. We generate information based on the current system time:
public void pollAndProcessInput()
{
long current_time = System.currentTimeMillis();
// do the processing here and setup values to be returned
}
That's all the input device needs to do. The next step is to implement the
sensors because that is what Java 3D interacts with to get the real values.
|
|
[ j3d.org ]
[ Aviatrix3D ]
[ Code Repository ]
[ Java3D ]
[ OpenGL ]
[ Books ]
[ Contact Us ]
Hosted by Yumetech Last Updated: $Date: 2006/04/18 18:20:20 $ |