友善之臂Mini6410之Android开发学习笔记(1)-LED Demo
来源:互联网 发布:抢火车票软件 编辑:程序博客网 时间:2024/05/18 00:16
友善之臂Mini6410之Android开发学习笔记源码同步更新,请使用git工具进行同步。关于Git工具更多信息,请参考:http://progit.org/book/zh/
git clone https://code.google.com/p/androiddemoformini6410/
LEDActivity.java
package com.mini6410.LED;import com.friendlyarm.AndroidSDK.HardwareControler;import com.mini6410.R;import android.app.Activity;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.ToggleButton;/** * * ClassName:LEDActivity * Reason: LED Demo * * @author snowdream * @version * @since Ver 1.1 * @Date 20112012-03-1116:07 * * @see */public class LEDActivity extends Activity implements ToggleButton.OnCheckedChangeListener {/*四个LED灯,编号ID依次为:LED 0,LED_1,LED_2,LED_3*/public static final int LED_0 = 0;public static final int LED_1 = 1;public static final int LED_2 = 2;public static final int LED_3 = 3;/*LED灯的状态: ON 表示点亮, OFF表示熄灭*/public static final int OFF = 0;public static final int ON = 1;private int mledID = LED_0; private int mledState = OFF;private boolean mStop = false;/*LED编号数组*/private int[] mleds = new int[]{LED_0,LED_1,LED_2,LED_3};/*5个开关按钮*/private ToggleButton mToggleButton_led0 = null;private ToggleButton mToggleButton_led1 = null;private ToggleButton mToggleButton_led2 = null;private ToggleButton mToggleButton_led3 = null;private ToggleButton mToggleButton_ledrandom = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.leddemo);initUI();}/** * * initUI: 初始化UI * * @param * @return * @throws */public void initUI(){mToggleButton_led0 = (ToggleButton)findViewById(R.id.button_led0);mToggleButton_led1 = (ToggleButton)findViewById(R.id.button_led1);mToggleButton_led2 = (ToggleButton)findViewById(R.id.button_led2);mToggleButton_led3 = (ToggleButton)findViewById(R.id.button_led3);mToggleButton_ledrandom = (ToggleButton)findViewById(R.id.button_ledrandom);mToggleButton_led0.setOnCheckedChangeListener(this);mToggleButton_led1.setOnCheckedChangeListener(this);mToggleButton_led2.setOnCheckedChangeListener(this);mToggleButton_led3.setOnCheckedChangeListener(this);mToggleButton_ledrandom.setOnCheckedChangeListener(this);}/** * * onCheckedChanged: 开关按钮监听器 * * @param buttonView 当前被按下的按钮对象;isChecked表示该按钮的开关状态 * @return * @throws */public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {ToggleButton mToggleButton = (ToggleButton)buttonView;if(isChecked)mledState = ON;elsemledState = OFF;switch (mToggleButton.getId()) {case R.id.button_led0:mledID = LED_0;setLedState(mledID, mledState);break;case R.id.button_led1:mledID = LED_1;setLedState(mledID, mledState);break;case R.id.button_led2:mledID = LED_2;setLedState(mledID, mledState);break;case R.id.button_led3:mledID = LED_3;setLedState(mledID, mledState);break;case R.id.button_ledrandom:if(isChecked){mStop = false;RandomLight();}else{mStop = true;setALlLightsOff();}break;default:break;}}/** * * setLedState: 设置LED灯的开关 * * @param ledID LED灯编号;ledState LED灯的开关状态 * @return true,表示操作成功;否则返回 false。 * @throws */public boolean setLedState(int ledID, int ledState){boolean ret = false;int result = -1;result = HardwareControler.setLedState(ledID, ledState);if(result == 0)ret = true;elseret = false;return ret;}/** * * RandomLight: 随机点亮LED灯 * * @param * @return * @throws */public void RandomLight(){new Thread(){int mledNum = mleds.length;int mrandom = 0;@Overridepublic void run() {while(!mStop){/*从0 1 2 3范围内产生一个整数随机数*/mrandom = (int)(Math.random()*(mledNum));/*随机点亮一盏LED灯,然后关闭其他的LED灯*/for(int i = 0; i <mleds.length; i++){if(i == mrandom){setLedState(mleds[i], ON);}else{setLedState(mleds[i], OFF);}}try {sleep(200);} catch (InterruptedException e) {e.printStackTrace();}}}}.start();}/** * * setALlLightsOff: 熄灭全部的LED灯 * * @param * @return * @throws */public void setALlLightsOff(){for(int i = 0; i <mleds.length; i++){setLedState(mleds[i], OFF);}}/** * * setALlLightsOn: 点亮全部的LED灯 * * @param * @return * @throws */public void setALlLightsOn(){for(int i = 0; i <mleds.length; i++){setLedState(mleds[i], ON);}}}
leddemo.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:id="@+id/linear_led0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:orientation="vertical" > <TextView android:id="@+id/textview_led0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/led0" > </TextView> <ToggleButton android:id="@+id/button_led0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="@string/textoff" android:textOn="@string/texton" > </ToggleButton> </LinearLayout> <LinearLayout android:id="@+id/linear_led1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:orientation="vertical" > <TextView android:id="@+id/textview_led1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/led1" > </TextView> <ToggleButton android:id="@+id/button_led1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="@string/textoff" android:textOn="@string/texton" > </ToggleButton> </LinearLayout> <LinearLayout android:id="@+id/linear_led2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:orientation="vertical" > <TextView android:id="@+id/textview_led2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/led2" > </TextView> <ToggleButton android:id="@+id/button_led2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="@string/textoff" android:textOn="@string/texton" > </ToggleButton> </LinearLayout> <LinearLayout android:id="@+id/linear_led3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:orientation="vertical" > <TextView android:id="@+id/textview_led3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/led3" > </TextView> <ToggleButton android:id="@+id/button_led3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="@string/textoff" android:textOn="@string/texton" > </ToggleButton> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/linear_ledrandom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:orientation="vertical" > <TextView android:id="@+id/textview_ledrandom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/ledrandom" > </TextView> <ToggleButton android:id="@+id/button_ledrandom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textOff="@string/textoff" android:textOn="@string/texton" > </ToggleButton> </LinearLayout></LinearLayout>
预览效果:
- 友善之臂Mini6410之Android开发学习笔记(1)-LED Demo
- 友善之臂Mini6410之Android开发学习笔记(2)-PWM Demo
- 友善之臂Mini6410之Android开发学习笔记(3)-ADC Demo
- 友善之臂Mini6410之Android开发学习笔记(4)-EEPROM Demo
- 友善之臂 Mini6410 Android 2.3 编译
- 移植Qt4.7到友善之臂mini6410开发板全过程
- 友善之臂MINI6410板子移植Linux出现串口乱码
- 友善之臂最新版mini2440学习笔记——开发板架构和搭建开发环境
- JNI基础实验一:调用.so文件--友善之臂Tiny210 android 串口/pwm/ADC/LED
- 友善之臂4412开发板android应用层通过jni直接控制除LED、蜂鸣器外的其它IO口,系统直接重启问题
- 友善之臂第一个裸板驱动LED程序的详细分析(新手向)
- 安卓驱动学习笔记1----关于友善之臂SD卡烧写的问题
- 友善之臂最新版mini2440学习笔记——u-boot 1.1.6移植(一)
- 友善之臂tiny4412-如何搭建android开发环境(64位操作系统)
- mini6410----led驱动学习
- 友善之臂视频监控方案源码学习(1)
- 友善之臂随笔
- 友善之臂使用
- Unix文件系统
- poj 3037 Skiing
- 从src包编译内核【精简版】
- js 分页代码
- ubuntu su root出现鉴定故障
- 友善之臂Mini6410之Android开发学习笔记(1)-LED Demo
- Proxool pool的配置
- c 语言中的 volitile 限定符
- 最短路总结
- HTML5目前无法实现的5件事
- java学习笔记--第七天
- 净空法师法语:你想真正消业障,要用真心待人接物!南无阿弥陀佛!
- 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法
- 探索Hadoop outputFormat