Best Practices

This page has been updated on the 6 March 2010.

This page describes the Mobile Devices’ best practices.

Import a project

The best way to import a project with no error is to clean it before.

In a MDI project the parts needed are only:

  • src folder
  • JCPN file
  • MDU file (if user interface project)
  • style folder (if user interface project)

You can delete all other file, they will be regenerated automatically.
If you don’t clean your project, you can have problem with path of library, local settings and compilation.

After cleaning, you can import it in the SDK by using: File → Import… in the eclipse menu.

Initial class

We recommend having just one initial class by project.

We recommend also that the start() method of the initial to be a non blocking method, with no loop and wait.
Some Morpheus OS components check if the method has ran and finished properly, so an error will be generated if the start() method takes too much time to execute.

Typically, in the start() method we retrieve component instances, initialize variables and components and run a thread.

e.g.:

public class Initial implements com.mdi.tools.cpn.Initial, Runnable 
{
  private Debug  _dbg = null;

  public void start() 
  {
    _dbg = Component.getInstance().getDebug(); // Retrieve component
		
    _dbg.init(0); // initialization
		
    new Thread(this).start(); // Launching thread
  }
  
  public void run() 
  {
    _dbg.print("Thread is running");
      [...]
  }
}

BoolRef, IntRef and BuffRef class

In the framework, we have some specific class to manage simple type boolean, integer, byte, byteArray and string.
Those classes are:

  • IntRef
  • BoolRef
  • BuffRef

If you want to retrieve the value inside of an IntRef or BoolRef you must use: myData.value
“Value” is directly the value as boolean or integer.

BuffRef can manage String and ByteArray, its use is almost similar to a StringBuffer and it can manage text or binary content.
This class has also some methods to manage UTF8 buffer.

Save instance

If you use some instance of a class more than one time, a good practice is to save it in a final variable instead of calling the getInstance() method several time.

e.g.: USE

  Component cpn = Component.getInstance();
  Debug dbg = cpn.getDebug();
  Gps 	gps = cpn.getGps();

INSTEAD OF

  Debug dbg = Component.getInstance().getDebug();
  Gps 	gps = Component.getInstance().getGps();

Embedded programming

Always keep in mind that you are in an embedded software. It means that the memory and CPU time is limited.

So basically:

  • avoid multiple creation of the same object, try to use it several time
  • avoid if possible creation of object inside a loop
  • make a limitations in your loops, with temporization or blocking methods to avoid infinite loop.

e.g.:
NOT GOOD

  public void run() 
  {
    while (true)
    {
      IntRef lat = new IntRef();
      IntRef lon = new IntRef();
      IntRef spd = new IntRef();
      IntRef crs = new IntRef();

      _gps.getPosition(lon, lat, spd, crs, false);
      _dbg.print("coordinates lat="+lat.value+" lon="+lon.value);
    }		
  }

GOOD

  public void run() 
  {
    // create object outside of the loop and use it several time
    IntRef lat = new IntRef();
    IntRef lon = new IntRef();
    IntRef spd = new IntRef();
    IntRef crs = new IntRef();

    while (_run) // loop can stop
    {
      _gps.getPosition(lon, lat, spd, crs, false);
      _dbg.print("coordinates lat="+lat.value+" lon="+lon.value);
      try {Thread.sleep(1000);} // wait 1 second 
      catch (InterruptedException e) {e.printStackTrace();}
    }		
  }