LEDActivity.java
- packagecom.mini6410.LED;
- importcom.friendlyarm.AndroidSDK.HardwareControler;
- importcom.mini6410.R;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.CompoundButton;
- importandroid.widget.ToggleButton;
- /**
- *
- *ClassName:LEDActivity
- *Reason:LEDDemo
- *
- *@authorsnowdream
- *@version
- *@sinceVer1.1
- *@Date20112012-03-1116:07
- *
- *@see
- */
- publicclassLEDActivityextendsActivityimplementsToggleButton.OnCheckedChangeListener{
- /*四个LED灯,编号ID依次为:LED0,LED_1,LED_2,LED_3*/
- publicstaticfinalintLED_0=0;
- publicstaticfinalintLED_1=1;
- publicstaticfinalintLED_2=2;
- publicstaticfinalintLED_3=3;
- /*LED灯的状态:ON表示点亮,OFF表示熄灭*/
- publicstaticfinalintOFF=0;
- publicstaticfinalintON=1;
- privateintmledID=LED_0;
- privateintmledState=OFF;
- privatebooleanmStop=false;
- /*LED编号数组*/
- privateint[]mleds=newint[]{LED_0,LED_1,LED_2,LED_3};
- /*5个开关按钮*/
- privateToggleButtonmToggleButton_led0=null;
- privateToggleButtonmToggleButton_led1=null;
- privateToggleButtonmToggleButton_led2=null;
- privateToggleButtonmToggleButton_led3=null;
- privateToggleButtonmToggleButton_ledrandom=null;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.leddemo);
- initUI();
- }
- /**
- *
- *initUI:初始化UI
- *
- *@param
- *@return
- *@throws
- */
- publicvoidinitUI(){
- 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:开关按钮监听器
- *
- *@parambuttonView当前被按下的按钮对象;isChecked表示该按钮的开关状态
- *@return
- *@throws
- */
- publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
- ToggleButtonmToggleButton=(ToggleButton)buttonView;
- if(isChecked)
- mledState=ON;
- else
- mledState=OFF;
- switch(mToggleButton.getId()){
- caseR.id.button_led0:
- mledID=LED_0;
- setLedState(mledID,mledState);
- break;
- caseR.id.button_led1:
- mledID=LED_1;
- setLedState(mledID,mledState);
- break;
- caseR.id.button_led2:
- mledID=LED_2;
- setLedState(mledID,mledState);
- break;
- caseR.id.button_led3:
- mledID=LED_3;
- setLedState(mledID,mledState);
- break;
- caseR.id.button_ledrandom:
- if(isChecked){
- mStop=false;
- RandomLight();
- }else{
- mStop=true;
- setALlLightsOff();
- }
- break;
- default:
- break;
- }
- }
- /**
- *
- *setLedState:设置LED灯的开关
- *
- *@paramledIDLED灯编号;ledStateLED灯的开关状态
- *@returntrue,表示操作成功;否则返回false。
- *@throws
- */
- publicbooleansetLedState(intledID,intledState){
- booleanret=false;
- intresult=-1;
- result=HardwareControler.setLedState(ledID,ledState);
- if(result==0)
- ret=true;
- else
- ret=false;
- returnret;
- }
- /**
- *
- *RandomLight:随机点亮LED灯
- *
- *@param
- *@return
- *@throws
- */
- publicvoidRandomLight(){
- newThread(){
- intmledNum=mleds.length;
- intmrandom=0;
- @Override
- publicvoidrun(){
- while(!mStop){
- /*从0123范围内产生一个整数随机数*/
- mrandom=(int)(Math.random()*(mledNum));
- /*随机点亮一盏LED灯,然后关闭其他的LED灯*/
- for(inti=0;i<mleds.length;i++){
- if(i==mrandom){
- setLedState(mleds[i],ON);
- }else{
- setLedState(mleds[i],OFF);
- }
- }
- try{
- sleep(200);
- }catch(InterruptedExceptione){
- e.printStackTrace();
- }
- }
- }}.start();
- }
- /**
- *
- *setALlLightsOff:熄灭全部的LED灯
- *
- *@param
- *@return
- *@throws
- */
- publicvoidsetALlLightsOff(){
- for(inti=0;i<mleds.length;i++){
- setLedState(mleds[i],OFF);
- }
- }
- /**
- *
- *setALlLightsOn:点亮全部的LED灯
- *
- *@param
- *@return
- *@throws
- */
- publicvoidsetALlLightsOn(){
- for(inti=0;i<mleds.length;i++){
- setLedState(mleds[i],ON);
- }
- }
- }
leddemo.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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>
预览效果: