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. nginx模块开发篇 (阿里著作)

    背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...

  2. Scala 深入浅出实战经典 第57讲:Scala中Dependency Injection实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  3. 2013年ACM湖南省赛总结

    今年的比赛最大的变化就是改用OJ判题了,相比于PC^2确实省事了不少,至少可以直接复制样例了.题目方面依旧是刘汝佳命题,这点还是相当好的,至少给人以足够的安全感. 开始比赛之后安叔瞬间就把前半部分题目 ...

  4. mysql性能监控工具:mycheckpoint的使用方法

    mycheckpoint 是针对mysql的一个性能监控.指标采集的python写成的工具. 工作原理说明: mycheckpoint是一段脚本,通过将其设置为crontab定时任务,每几分钟采集一次 ...

  5. Android 5.1 AOSP 源码获取

    本文已同步更新至:http://dxjia.cn/2015/08/android-aosp-code-sync/ Android 5.1源码开放有一个多月啦,但由于城墙的关系,每次想着更新最新源码学习 ...

  6. VS2010安装失败解决办法

    1. 运行regedit打开注册表: 2. 找到HKEY_LOCAL_MACHINE\SOFWARE\ Microsoft\ Internet Explorer\ MAIN: 3. MAIN子键的权限 ...

  7. 使用ueditor小结

    1. 导入 js: ueditor.config.js ueditor.all.js css/images/plugin: themes lang dialog(可选) third-party(可选) ...

  8. Oracle 数据库表同步方法浅议

    总结一下Oracle数据库表级别的复制同步 一.通过触发器进行表的复制 原理,是监听表上都某一字段进行的DML操作,然后得到DML操作的数据,重新在另一个表上执行DML操作. 优点: 简单,编写一个触 ...

  9. 安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误

    安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误 这个错误几个月以前解决过一次,但是到又碰到的时候,竟然完全忘记当时怎么解决的了, 看来上了年纪记忆真是越来越不行了... 解决方案很简单 ...

  10. 2015 Android Dev Summit(安卓开发峰会)第一天

    今年的Google I/O没有抽到票,不能到现场参加.不过11月举行的Android Dev Summit的票是先到先得的方式,所以早早的提交了注册.今天终于有机会当面跟Android系统的设计开发者 ...