ok6410 android driver(9)

时间:2025-01-02 19:07:50

  In this essay, I will write the JNI to test our leds device.

  If you don't know how to create a jni project, I suggest you to have a read on the following website :

http://www.cnblogs.com/plinx/p/3213824.html

  1、string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">ledsjni</string>
<string name="action_settings">Settings</string>
<string name="string_hello">String input:</string>
<string name="str_button">Str Input</string>
<string name="ioctl_hello">I/O control:</string>
<string name="io_button">I/O Input</string>
<string name="led1">Led1</string>
<string name="led2">Led2</string>
<string name="led3">Led3</string>
<string name="led4">Led4</string> </resources>

  2、activity_main.xml

  (1) graphical layout

ok6410 android driver(9)

  TIPS :

Ok6410's screen is placing horizontally, so you should design your application horizontally to adapt the screen.

  (2) activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <CheckBox
android:id="@+id/checkBox_str1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView_str"
android:text="@string/led1" /> <TextView
android:id="@+id/textView_str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/string_hello" /> <TextView
android:id="@+id/textView_io"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox_str1"
android:layout_below="@+id/checkBox_str1"
android:text="@string/ioctl_hello" /> <Button
android:id="@+id/onClick_strButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView_io"
android:layout_alignParentRight="true"
android:onClick="onClick_strButton"
android:text="@string/str_button" /> <Button
android:id="@+id/onClick_ioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/onClick_strButton"
android:layout_below="@+id/textView_io"
android:onClick="onClick_ioButton"
android:text="@string/io_button" /> <CheckBox
android:id="@+id/checkBox_io1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView_io"
android:text="@string/led1" /> <CheckBox
android:id="@+id/checkBox_io2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_io"
android:layout_toRightOf="@+id/checkBox_io1"
android:text="@string/led2" /> <CheckBox
android:id="@+id/checkBox_io3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_io"
android:layout_toRightOf="@+id/checkBox_str2"
android:text="@string/led3" /> <CheckBox
android:id="@+id/checkBox_io4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_io"
android:layout_toRightOf="@+id/checkBox_io3"
android:text="@string/led4" /> <CheckBox
android:id="@+id/checkBox_str2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_str"
android:layout_toRightOf="@+id/checkBox_str1"
android:text="@string/led2" /> <CheckBox
android:id="@+id/checkBox_str3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_str"
android:layout_toRightOf="@+id/checkBox_str2"
android:text="@string/led3" /> <CheckBox
android:id="@+id/checkBox_str4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_str"
android:layout_toRightOf="@+id/checkBox_str3"
android:text="@string/led4" /> </RelativeLayout>

  3、MainActivity.java

package com.example.ledsjni;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView; public class MainActivity extends Activity {
private CheckBox[] cbox_str = new CheckBox[4];
private CheckBox[] cbox_io = new CheckBox[4];
private TextView tview_str;
private TextView tview_io; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tview_str = (TextView)findViewById(R.id.textView_str);
tview_io = (TextView)findViewById(R.id.textView_io); cbox_str[0] = (CheckBox)findViewById(R.id.checkBox_str1);
cbox_str[1] = (CheckBox)findViewById(R.id.checkBox_str2);
cbox_str[2] = (CheckBox)findViewById(R.id.checkBox_str3);
cbox_str[3] = (CheckBox)findViewById(R.id.checkBox_str4); cbox_io[0] = (CheckBox)findViewById(R.id.checkBox_io1);
cbox_io[1] = (CheckBox)findViewById(R.id.checkBox_io2);
cbox_io[2] = (CheckBox)findViewById(R.id.checkBox_io3);
cbox_io[3] = (CheckBox)findViewById(R.id.checkBox_io4);
} public void onClick_strButton(View view) {
tview_io.setText("I/O control:");
String str = "";
for (int i = 0; i < 4; i++) {
if(cbox_str[i].isChecked())
str += "1";
else
str += "0";
}
tview_str.setText("Leds :" + str);
strLeds(str);
} public void onClick_ioButton(View view) {
tview_str.setText("I/O control:");
String str = "";
for (int i = 0; i < 4; i++) {
if(cbox_io[i].isChecked()) {
ioLeds(1, i);
str += "1";
} else {
ioLeds(0, i);
str += "0";
}
}
tview_io.setText("Leds :" + str);
} public native void strLeds(String str);
public native void ioLeds(int cmd, int arg); static {
System.loadLibrary("ledsjni");
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

  4、ledsjni.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ledsjni_MainActivity */ #ifndef _Included_com_example_ledsjni_MainActivity
#define _Included_com_example_ledsjni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_ledsjni_MainActivity
* Method: strLeds
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_example_ledsjni_MainActivity_strLeds
(JNIEnv *, jobject, jstring); /*
* Class: com_example_ledsjni_MainActivity
* Method: ioLeds
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_com_example_ledsjni_MainActivity_ioLeds
(JNIEnv *, jobject, jint, jint); #ifdef __cplusplus
}
#endif
#endif

  5、ledsjni.cpp

#include <jni.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h> #include "ledsjni.h" char* jstring_to_pchar(JNIEnv* env, jstring str)
{
char* pstr = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("utf-8");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray byteArray = (jbyteArray)(env->CallObjectMethod(str, mid, strencode));
jsize size = env->GetArrayLength(byteArray);
jbyte* pbyte = env->GetByteArrayElements(byteArray, JNI_FALSE);
if (size > ) {
pstr = (char*) malloc(size);
memcpy(pstr, pbyte, size);
}
return pstr;
} JNIEXPORT void JNICALL Java_com_example_ledsjni_MainActivity_strLeds
(JNIEnv* env, jobject thiz, jstring str)
{
int fd_leds;
fd_leds = open("/dev/s3c6410_leds", O_WRONLY);
char* pstr = jstring_to_pchar(env, str);
if (pstr != NULL) {
write(fd_leds, pstr, strlen(pstr));
} close(fd_leds);
} JNIEXPORT void JNICALL Java_com_example_ledsjni_MainActivity_ioLeds
(JNIEnv* env, jobject thiz, jint cmd, jint arg)
{
int fd_leds;
fd_leds = open("/dev/s3c6410_leds", O_WRONLY);
ioctl(fd_leds, cmd, arg); close(fd_leds);
}

  That's all about the ledsjni and leds device driver.