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. ASP.NET MVC中的模型装配 封装方法 非常好用

    下面说一下 我们知道在asp.net mvc中 视图可以绑定一个实体模型 然后我们三层架构中也有一个model模型 但是这两个很多时候却是不一样的对象来的 就拿微软的官方mvc例子来说明 微软的视图实 ...

  2. Windows7 网上邻居设置

    1.运行gpedit.msc进入组策略:Windows设置---安全配置---本地策略---安全选项---网络访问:本地账户的共享和安全模型(设为仅来宾-对本地用户进行身份认证)---网络访问:不允许 ...

  3. 判断big endian和little endian的方法

    http://blog.sina.com.cn/s/blog_6ab0b9a80101awzr.html   不同体系的CPU在内存中的数据存储往往存在着差异.例如,Intel的x86系列处理器将低序 ...

  4. MTNET 自用ios网络库开源

    短短两天就在https://git.oschina.net/gangwang/MTNET这里收获15个星 github 5星, 值得收藏! MTNET 自用ios网络库开源, 自用很久了,在数歀上架的 ...

  5. [LeetCode] Remove Invalid Parentheses

    This problem can be solved very elegantly using BFS, as in this post. The code is rewritten below in ...

  6. 对已经add的文件不在跟踪

    记录每次更新到仓库 现在我们手上已经有了一个真实项目的 Git 仓库,并从这个仓库中取出了所有文件的工作拷贝.接下来,对这些文件作些修改,在完成了一个阶段的目标之后,提交本次更新到仓库. 请记住,工作 ...

  7. WCF关于svcutil生成关于绑定出现 元数据包含无法解析的引用的解决方案

    元数据包含无法解析的引用. 没有终结点在侦听可以接受消息的 net.tcp://localhost:8000/service.这通常是由于不正确的地址或者 SOAP 操作导致的.如果存在此情况,请参阅 ...

  8. ReentrantLock的使用

    class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondi ...

  9. 使用windbg分析iis崩溃的一个实例

    问题背景说明:客户的生产环境不定时发生崩溃,需要定位崩溃的原因.在开发环境不能重现该问题,准备抓取IIS的dump文件分析 第一步:在客户的生产环境抓取dump文件 参考:IIS崩溃时自动抓取Dump ...

  10. java攻城狮之路(Android篇)--MP3 MP4、拍照、国际化、样式主题、图片移动和缩放

    一.MP3播放器 查看Android API文档可以看到MediaPlayer状态转换图: 练习: package com.shellway.mp3player; import java.io.Fil ...