Skip Headers
Oracle® Java ME Embedded Device Access API Guide
Release 3.3
E35134-02
  Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
 
Next
Next
 

2 Analog-to-Digital Converter

The com.oracle.deviceaccess.adc package contains interfaces and classes for reading analog inputs using an Analog-to-Digital Converter (ADC). An ADC converts a continuous physical input quantity, such as a voltage, to a digital number. The conversion involves quantization of the input, so it typically introduces a small amount of error.

One ADC converter can have several channels. Each channel can sample a continuous input voltage and convert it to a numerical value. In order to access and control a specific ADC channel, an application should first open and obtain an ADCChannel instance for the ADC channel using its numerical ID, name, type (interface) and/or properties.

ADC channels are opened by invoking one of the PeripheralManager.open() methods. This is an example of using its ID.

 ADCChannel channel = (ADCChannel) PeripheralManager.open(8);

This is an example of using its name and interface.

 ADCChannel channel = (ADCChannel) PeripheralManager.open("TEMPERATURE",
     ADCChannel.class, null);
 

Once the peripheral is opened, the application can read or monitor sampled input values using methods of the ADCChannel interface, such as the getValue() method:

 int temp = channel.getValue();
 

When done, the application should call the ADCChannel.close() method to release ADC channel.

 channel.close();

Example 2-1 demonstrates two ways of using the ADC APIs.

Example 2-1 Using the ADC APIs

import com.oracle.deviceaccess.PeripheralManager;
import com.oracle.deviceaccess.PeripheralNotAvailableException;
import com.oracle.deviceaccess.PeripheralNotFoundException;
import com.oracle.deviceaccess.adc.ADCChannel;
import com.oracle.deviceaccess.adc.AcquisitionEvent;
import com.oracle.deviceaccess.adc.AcquisitionListener;
import java.io.IOException;
 
class ADCAcquisition implements AcquisitionListener {
 
     private ADCChannel channel = null;
 
     public void start(int channelID) throws IOException,
         PeripheralNotAvailableException, PeripheralNotFoundException
     {
         channel = (ADCChannel) PeripheralManager.open(channelID);
         channel.setSamplingInterval(100); // every 100 milliseconds
         int[] values = new int[10];
         channel.startAcquisition(values, 0, values.length, false, this);
     }
 
     public void inputAcquired(AcquisitionEvent event) {
         for (int i = 0; i < event.getCount(); i++) {
             int value = event.getValues()[event.getOffset() + i];
             // Handle value...
         }
     }
 
     public void stop() throws IOException, PeripheralNotAvailableException {
         if (channel != null) {
             channel.stopAcquisition();
             channel.close();
         }
     }
 }


class ADCThreshold implements MonitoringListener {

     private ADCChannel channel = null;

     public void start(int channelID, int low, int high)
         throws IOException, PeripheralNotAvailableException,
             PeripheralNotFoundException {
         channel = (ADCChannel) PeripheralManager.open(channelID);
         channel.setSamplingInterval(100); // every 100 milliseconds
         channel.startMonitoring(low, high, this);
     }
 
     public void thresholdReached(MonitoringEvent event) {
         if (event.getType() == MonitoringEvent.OUT_OF_RANGE) {
             int value = event.getValue();
             // Handle condition...
         }
     }
 
     public void stop() throws IOException, PeripheralNotAvailableException {
         if (channel != null) {
             channel.stopMonitoring();
             channel.close();
         }
     }
 }

Note that the com.oracle.deviceaccess.adc permission allows access to be granted to ADC channels as a whole. This permission must be requested in the JAD file under MIDlet-Permissions or MIDlet-Permissions-Opt, and the application must be digitally signed by a trusted authority to gain access to the ADC APIs. Alternatively, the permission may be allowed for all applications in the untrusted domain of the security policy file (policy.txt).

Because of performance issue, any procedures that handle analog inputs, and especially event listeners, should be optimized to be as fast as possible.

The AcquisitionListener Interface

The AcquisitionListener interface contains methods for being notified of the availability of sampled values. An AcquisitionListener can be registered using the ADCChannel.startAcquisition(int[], int, int, boolean, com.oracle.deviceaccess.adc.AcquisitionListener) method.

The AcquisitionListener interface contains only one method.

The ADCChannel Interface

The ADCChannel interface provides methods for controlling an ADC (Analog to Digital Converter) channel. One ADC device can have several channels. Analog input are sampled and converted according to the ADC device resolution to raw digital values between getMinValue() and getMaxValue(). Actual input voltage values can be calculated from raw digital values and the reference voltage value as returned by getVRefValue(). Each ADC channel is identified by a numerical ID and by a name. An ADCChannel instance can be opened by a call to one of the PeripheralManager.open() methods. Once opened, an application can read the current sampled input value of an ADC channel by calling the getValue() method or can acquire the input values sampled over a period of time by calling the getValues(int[], int, int) method. An application can also asynchronously acquire the input values sampled over a period of time by calling the startAcquisition() methods with an AcquisitionListener instance which will get cyclicly and asynchronously notified when the desired number of samples have been acquired. The input acquisition can be stopped by calling the stopAcquisition() method. An application can monitor the input value by calling the startMonitoring() method with a low and a high threshold value and MonitoringListener instance which will get asynchronously notified when the input value gets out of or back in the defined range. The monitoring can be stopped by calling the stopMonitoring() method. Only one acquisition (synchronous or asynchronous) and one monitoring can occur at any time. One acquisition and one monitoring can be performed concurrently at the same sampling rate (see getSamplingInterval()). They therefore respectively acquire and monitor the same sampled input values. When an application is no longer using an ADC channel, it should call the ADCChannel.close() method to release the ADC channel. Any further attempt to set or get the value of a ADC channel which has been closed will throw a PeripheralNotAvailableException. Note that asynchronous notification of range conditions or input acquisition is only loosely tied to hardware-level interrupt requests. The platform does not guarantee notification in a deterministic or timely manner.

The ADCChannel interface consists of several methods:

The MonitoringListener Interface

The MonitoringListener interface defines a method for being notified of ADC channel under- and over-threshold input value conditions. A MonitoringListener can be registered using the ADCChannel.startMonitoring(int, int, MonitoringListener) method.

The MonitoringListener interface consists of only one method:

The AcquisitionEvent Class

The AcquisitionEvent class encapsulates ADC channel input acquisition conditions. Note that this kind of event is never coalesced.

There are two constructors in the AcquisitionEvent class:

There are three methods in this class as well.

The ADCChannelConfig Class

The ADCChannelConfig class encapsulates the hardware addressing information, and static and dynamic configuration parameters, of an ADC channel. Some hardware addressing parameter, and static and dynamic configuration parameters, may be set to PeripheralConfig.DEFAULT. Whether such default settings are supported is both platform-dependent and peripheral driver-dependent.

An instance of ADCChannelConfig can be passed to the PeripheralManager.open(PeripheralConfig) or PeripheralManager.open(Class, PeripheralConfig) method to open the designated ADC channel with the specified configuration. A PeripheralConfigInvalidException is thrown when attempting to open a peripheral device with an invalid or unsupported configuration.

The ADCChannelConfig class consists of one constructor and several methods.

The MonitoringEvent Class

The MonitoringEvent class encapsulates ADC channel under- and over-threshold value conditions. If range events for the same ADC channel are coalesced, the value and the type (either in or out of range) are that of the last occurrence.

The PeripheralManager class contains two constants:

The PeripheralManager class consists of the following constructors and methods:

Exceptions

The com.oracle.deviceaccess.adc package consists of one exception, which is shown in Table 2-1:

Table 2-1 Exceptions of the com.oracle.deviceaccess.adc Package

Suite Type Description

InvalidSamplingRateException

Thrown by an instance of ADCChannel when the requested sampling rate is higher than the maximum sampling rate the ADC device can support.