博航社区

 找回密码
 立即注册博航社区

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 10098|回复: 0
打印 上一主题 下一主题

android之应用程序LED(zz) [复制链接]

Rank: 2

跳转到指定楼层
楼主
发表于 2011-11-22 00:25:56 |只看该作者 |倒序浏览
分享到:
参考友善的LED程序,自己也写了个,不是用友善的libfriendlyarm-hardware.so,自己用NDK中的例子hello-jni写了个程序,已经编译成libhello-jni.so.自己摸索了好久总算有点收获,感谢各位网友的帮忙。部门源码贴出来给大家参考! 刚搞完就拿出来分享了,欢迎大家指正。有谁想看libfriendlyarm-hardware.so的源码的可以参考这个。
JNI部分 用C语言实现

复制代码
  • /*
  • * Copyright (C) 2009 The Android Open Source Project
  • *
  • * Licensed under the Apache License, Version 2.0 (the "License");
  • * you may not use this file except in compliance with the License.
  • * You may obtain a copy of the License at
  • *
  • * http://www.apache.org/licenses/LICENSE-2.0
  • *
  • * Unless required by applicable law or agreed to in writing, software
  • * distributed under the License is distributed on an "AS IS" BASIS,
  • * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • * See the License for the specific language governing permissions and
  • * limitations under the License.
  • *
  • */
  • #include <string.h>
  • #include <jni.h>
  • #include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
  • int fd;
  • /* This is a trivial JNI example where we use a native method
  • * to return a new VM String. See the corresponding Java source
  • * file located at:
  • *
  • * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  • */
  • jstring
  • Java_com_helloworld_helloworld_stringFromJNI( JNIEnv* env,
  • jobject thiz )
  • {
  • return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
  • }
  • jint Java_com_helloworld_helloworld_openled( JNIEnv* env,
  • jobject thiz )
  • {
  • fd = open("/dev/leds0", O_RDONLY);
  • if (fd < 0)
  • {
  • fd = open("/dev/leds", O_RDONLY);
  • return 1;
  • }
  • return 0;
  • }
  • jint Java_com_helloworld_helloworld_setLedState( JNIEnv* env,
  • jobject thiz ,jint ledID,jint ledState)
  • {
  • ioctl(fd,ledState, ledID);
  • return 1;
  • }
  • jint Java_com_helloworld_helloworld_closeled( JNIEnv* env,
  • jobject thiz )
  • {
  • close(fd);
  • return 1;
  • }



APP部分即JAVA


复制代码
  • package com.helloworld;
  • import android.app.Activity;
  • import android.widget.TextView;
  • import android.os.Bundle;
  • import android.view.View;
  • import android.widget.Button;
  • import android.view.View.OnClickListener;
  • public class helloworld extends Activity {
  • private Button btnLED1On;
  • private Button btnLED1Off;
  •     /** Called when the activity is first created. */
  •     @Override
  •     public void onCreate(Bundle savedInstanceState)
  •     {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.main);
  •         openled();
  •      btnLED1On = (Button)findViewById(R.id.btnLED1On);
  •      btnLED1Off = (Button)findViewById(R.id.btnLED1Off);
  •      btnLED1On.setOnClickListener(new View.OnClickListener()
  •      {
  •       public void onClick(View v) /**当按钮按下*/
  •       {
  •        setLedState(0,1);
  •       }
  •      }
  •      );
  •      btnLED1Off.setOnClickListener(new View.OnClickListener()
  •      {
  •       public void onClick(View v) /**当按钮按下*/
  •       {
  •        setLedState(0,0);
  •       }
  •      }
  •      );
  •     }
  •     /* A native method that is implemented by the
  •      * 'hello-jni' native library, which is packaged
  •      * with this application.
  •      */
  •     public native String  stringFromJNI();
  •     public native int openled();
  •     public native int setLedState(int ledID,int ledState);
  •     public native int closeled();
  •     /* This is another native method declaration that is *not*
  •      * implemented by 'hello-jni'. This is simply to show that
  •      * you can declare as many native methods in your Java code
  •      * as you want, their implementation is searched in the
  •      * currently loaded native libraries only the first time
  •      * you call them.
  •      *
  •      * Trying to call this function will result in a
  •      * java.lang.UnsatisfiedLinkError exception !
  •      */
  •     public native String  unimplementedStringFromJNI();
  •     /* this is used to load the 'hello-jni' library on application
  •      * startup. The library has already been unpacked into
  •      * /data/data/com.example.HelloJni/lib/libhello-jni.so at
  •      * installation time by the package manager.
  •      */
  •     static {
  •         System.loadLibrary("hello-jni");
  •     }
  • }




复制代码
  • /*
  • * Copyright (C) 2009 The Android Open Source Project
  • *
  • * Licensed under the Apache License, Version 2.0 (the "License");
  • * you may not use this file except in compliance with the License.
  • * You may obtain a copy of the License at
  • *
  • * http://www.apache.org/licenses/LICENSE-2.0
  • *
  • * Unless required by applicable law or agreed to in writing, software
  • * distributed under the License is distributed on an "AS IS" BASIS,
  • * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • * See the License for the specific language governing permissions and
  • * limitations under the License.
  • *
  • */
  • #include <string.h>
  • #include <jni.h>
  • #include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
  • int fd;
  • /* This is a trivial JNI example where we use a native method
  • * to return a new VM String. See the corresponding Java source
  • * file located at:
  • *
  • * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  • */
  • jstring
  • Java_com_helloworld_helloworld_stringFromJNI( JNIEnv* env,
  • jobject thiz )
  • {
  • return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
  • }
  • jint Java_com_helloworld_helloworld_openled( JNIEnv* env,
  • jobject thiz )
  • {
  • fd = open("/dev/leds0", O_RDONLY);
  • if (fd < 0)
  • {
  • fd = open("/dev/leds", O_RDONLY);
  • return 1;
  • }
  • return 0;
  • }
  • jint Java_com_helloworld_helloworld_setLedState( JNIEnv* env,
  • jobject thiz ,jint ledID,jint ledState)
  • {
  • ioctl(fd,ledState, ledID);
  • return 1;
  • }
  • jint Java_com_helloworld_helloworld_closeled( JNIEnv* env,
  • jobject thiz )
  • {
  • close(fd);
  • return 1;
  • }



APP部分即JAVA


复制代码
  • package com.helloworld;
  • import android.app.Activity;
  • import android.widget.TextView;
  • import android.os.Bundle;
  • import android.view.View;
  • import android.widget.Button;
  • import android.view.View.OnClickListener;
  • public class helloworld extends Activity {
  • private Button btnLED1On;
  • private Button btnLED1Off;
  •     /** Called when the activity is first created. */
  •     @Override
  •     public void onCreate(Bundle savedInstanceState)
  •     {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.main);
  •         openled();
  •      btnLED1On = (Button)findViewById(R.id.btnLED1On);
  •      btnLED1Off = (Button)findViewById(R.id.btnLED1Off);
  •      btnLED1On.setOnClickListener(new View.OnClickListener()
  •      {
  •       public void onClick(View v) /**当按钮按下*/
  •       {
  •        setLedState(0,1);
  •       }
  •      }
  •      );
  •      btnLED1Off.setOnClickListener(new View.OnClickListener()
  •      {
  •       public void onClick(View v) /**当按钮按下*/
  •       {
  •        setLedState(0,0);
  •       }
  •      }
  •      );
  •     }
  •     /* A native method that is implemented by the
  •      * 'hello-jni' native library, which is packaged
  •      * with this application.
  •      */
  •     public native String  stringFromJNI();
  •     public native int openled();
  •     public native int setLedState(int ledID,int ledState);
  •     public native int closeled();
  •     /* This is another native method declaration that is *not*
  •      * implemented by 'hello-jni'. This is simply to show that
  •      * you can declare as many native methods in your Java code
  •      * as you want, their implementation is searched in the
  •      * currently loaded native libraries only the first time
  •      * you call them.
  •      *
  •      * Trying to call this function will result in a
  •      * java.lang.UnsatisfiedLinkError exception !
  •      */
  •     public native String  unimplementedStringFromJNI();
  •     /* this is used to load the 'hello-jni' library on application
  •      * startup. The library has already been unpacked into
  •      * /data/data/com.example.HelloJni/lib/libhello-jni.so at
  •      * installation time by the package manager.
  •      */
  •     static {
  •         System.loadLibrary("hello-jni");
  •     }
  • }




图片:

应用程序在这

右边的那个LED程序是我的
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0 转发到微博

使用道具 举报

Archiver|手机版|谷歌地图|百度地图|官网地图|淘宝链接|博航智能圈 - ARM嵌入式AI物联网IOT机器人

GMT+8, 2024-5-5 17:37 , Processed in 0.026728 second(s), 6 queries , Gzip On, Memcache On.

club.broadon.net

© 2007-2012 BroadOn

回顶部