LED灯循环闪灭

一、需求描述

通过打开写的测试的app,点击测试按钮,循环调用控制LED闪灭的方法来控制LED的闪灭。


二、完成测试程序涉及到的文件:

1.一个xxx.so文件:libzhc_LedControl_jni.so

这个文件要导入到Android系统的开发机器中(我司是平板开发的,这个文件不了解,只知道要调用到平板上的LED灯时,一定要将此文件导入到系统的system/lib/目录下才行)。

push之前先adb remount下不然导不进,还会报read-only file System。



2.一个zhc_LedControl.java文件

扫描二维码关注公众号,回复: 11522802 查看本文章

这个文件将会被导入到创建的app的工程目录的src下,然后会报错,根据提示生成相应的包名;根据提示至不报错即可;控制LED亮灭的方法在这个文件里面(这是个.java文件下

面会附上)。



三、测试app的相关代码

1.MainActivity.java

<pre name="code" class="html">package com.zhc.Demo;

import com.zhc.android.infrared.zhc_LedControl;
import android.os.Bundle;
import android.view.View;
import android.widget.ToggleButton;
import android.app.Activity;

public class MainActivity extends Activity {
	private static final int LED_CMD_ON = 1;
	private static final int LED_TYPE_LOGO = 0;
	protected static final int LED_CMD_OFF = 0;
	private static final String TAG = "MainActivity";
	private ToggleButton mBtn;
	boolean mDone = false;
	protected Thread mThread;
	protected Thread mThread1;
	private zhc_LedControl zhccontrol;
	boolean one = true;
	boolean led_on = true;
	boolean start_led_test = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mBtn = (ToggleButton) findViewById(R.id.send);
		zhccontrol = new zhc_LedControl();
		// 1.创建线程
		mThread1 = new Thread() {
			public void run() {
				while (true) {
					if (start_led_test) {
						if (led_on) {
							zhccontrol.ZHC_LedControl(LED_TYPE_LOGO, LED_CMD_ON);
						} else {
							zhccontrol.ZHC_LedControl(LED_TYPE_LOGO,LED_CMD_OFF);
						}
						led_on = !led_on;
						// 睡眠 1 秒
						try {
							sleep(1 * 1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					} else {
						// 睡眠 100ms
						try {
							sleep(1 * 100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		};

		mThread1.start();

		// 2.发送按钮的监听
		mBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (v.getId() == R.id.send) {
					if (!mDone) {
						mDone = true;
						start_led_test = true;					
					} else {
						mDone = false;
						start_led_test = false;
						mThread1 = null;
					}
				}
			}
		});
	}
}

 这里线程不创建在按钮点击的代码里面就避免了点击一次就去创建一个线程了。 
 

2.zhc_LedControl.java

package com.zhc.android.infrared;

import android.util.Log;

public class zhc_LedControl {
	
	static String TAG = "zhc_LedControl_Jni";

    // 错误代码
	public static int LED_ERR_NOT			= -0;		// 没有错误
	public static int LED_ERR_OPENFILE		= -1;		// 打开LED设备失败,设备驱动可能未能成功加载,或者没有权限
	public static int LED_ERR_CTRLCMD		= -2;		// 写控制命令失败
	public static int LED_ERR_NOTDEFINEDCMD	= -9;		// 未定义命令
	
	// LED 控制
	public static int LED_CMD_ON			= 1;		// 打开指定类型的 LED 
	public static int LED_CMD_OFF			= 0;		// 关闭指定类型的 LED
	
	// LED 类型
	public static int LED_TYPE_LOGO			= 0;		// LED 类型
	
	/*************************************************************
	*	函数名: ZHC_LedControl
	*	参数:
	*		LedType:		LED_TYPE_*
	*		CmdOn:			LED_CMD_*
	*	返回值:
	*		参考上面的错误代码 LED_ERR_*
	**************************************************************/
	public native static int ZHC_LedControl(int LedType, int CmdOn);
	
	/* 加载库文件 */
	static 
	{
		System.loadLibrary("zhc_LedControl_jni");	
	}
}

3.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" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="测试项" />

    <TextView
        android:id="@+id/logo_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/tv"
        android:text="logo灯测试" />

    <ToggleButton
        android:id="@+id/send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/logo_tv"
        android:textOff="@string/btn_start"
        android:textOn="@string/btn_stop" />
</RelativeLayout>

4.strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Demo</string>
     <string name="btn_start">测试</string>
    <string name="btn_stop">Stop</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>



猜你喜欢

转载自blog.csdn.net/ningchao328/article/details/53021588