Package com.motorola.oem.hapi

HW API package - Controls GPIO and A2D HW lines.

See:
          Description

Interface Summary
A2dEventListener A listener to A2d events related to specific channel.
GpioCounterListener A listener to Gpio Counter events.
GpioInterruptListener A listener to Gpio Interrupt events.
 

Class Summary
A2dAutoPeriodConfig The A2dAutoPeriodConfig class is designed for holding the configuration profile of A2D channel to period mode.
A2dAutoThresholdConfig The A2dAutoThresholdConfig class is designed for holding the configuration profile of a2d channel to threshold mode.
A2dChannel The A2dChannel class contains methods for configuration, and receiving notifications from the A2d channels.
A2dManager The A2dManager class contains methods for initialization and management of the A2d unit.
Gpio Gpio Class is an abstract base class to support common access methods Gpio Specific Configuration:
- Available pins: GPIO1-GPIO7, GPIO9-GPIO16.
GpioCounter The class provides Gpio Counter service methods.
GpioCounterConfig The class encapsulates all Gpio Counter relevant configuration data.
GpioInput GpioInput enables configuring each available gpio in the system as an input one.
GpioInterruptConfig Encapsulates all GPIO interrupt relevant data: notification type, debounce time and listener.
GpioOutput GpioOutput class enables configuring each available GPIO in the system as output.
 

Exception Summary
A2dException A2d specific exception.
GpioException GPIO specific exception.
 

Package com.motorola.oem.hapi Description

  HW API package - Controls GPIO and A2D HW lines.
GPIO (General Purpose Input/Output) - Interact with external HW lines. Set or get the line value (0 or 1), receive interrupt notification on line value changed. Perform I/O line Real-time related configurations.

A2D (Analog to Digital) – receive notification or get the level value of Analog to Digital converters found on the device. These converters are likely to be connected to external sensors. The converters translate voltage level to digital value (0-255).

GPIO API
========
A line can be configured to serve as Input. In this case a notification will
be received when a line value is changed externally.
By configuring a GPIO line to serve as an output, the GPIO value can be set/toggled
in order to notify external devices.

For specific GPIO configuration (Available pins, Interrupts' availability, Counter availability), refer to the device's User Guide.

Basic Read/Write Example:

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.motorola.oem.hapi.GpioException;
import com.motorola.oem.hapi.GpioInput;
import com.motorola.oem.hapi.GpioOutput;

public class UserMIDlet extends MIDlet{

    private GpioInput gpioInput;
    private GpioOutput gpioOutput;

    public UserMIDlet() {
        super();
    }

    protected void startApp() throws MIDletStateChangeException {

        try {
            //Configuring GPIO1 as Input
            gpioInput = new GpioInput(1);
            gpioInput.read();

            //Configuring GPIO2 as Output
            //Writing low value to GPIO2
            gpioOutput = new GpioOutput(2);
            gpioOutput.write(false);

        } catch (GpioException e) {
            if (gpioInput != null) gpioInput.clear();
            if (gpioOutput != null) gpioOutput.clear();
            e.printStackTrace();
        }
    }

    protected void pauseApp() {

    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

    }
 }

   

Interrupt Example:

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.motorola.oem.hapi.GpioException;
import com.motorola.oem.hapi.GpioInput;
import com.motorola.oem.hapi.GpioInterruptConfig;
import com.motorola.oem.hapi.GpioInterruptListener;

public class UserMIDlet extends MIDlet implements GpioInterruptListener{

    private GpioInput gpioInput;
    private GpioInterruptConfig gpioInterruptConfig;

    public UserMIDlet() {
        super();
    }

    protected void startApp() throws MIDletStateChangeException {

        try {
            //Configuring GPIO1 as Input
            //and enabling interrupt
            gpioInput = new GpioInput(1);
            gpioInterruptConfig = new GpioInterruptConfig(this);
            gpioInput.enableInterrupt(gpioInterruptConfig);

        } catch (GpioException e) {
            if (gpioInput != null) gpioInput.clear();
            e.printStackTrace();
        }
    }

    protected void pauseApp() {

    }


    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

    }

    public void onInterrupt(int gpioNum, boolean val) {

        //Handle interrupts on GPIO1 here

    }
}
    

Counter Example:

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.motorola.oem.hapi.GpioException;
import com.motorola.oem.hapi.GpioInput;
import com.motorola.oem.hapi.GpioCounterConfig;
import com.motorola.oem.hapi.GpioInterruptConfig;
import com.motorola.oem.hapi.GpioCounterListener;

public class UserMIDlet extends MIDlet implements GpioCounterListener{

    private GpioInput gpioInput;
    private GpioCounterConfig gpioCounterConfig;

    public UserMIDlet() {
        super();
    }

    protected void startApp() throws MIDletStateChangeException {

        try {
            //Configuring GPIO14 as Input
                        //Configuring gpioCounterConfig object with requested counter configuration.
            //Enabling counter.
            gpioInput = new GpioInput(14);
            gpioCounterConfig = new GpioCounterConfig(this);
                        gpioCounterConfig.setNotificationType(GpioInterruptConfig.NOTIFICATION_TYPE_HIGH_TO_LOW);
            gpioCounterConfig.setGpioCounterExpiryValue(2500);
             gpioCounterConfig.setGpioCounterType(GpioCounterConfig.COUNTER_TYPE_ONE_SHOT);
            gpioInput.enableCounter(gpioCounterConfig);

        } catch (GpioException e) {
            if (gpioInput != null) gpioInput.clear();
            e.printStackTrace();
        }
    }

    protected void pauseApp() {

    }


    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

    }

    public void onCounterExpired(int gpioNum) {

        //Handle counter expiry on GPIO14 here

    }
}
    

Interrupt and Polling Example:

In this example we'll define two additional types: Listener (Interface) and GpioPoll (Thread extended Class).
GpioPoll will poll its GpioInput member every second, informing its listener of every polling.
We will configure GPIO1 and GPIO6 as inputs. GPIO1 will carry interrupts while GPIO6 will
be polled. Implementing Listener lets us handle updates received from the polling thread in the Midlet's main area.

import com.motorola.oem.hapi.GpioInput;

public interface Listener {

    public void update(boolean readVal, GpioInput gpioInput);

}
        

import com.motorola.oem.hapi.GpioInput;

public class GpioPoll extends Thread{

    private GpioInput gpioInput = null;
    private Listener listener;

    public GpioPoll(GpioInput gpioInput, Listener listener) {
        this.gpioInput = gpioInput;
        this.listener = listener;
    }

    public void run() {
        try {
            while(true)
            {
                if (gpioInput == null){
                    System.out.println("GpioInput cannot be null");
                    break;
                }
                if (listener == null){
                   System.out.println("Listener cannot be null");
                   break;
                }
                boolean readVal = gpioInput.read();
                listener.update(readVal,gpioInput);

                //Sleeping for one second
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
    

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.motorola.oem.hapi.GpioException;
import com.motorola.oem.hapi.GpioInput;
import com.motorola.oem.hapi.GpioInterruptConfig;
import com.motorola.oem.hapi.GpioInterruptListener;

public class UserMIDlet extends MIDlet implements GpioInterruptListener, Listener{

    private GpioInput gpioInputInterrupt;
    private GpioPoll gpioPoll = null;
    private GpioInput gpioInput6 = null;
    private boolean gpioInput6ReadVal = false;

    public UserMIDlet() {
        super();

    }

    protected void startApp() throws MIDletStateChangeException {

        try {

            //Enable GPIO1 interrupt.
            //GpioInterruptConfig constructs with Midlet itself as the interrupt listener.
            GpioInterruptConfig gpioInterruptConfig = new GpioInterruptConfig(this);
            gpioInputInterrupt = new GpioInput(1);
            gpioInputInterrupt.enableInterrupt(gpioInterruptConfig);
        } catch (GpioException e) {
            //Handle Exception 
        }

        try {
            //Configure GPIO6 as input
            gpioInput6 = new GpioInput(6);
            gpioInput6ReadVal = gpioInput6.read();

            //Start polling on GPIO6
            gpioPoll = new GpioPoll(gpioInput6,this);
            gpioPoll.start();
        } catch (GpioException e) {
            //Handle Exception 
        }

    }

    protected void pauseApp() {

    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

    }

    public void onInterrupt(int gpioNum, boolean val) {
        //Handle interrupts on GPIO1 here
    }

    public void update(boolean readVal, GpioInput gpioInput) {

        if (gpioInput == gpioInput6)
        {
            if (readVal == false && gpioInput6ReadVal == true)
            {
                //handle polling change on GPIO6
            }
            else if (readVal == true && gpioInput6ReadVal == false)
            {
                //handle polling change on GPIO6
            }

            gpioInput6ReadVal = readVal;
        }
    }
}


      

A2D API
=======
A2D API enables configuration of ADC converters to several reports' modes.
There are three types of A2d reports' modes defined:

1) Automatic notification off - Always available and doesn't need special configuration.
2) Automatic notification by period - Notifications will be executed cyclically with pre-defined period.
3) Automatic notification by threshold - Notifications will be executed when:
a. Applied voltage decreases from the level above the high threshold of low border to the level below
the low threshold of the low border;
b. Applied voltage increases from the level below the low threshold of the high border to the level above
then high threshold of the high border;

Notification types can be enabled independently by startNotification() method and
may be re-defined at any time.
If the A2d configuration was not defined, the default definition will be taken instead - auto notification off.

Limitations:
1) Period must be define in multiple of 4, for example 4,8,12,...,100,..(Minimum period value is 4 sec).
2) Threshold notification resolution is 4 second.
3) Threshold resolution is 255 units. When 0 represent 0 volt and 255 represent 2.3 volt.

Basic query Example:

  

import javax.microedition.midlet.*;

import com.motorola.oem.hapi.A2dException;
import com.motorola.oem.hapi.A2dManager;
import com.motorola.oem.hapi.A2dChannel;

public class UserMidlet extends MIDlet {

   private A2dManager a2d = null;
   private int queryValue = 0;

    public UserMidlet() {
        super();
    }

   public void startApp() {

            try
            {
                a2d = A2dManager.getInstance();
                queryValue = a2d.getChannel(a2d.CH1).getValue();
            } 
            catch(A2dException e)
            {
                //Handle Exception 
            }
            catch(InterruptedException e)
            {
                //Handle Exception  
            }
        }
   public void pauseApp() {
    }

   public void destroyApp(boolean unconditional) {
    }

}
        

Basic Threshold mode Example:

  

import javax.microedition.midlet.*;

import com.motorola.oem.hapi.A2dException;
import com.motorola.oem.hapi.A2dManager;
import com.motorola.oem.hapi.A2dEventListener;
import com.motorola.oem.hapi.A2dChannel;
import com.motorola.oem.hapi.A2dAutoThresholdConfig;

public class UserMidlet extends MIDlet implements A2dEventListener{

    private A2dManager a2d = null;;
    private A2dAutoThresholdConfig threshold_config = null;

    public UserMidlet() {
        super();
    }

   public void startApp() {

            try
            {
                A2dManager a2d = A2dManager.getInstance();
                A2dAutoThresholdConfig threshold_config = new A2dAutoThresholdConfig();

                threshold_config.setThreshold(threshold_config.THRESHOLD_LOW_LIMIT_LOW,(short)(115));
                threshold_config.setThreshold(threshold_config.THRESHOLD_HIGH_LIMIT_LOW,(short)(135));
                threshold_config.setThreshold(threshold_config.THRESHOLD_LOW_LIMIT_HIGH,(short)(155));
                threshold_config.setThreshold(threshold_config.THRESHOLD_HIGH_LIMIT_HIGH,(short)(175));

                                a2d.getChannel(a2d.CH1).setListener(this);
                                a2d.getChannel(a2d.CH1).startNotification(threshold_config);
            } 
            catch(A2dException e)
            {
                //Handle Exception 
            }
        }
   public void pauseApp() {
    }

   public void destroyApp(boolean unconditional) {
    }

   public void onNotification(int reportType,int channelNum,int a2dValue){
        //Handle notification on A2d channel            
   }

}
        

Basic period mode Example:

  
import javax.microedition.midlet.*;

import com.motorola.oem.hapi.A2dException;
import com.motorola.oem.hapi.A2dManager;
import com.motorola.oem.hapi.A2dEventListener;
import com.motorola.oem.hapi.A2dChannel;
import com.motorola.oem.hapi.A2dAutoPeriodConfig;
import com.motorola.oem.hapi.A2dAutoThresholdConfig;

public class UserMidlet extends MIDlet implements A2dEventListener{

    private A2dManager a2d = null;
    private A2dAutoThresholdConfig threshold_config = null;      

         public UserMidlet() {
        super();
     }

     public void startApp() {

            try
            {
                A2dManager a2d = A2dManager.getInstance();
                A2dAutoPeriodConfig period_config = new A2dAutoPeriodConfig();

                period_config.setPeriod(8);

                                a2d.getChannel(a2d.CH1).setListener(this);
                                a2d.getChannel(a2d.CH1).startNotification(period_config);
            } 
            catch(A2dException e)
            {
                //Handle Exception 
            }
        }
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void onNotification(int reportType,int channelNum,int a2dValue){
        //Handle notification on A2d channel                    
   }
}