主题 : 模仿carlin MYLED写了一个mytimer(PWM) 蜂鸣器响了 哦哈哈 复制链接 | 浏览器收藏 | 打印
级别: 侠客
UID: 54019
精华: 0
发帖: 76
金钱: 380 两
威望: 76 点
贡献值: 0 点
综合积分: 152 分
注册时间: 2011-08-22
最后登录: 2012-03-17
楼主  发表于: 2011-09-16 11:35

 模仿carlin MYLED写了一个mytimer(PWM) 蜂鸣器响了 哦哈哈

按照myled先整了一遍led
如上先整了一个jni
然后是在java中调用
但是蜂鸣器毫无反应
我表示很郁闷。。。
mytimer.rar (82 K) 下载次数:42
这是jni
复制代码
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <jni.h>
  19. #include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
  20. int fd;
  21. /* This is a trivial JNI example where we use a native method
  22. * to return a new VM String. See the corresponding Java source
  23. * file located at:
  24. *
  25. *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  26. */
  27. jstring
  28. Java_com_PwmCtr_PwmCtr_stringFromJNI( JNIEnv* env,
  29.                                                   jobject thiz )
  30. {
  31.     return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
  32. }
  33. jint Java_com_PwmCtr_PwmCtr_open( JNIEnv* env,
  34.         jobject thiz )
  35. {
  36.     fd = open("/dev/pwm", O_RDONLY);
  37.     if (fd < 0)
  38.     {
  39.         fd = open("/dev/pwm", O_RDONLY);
  40.         return 1;
  41.     }
  42.     return 0;
  43. }
  44. jint Java_com_PwmCtr_PwmCtr_ioctrl( JNIEnv* env,
  45.         jobject thiz ,jint cmd,jlong arg)
  46. {
  47.     ioctl(fd,cmd, arg);
  48.     return 1;
  49. }
  50. jint Java_com_PwmCtr_PwmCtr_close( JNIEnv* env,
  51.         jobject thiz )
  52. {
  53.     close(fd);
  54.     return 1;
  55. }

这是pwmctrl类
复制代码
  1. package com.PwmCtr;
  2. public class PwmCtr {
  3.     public native int open();
  4.     public native int ioctrl(int cmd ,long  arg);
  5.     public native int close();
  6.     public native String  unimplementedStringFromJNI();
  7.     static {
  8.         System.loadLibrary("pwm-jni");
  9.     }
  10. }

这是activity
复制代码
  1. package com.Mytimer;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import com.Mytimer.R;
  8. import com.PwmCtr.PwmCtr;
  9. public class MytimerActivity extends Activity implements OnClickListener{
  10.   
  11.     private Button Bt1;
  12.     private Button Bt2;
  13.     private Button Bt3;
  14.     private static long frq=0;
  15.     PwmCtr pwm;
  16.     
  17.     /** Called when the activity is first created. */
  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.main);
  22.         frq=0;
  23.         pwm=new PwmCtr();
  24.         pwm.open();
  25.         pwm.ioctrl(0,0);
  26.         
  27.         Bt1 = (Button)findViewById(R.id.button1);
  28.         Bt2 = (Button)findViewById(R.id.button2);
  29.         Bt3 = (Button)findViewById(R.id.button3);
  30.         
  31.         Bt1.setOnClickListener(this);
  32.         Bt2.setOnClickListener(this);
  33.         Bt3.setOnClickListener(this);
  34.     }
  35.     
  36.     
  37.    public void onClick(View v) {
  38.         
  39.         switch (v.getId()) {
  40.         case R.id.button1:
  41.             frq+=1000;
  42.             pwm.ioctrl(1,frq);//关闭蜂鸣器
  43.             break;
  44.         case R.id.button2:
  45.             if(frq>1000)
  46.             {
  47.                 frq-=1000;
  48.             }
  49.             else
  50.             {
  51.                 frq=0;
  52.             }
  53.             pwm.ioctrl(1,frq);//关闭蜂鸣器
  54.             break;
  55.         case R.id.button3:
  56.             pwm.ioctrl(0,0);//关闭蜂鸣器
  57.             
  58.             break;
  59.             default:
  60.                 break;
  61.         }
  62.    }
  63. }
[ 此帖被odanobunaga在2011-09-16 12:10重新编辑 ]
菜鸟在路上
级别: 圣骑士
UID: 42749
精华: 6
发帖: 241
金钱: 1530 两
威望: 306 点
贡献值: 6 点
综合积分: 602 分
注册时间: 2011-04-11
最后登录: 2016-07-19
1楼  发表于: 2011-09-16 11:58
应用程序没有挂吧, 蜂鸣器是不是好的
级别: 侠客
UID: 54019
精华: 0
发帖: 76
金钱: 380 两
威望: 76 点
贡献值: 0 点
综合积分: 152 分
注册时间: 2011-08-22
最后登录: 2012-03-17
2楼  发表于: 2011-09-16 12:49
是参数名称不匹配
java中的参数名称类型一定要与jni.c中的参数名称 类型一致
否则调用不成功