Re: [Nxtcommand-developers] Making Icommand ready for different NXTCOMM implementations
Status: Beta
Brought to you by:
bbagnall
|
From: Peter J. <ptg...@ch...> - 2006-11-11 01:54:16
|
Brian, other developers,
My mails of yesterday were written a little late.
As a result I was not that clear in my mind.
At this moment I have changed the sources in my eclipse workspace.
I have come up with the following proposal.
If you agree, than I will continue testing, and then commit my changes.
in package icommand.nxt:
I have an interface NXTComm, and 2 implementation classes NXTCommRXTX
and NXTCommBluez. I had to remove the 'static' modifier of the methods
in NXTCommRXTX in order to use the interface NXTComm.
I have added NXTComm.open() to the API. So at start of the program
NXTCommand.open() should be called, and at the end NXTCommand.close();
When I started programming I did think that I needed NXTCommand.open()
to be in the API for technical reasons. But there is no technical
reason. I you prefer I can remove this API change. Personally I prefer
to put it in the API, because to me it looks "strange" to see a close()
statement, without a preceding open() statement. Make your choice!
I still want to make icommand.nxt a non-API package someday, but for the
moment I want to leave it as is.
My 2nd goal was to remove static methods where they where not really
needed. Personally I think the static modifier is OK for defining
constants (public static final String GREET = "Hello") an so-called
utility classes (e.g. java.lang.Math). I have also removed the 'static'
modifier in the methods from NXTCommand as much as possible.
The methods open() close() and setVerify(...) I did leave static,
because they are part of the "static Lejos API".
I did make the NXTCommand class a singleton, so that it can easily
be accessed by the static Lejos API. When I say singleton, I mean
the singleton design pattern, not static methods. (There is a
difference).
In the example below you can see how the singleton is accessed
from the classes in the package icommand.patform.nxt
public class Battery implements NXTProtocol {
private static final NXTCommand nxtCommand = NXTCommand.getSingleton();
// Ensure no one tries to instantiate this.
private Battery() {}
/**
* The NXT uses 6 batteries of 1500 mV each.
* @return Battery voltage in mV. ~9000 = full.
*/
public static int getVoltageMilliVolt() {
/*
* calculation from LEGO firmware
*/
int voltage = nxtCommand.getBatteryLevel();
return voltage;
}
/**
* The NXT uses 6 batteries of 1.5 V each.
* @return Battery voltage in Volt. ~9V = full.
*/
public static float getVoltage() {
return (float)(getVoltageMilliVolt() * 0.001);
}
}
Greetings,
Peter Joosten.
|