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

  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.

  

  

ok6410 android driver(9)的更多相关文章

  1. ok6410 android driver(5)

    Test the android driver by JNI (Java Native Interface), In the third article, we know how to compile ...

  2. ok6410 android driver(11)

    This essay, I go to a deeply studying to android HAL device driver program. According to the android ...

  3. ok6410 android driver(8)

    In the past, we know how to create and run a simple character device driver on pc, goldfish and ok64 ...

  4. ok6410 android driver(3)

    This article discusses the Makefile and how to port the module to different platform (localhost and ...

  5. ok6410 android driver(12)

    In this essay, I will talk about how to write the service libraries. TIPS : I won't discuss the name ...

  6. ok6410 android driver(10)

    From this essay, we go to a new discussion "Android Hardware Abstraction Layer". In this e ...

  7. ok6410 android driver(7)

    This article talk about how to test device driver on JNI. There are two ways to test the device driv ...

  8. ok6410 android driver(6)

    This is a short essay about the mistakes in compiling ok6410 android-2.3 source codes. If there is n ...

  9. ok6410 android driver(1)

    target system : Android (OK6410) host system : Debian Wheezy AMD64 1.Set up android system in ok6410 ...

随机推荐

  1. AngularJS datepicker 和 datatimepicker

    本文内容 项目结构 AngularJS datepicker AngularJS+jQueryUI datetimepicker 本文介绍 AngualrJS datetimepicker 控件.说明 ...

  2. 为EasyUI 的Tab 标签添加右键菜单

    在网上看了很多demo 自己实现了一个效果如下 ps jquery1.7.2 jQuery EasyUI 1.3.6easyui QQ群:15129679 <!doctype html> ...

  3. Android开发(二十七)——android:layout_weight的真实含义

    android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比! ...

  4. LCLFramework框架之Repository模式

    Respository模式在示例中的实际目的小结一下 Repository模式是架构模式,在设计架构时,才有参考价值: Repository模式主要是封装数据查询和存储逻辑: Repository模式 ...

  5. android实现自动升级并安装打开

    http://blog.csdn.net/wa991830558/article/details/41014673 这是一个比较简单的程序,但网上还是有很多人问起这个问题,并且回答的人,也没有完全回答 ...

  6. A cycle was detected in the build path of project

    解决Eclipse中Java工程间循环引用而报错的问题 如果我们的项目包含多个工程(project),而它们之间又是循环引用的关系,那么Eclipse在编译时会抛出如下一个错误信息: “A cycle ...

  7. EF: Returns multi table from procedure

    原文:https://msdn.microsoft.com/en-us/data/jj691402.aspx

  8. Quartz的线程池解析

    [org.quartz.core相关类图] 可以看到核心类为QuartzScheduler [QuartzScheduler构造函数] public QuartzScheduler(QuartzSch ...

  9. ThreadPoolExecutor

    ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...

  10. UDP网络通信OSC 协议

    使用方法 ofxOscMessage mesg; mesg.setAddress("m"); mesg.addIntArg(); mesg.addIntArg(); mesg.ad ...