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. JAVA Builder模式构建MAP/LIST的示例

    我们在构建一个MAP时,要不停的调用put,有时候看着觉得很麻烦,刚好,看了下builder模式,觉得这思路不错,于是乎,照着用builder模式写了一个构建MAP的示例,代码如下: import j ...

  2. retrofit2 上传图片

    直接上代码 接口请求代码 NewResultApi.modifyUserInfo(userModel.getUid(), userModel.getToken(), sex, intro, name, ...

  3. [转]移动端web页面使用字体的思考

    一直不知道手机端用的什么字体,只是觉得类似雅黑,直到有一次设计师问到设计移动web页面该用什么字体才严肃地想起这个问题. 前人已栽树,后人我就直接转来吧…… 回想2年前刚开始接触手机项目,接到PSD稿 ...

  4. fio terse输出详解

    fio, the flexible IO tester, is a very useful tool for benchmarking IO performance. It has an option ...

  5. <欧奈尔制胜法则—如何在股市中赚钱>读书笔记

    在选择个股建仓时,要选择那些在最近季度报表中,每股收益比上年同期要有较大增幅的股票. 每股收益是指公司税后净利润除以公司普通股的总股本 选择年增长率为25%--50%的公司 年度盈利和季度盈利都要出色 ...

  6. window_x64微信小程序环境搭建

    所需文件地址如下: http://pan.baidu.com/s/1nv0IHhn(ylk7)   1.下载微信开发工具0.7.0_x64 安装完成后,打开程序,进行微信扫码登录 2.下载微信开发工具 ...

  7. makeimg

    >./simg2img [system.img] [system2.img]>mount -o loop [system2.img] [s/]>./make_ext4fs -s -l ...

  8. React Native 重新建项目遇到的一些问题

    1.基本上一句话,就是本地的node太旧了,跟不上React_Native的节奏,所以需要更新node,但是单纯的更新node丫丫竟然不让我跟,因为是用Homebrew来管理的,所以先update了下 ...

  9. AX 与Citrix打印机问题

    国外文章,抄个链接,备查 http://blogs.msdn.com/b/axsupport/archive/2010/07/06/ax-2009-citrix-amp-terminal-server ...

  10. MySql批量更新方法

    准备数据 表 user(用户).dept(部门) 1:更新符合单个条件的某个字段的一条数据 update user u set u.name = '测试' where u.id = "&qu ...