An Analog to Digital Converter (ADC) is a device that translates an analog voltage to a digital value that a microprocessor can understand. There are several channels available depending on module version and technology; however, these channels all work the same way.
The ConnectCore 6 has several ADC interfaces to measure analog signals and transform them into digital values. In the ConnectCore 6 Hardware Reference Manual you can find information about the available ADC channels.
Digi adds to Android an API to manage these ADC channels. You can configure them, read values, and program periodic sampling among other things. In the Digi APIx javadoc you can find a complete list of the available methods in this API.
Unless noted, all ADC API methods require the com.digi.android.permission.ADC permission.
If your application does not have the com.digi.android.permission.ADC permission it will not have access to any ADC service feature. |
First, a new ADCManager object must be instantiated by passing the Android Application Context.
import android.app.Activity;
import android.os.Bundle;
import com.digi.android.adc.ADC;
import com.digi.android.adc.ADCManager;
public class ADCSampleActivity extends Activity {
ADCManager adcManager;
[...]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Instantiate the ADC manager object.
adcManager= new ADCManager(this);
[...]
}
[...]
}
Instantiate an ADC channel
The ADCManager allows you to create an ADC object for a specific ADC channel and get its current value.
import com.digi.android.adc.ADC;
import com.digi.android.adc.ADCManager;
[...]
ADCManager adcManager = ...;
// Create ADC object for channel 1.
ADC adc = adcManager.createADC(1);
// Get the current value of channel 1.
System.out.format("Channel %d: %d%n", adc.getChannel(), adc.getValue());
[...]
Monitor ADC channels
You can monitor a specific ADC channel if you subscribe an IADCListener to the ADC object. Use the registerListener(IADCListener) method to register for particular ADC channel updates. The listener starts receiving ADC samples as soon as the startSampling(int) method is called specifying the interval between samples in milliseconds.
import com.digi.android.adc.ADC;
import com.digi.android.adc.ADCManager;
[...]
ADCManager adcManager = ...;
// Create ADC object for channel 1.
ADC adc = adcManager.createADC(1);
// Create the ADC listener.
MyADCListener myADCListener = ...;
// Register the ADC listener.
adc.registerListener(myADCListener);
// Start monitoring ADC channel every 5 seconds.
adc.startSampling(5000);
[...]
The registered listener class, MyADCListener, must implement the IADCListener interface. This interface defines the sampleReceived(ADCSample) method, that is called when a new ADC sample for the channel is received, in the example above every 5 seconds.
The sampleReceived(ADCSample) method receives a new ADCSample object with the value of the ADC channel (getValue()) and the time when the sample was taken (getTimestamp()).
import com.digi.android.adc.IADCListener;
public class MyADCListener implements IADCListener {
@Override
public void sampleReceived(ADCSample sample) {
// This code will be executed every time a new ADC sample is received.
System.out.format("ADC value (%l): %d%n", sample.getTimestamp(), sample.getValue());
}
}
You can have more than one IADCListener waiting for updates in the same channel. To stop the ADC channel notifications, use the stopSampling() method. If you no longer wish to receive ADC channel samples in a determined listener, use the unregisterListener(IADCListener) method to unsubscribe an already registered listener.
[...]
ADC adc = ...;
MyADCListener myADCListener = ...;
adc.registerListener(myADCListener);
adc.startSampling(5000);
[...]
// Stop ADC notifications.
adc.stopSampling();
// Remove the ADC listener.
adc.unregisterListener(myADCListener);
[...]
ADC example
Example: ADC |
---|
The ADC Sample Application demonstrates the usage of the ADC API. In this example, you can select one of the ADC channels available in your ConnectCore 6 and configure the sample rate of that interface. You can import the example using Digi’s Android Studio plugin. For more information, see Import a Digi sample application. To look at the application source code, go to the GitHub repository. |