按照myled先整了一遍led
如上先整了一个jni
然后是在java中调用
但是蜂鸣器毫无反应
我表示很郁闷。。。
mytimer.rar (82 K) 下载次数:42 这是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_PwmCtr_PwmCtr_stringFromJNI( JNIEnv* env,
- jobject thiz )
- {
- return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
- }
- jint Java_com_PwmCtr_PwmCtr_open( JNIEnv* env,
- jobject thiz )
- {
- fd = open("/dev/pwm", O_RDONLY);
- if (fd < 0)
- {
- fd = open("/dev/pwm", O_RDONLY);
- return 1;
- }
- return 0;
- }
- jint Java_com_PwmCtr_PwmCtr_ioctrl( JNIEnv* env,
- jobject thiz ,jint cmd,jlong arg)
- {
- ioctl(fd,cmd, arg);
- return 1;
- }
- jint Java_com_PwmCtr_PwmCtr_close( JNIEnv* env,
- jobject thiz )
- {
- close(fd);
- return 1;
- }
|
这是pwmctrl类
复制代码- package com.PwmCtr;
- public class PwmCtr {
- public native int open();
- public native int ioctrl(int cmd ,long arg);
- public native int close();
- public native String unimplementedStringFromJNI();
- static {
- System.loadLibrary("pwm-jni");
- }
- }
|
这是activity
复制代码- package com.Mytimer;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import com.Mytimer.R;
- import com.PwmCtr.PwmCtr;
- public class MytimerActivity extends Activity implements OnClickListener{
-
- private Button Bt1;
- private Button Bt2;
- private Button Bt3;
- private static long frq=0;
- PwmCtr pwm;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- frq=0;
- pwm=new PwmCtr();
- pwm.open();
- pwm.ioctrl(0,0);
-
- Bt1 = (Button)findViewById(R.id.button1);
- Bt2 = (Button)findViewById(R.id.button2);
- Bt3 = (Button)findViewById(R.id.button3);
-
- Bt1.setOnClickListener(this);
- Bt2.setOnClickListener(this);
- Bt3.setOnClickListener(this);
- }
-
-
- public void onClick(View v) {
-
- switch (v.getId()) {
- case R.id.button1:
- frq+=1000;
- pwm.ioctrl(1,frq);//关闭蜂鸣器
- break;
- case R.id.button2:
- if(frq>1000)
- {
- frq-=1000;
- }
- else
- {
- frq=0;
- }
- pwm.ioctrl(1,frq);//关闭蜂鸣器
- break;
- case R.id.button3:
- pwm.ioctrl(0,0);//关闭蜂鸣器
-
- break;
- default:
- break;
- }
- }
- }
|
[ 此帖被odanobunaga在2011-09-16 12:10重新编辑 ]