Creation Native Code Library from MATLAB-code by TMC Compiler for using in a mobile application
Example: TunerDemo application for Android (TM)
The source of this example is available in subdirectory
/examples/Simo_ADT/Simo_ADT_ws/TunerDemo
of TMC Compiler distribution
This example illustrates the usage of TMC Compiler for creation a native code math library from MATLAB-code by TMC Compiler. The library implements native methods of a Java class in Android application. The application GUI is written in Java and calls the native code using Java Native Interface (JNI).
Prerequisites
1.Installed Android Development Tools (ADT) as a plugin for the Eclipse IDE from http://http://developer.android.com/sdk/installing/installing-adt.html
2.Installed Native Development Kit (NDK) from http://developer.android.com/tools/sdk/ndk/index.html
Note: The support of ADT is announced as ending now and the developers are recommended to migrate to Android Studio. However, ADT platform had been selected due to its native code debugging support.
Application GUI
After a launch main activity window appears:
TunerDemo window |
User should enter the two parameters and presses "Run" button to start calculations. The calculations progress status will be shown in a tip tool. When finished, user should press "Show Nichols" button to view the results.
"Nichols plot" activity window appears and displays a graph that is updated by moving a slider or pressing one of buttons:
Nichols plot window |
Native methods: interface between Java and C
The application is written in Java. The main activity class (file: src/com/tmc/tunerdemo/MainActivity.java) is defined as
public class MainActivity extends Activity implements Runnable
This class supports multi-threading: the calculations in a math library are performed in a separate thread. The class declaration contains the following definitions for native methods:
public native String runTmcTuning(float M,float R,float L, float Ts);
public native int pidgetNumFreqs();
public native int pidgetTypeCrl();
public native int pidgetNumPlants();
public native int pidgetGetPlants(double [] a);
public native int pidgetNumCrl();
public native int pidgetGetCrl(double [] a);
and the static code:
static {
System.loadLibrary("TmcTunerDemo");
}
The first method performs calculations, others retrieve the results. The JNI-wrapper is manually written in C-code (file: jni/mainTunerDemoJni.c). It contains the native methods implementation:
jint Java_com_tmc_tunerdemo_MainActivity_pidgetNumFreqs(JNIEnv *env, jobject callingObject)
{
...
// Calls to the auto-generated tmc-functions here
...
}
The calculation progress and exception handling in the native library are implemented by calling Java methods from C-code. Refer to file tmc_jni_except.c, functions javaDispString and javaRaiseExcept.
No comments:
Post a Comment