2025-05-23

[html] <span style="font-family:Arial;">git clone https://code.google.com/p/androiddemoformini6410/</span> 
<span style="font-family:Arial;">git clone https://code.google.com/p/androiddemoformini6410/</span>

 

 

LEDActivity.java

[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     2011   2012-03-11      16: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; 
 
 
    @Override 
    protected 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; 
        else 
            mledState = 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; 
        else 
            ret = false; 
 
        return ret; 
    } 
 
 
    /**
     * 
     * RandomLight: 隨機點亮LED燈
     *
     * @param  
     * @return     
     * @throws 
     */ 
    public void RandomLight(){ 
        new Thread(){ 
 
            int mledNum =  mleds.length; 
            int mrandom = 0; 
 
            @Override 
            public 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); 
        } 
    }    

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  2011 2012-03-11  16: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;

 @Override
 protected 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;
  else
   mledState = 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;
  else
   ret = false;

  return ret;
 }

 /**
  *
  * RandomLight: 隨機點亮LED燈
  *
  * @param 
  * @return    
  * @throws
  */
 public void RandomLight(){
  new Thread(){

   int mledNum =  mleds.length;
   int mrandom = 0;

   @Override
   public 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

[html] <?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> 
<?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>
預覽效果:
  

摘自  snowdream
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *