友善之臂Mini6410之Android开发学习笔记


友善之臂Mini6410之Android开发学习笔记源码同步更新,请使用git工具进行同步。

  1. git clone https://code.google.com/p/androiddemoformini6410/  

LEDActivity.java

  1. package com.mini6410.LED;  
  2.   
  3. import com.friendlyarm.AndroidSDK.HardwareControler;  
  4. import com.mini6410.R;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.widget.CompoundButton;  
  9. import android.widget.ToggleButton;  
  10.   
  11. /** 
  12.  *  
  13.  * ClassName:LEDActivity 
  14.  * Reason:   LED Demo 
  15.  * 
  16.  * @author   snowdream 
  17.  * @version   
  18.  * @since    Ver 1.1 
  19.  * @Date     2011   2012-03-11      16:07 
  20.  * 
  21.  * @see       
  22.  */  
  23. public class LEDActivity extends Activity implements ToggleButton.OnCheckedChangeListener {  
  24.   
  25.     /*四个LED灯,编号ID依次为:LED 0,LED_1,LED_2,LED_3*/  
  26.     public static final int LED_0 = 0;  
  27.     public static final int LED_1 = 1;  
  28.     public static final int LED_2 = 2;  
  29.     public static final int LED_3 = 3;  
  30.   
  31.     /*LED灯的状态: ON 表示点亮, OFF表示熄灭*/  
  32.     public static final int OFF = 0;  
  33.     public static final int ON = 1;  
  34.   
  35.   
  36.     private int mledID = LED_0;   
  37.     private int mledState = OFF;  
  38.   
  39.   
  40.     private boolean mStop = false;  
  41.   
  42.     /*LED编号数组*/  
  43.     private int[] mleds = new int[]{LED_0,LED_1,LED_2,LED_3};  
  44.   
  45.     /*5个开关按钮*/  
  46.     private ToggleButton mToggleButton_led0 = null;  
  47.     private ToggleButton mToggleButton_led1 = null;  
  48.     private ToggleButton mToggleButton_led2 = null;  
  49.     private ToggleButton mToggleButton_led3 = null;  
  50.     private ToggleButton mToggleButton_ledrandom = null;  
  51.   
  52.   
  53.     @Override  
  54.     protected void onCreate(Bundle savedInstanceState) {  
  55.         super.onCreate(savedInstanceState);  
  56.         setContentView(R.layout.leddemo);  
  57.   
  58.         initUI();  
  59.     }  
  60.   
  61.     /** 
  62.      *  
  63.      * initUI: 初始化UI 
  64.      * 
  65.      * @param    
  66.      * @return      
  67.      * @throws  
  68.      */  
  69.     public void initUI(){  
  70.         mToggleButton_led0 = (ToggleButton)findViewById(R.id.button_led0);  
  71.         mToggleButton_led1 = (ToggleButton)findViewById(R.id.button_led1);  
  72.         mToggleButton_led2 = (ToggleButton)findViewById(R.id.button_led2);  
  73.         mToggleButton_led3 =  (ToggleButton)findViewById(R.id.button_led3);  
  74.         mToggleButton_ledrandom = (ToggleButton)findViewById(R.id.button_ledrandom);  
  75.   
  76.         mToggleButton_led0.setOnCheckedChangeListener(this);  
  77.         mToggleButton_led1.setOnCheckedChangeListener(this);  
  78.         mToggleButton_led2.setOnCheckedChangeListener(this);  
  79.         mToggleButton_led3.setOnCheckedChangeListener(this);  
  80.         mToggleButton_ledrandom.setOnCheckedChangeListener(this);         
  81.     }  
  82.   
  83.     /** 
  84.      *  
  85.      * onCheckedChanged: 开关按钮监听器 
  86.      * 
  87.      * @param   buttonView 当前被按下的按钮对象;isChecked表示该按钮的开关状态 
  88.      * @return      
  89.      * @throws  
  90.      */  
  91.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  92.         ToggleButton mToggleButton = (ToggleButton)buttonView;  
  93.   
  94.         if(isChecked)  
  95.             mledState = ON;  
  96.         else  
  97.             mledState = OFF;  
  98.   
  99.         switch (mToggleButton.getId()) {  
  100.         case R.id.button_led0:  
  101.             mledID = LED_0;  
  102.             setLedState(mledID, mledState);  
  103.             break;  
  104.         case R.id.button_led1:  
  105.             mledID = LED_1;  
  106.             setLedState(mledID, mledState);       
  107.             break;  
  108.         case R.id.button_led2:  
  109.             mledID = LED_2;  
  110.             setLedState(mledID, mledState);               
  111.             break;  
  112.         case R.id.button_led3:  
  113.             mledID = LED_3;  
  114.             setLedState(mledID, mledState);               
  115.             break;    
  116.         case R.id.button_ledrandom:  
  117.             if(isChecked){  
  118.                 mStop = false;  
  119.                 RandomLight();  
  120.             }else{  
  121.                 mStop = true;  
  122.                 setALlLightsOff();  
  123.             }  
  124.             break;    
  125.         default:  
  126.             break;  
  127.         }  
  128.     }  
  129.   
  130.     /** 
  131.      *  
  132.      * setLedState: 设置LED灯的开关 
  133.      * 
  134.      * @param   ledID LED灯编号;ledState LED灯的开关状态 
  135.      * @return     true,表示操作成功;否则返回 false。 
  136.      * @throws  
  137.      */  
  138.     public boolean setLedState(int ledID, int ledState){  
  139.         boolean ret = false;  
  140.         int result = -1;  
  141.   
  142.         result = HardwareControler.setLedState(ledID, ledState);  
  143.   
  144.         if(result == 0)  
  145.             ret = true;  
  146.         else  
  147.             ret = false;  
  148.   
  149.         return ret;  
  150.     }  
  151.   
  152.   
  153.     /** 
  154.      *  
  155.      * RandomLight: 随机点亮LED灯 
  156.      * 
  157.      * @param   
  158.      * @return      
  159.      * @throws  
  160.      */  
  161.     public void RandomLight(){  
  162.         new Thread(){  
  163.   
  164.             int mledNum =  mleds.length;  
  165.             int mrandom = 0;  
  166.   
  167.             @Override  
  168.             public void run() {  
  169.   
  170.                 while(!mStop){  
  171.                     /*从0 1 2 3范围内产生一个整数随机数*/  
  172.                     mrandom = (int)(Math.random()*(mledNum));  
  173.   
  174.                     /*随机点亮一盏LED灯,然后关闭其他的LED灯*/  
  175.                     for(int i = 0; i <mleds.length; i++){  
  176.                         if(i == mrandom){  
  177.                             setLedState(mleds[i], ON);  
  178.                         }else{  
  179.                             setLedState(mleds[i], OFF);  
  180.                         }  
  181.   
  182.                     }  
  183.   
  184.                     try {  
  185.                         sleep(200);  
  186.                     } catch (InterruptedException e) {  
  187.                         e.printStackTrace();  
  188.                     }  
  189.                 }  
  190.   
  191.             }}.start();  
  192.     }  
  193.   
  194.     /** 
  195.      *  
  196.      * setALlLightsOff: 熄灭全部的LED灯 
  197.      * 
  198.      * @param   
  199.      * @return      
  200.      * @throws  
  201.      */  
  202.     public void setALlLightsOff(){  
  203.         for(int i = 0; i <mleds.length; i++){  
  204.             setLedState(mleds[i], OFF);  
  205.         }  
  206.     }  
  207.   
  208.     /** 
  209.      *  
  210.      * setALlLightsOn: 点亮全部的LED灯 
  211.      * 
  212.      * @param   
  213.      * @return      
  214.      * @throws  
  215.      */  
  216.     public void setALlLightsOn(){  
  217.         for(int i = 0; i <mleds.length; i++){  
  218.             setLedState(mleds[i], ON);  
  219.         }  
  220.     }     
  221. }  

leddemo.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal" >  
  11.   
  12.         <LinearLayout  
  13.             android:id="@+id/linear_led0"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_margin="5dip"  
  17.             android:orientation="vertical" >  
  18.   
  19.             <TextView  
  20.                 android:id="@+id/textview_led0"  
  21.                 android:layout_width="wrap_content"  
  22.                 android:layout_height="wrap_content"  
  23.                 android:layout_gravity="center"  
  24.                 android:text="@string/led0" >  
  25.             </TextView>  
  26.   
  27.             <ToggleButton  
  28.                 android:id="@+id/button_led0"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"   
  31.                 android:textOff="@string/textoff"  
  32.                 android:textOn="@string/texton" >  
  33.             </ToggleButton>  
  34.         </LinearLayout>  
  35.   
  36.         <LinearLayout  
  37.             android:id="@+id/linear_led1"  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:layout_margin="5dip"  
  41.             android:orientation="vertical" >  
  42.   
  43.             <TextView  
  44.                 android:id="@+id/textview_led1"  
  45.                 android:layout_width="wrap_content"  
  46.                 android:layout_height="wrap_content"  
  47.                 android:layout_gravity="center"  
  48.                 android:text="@string/led1" >  
  49.             </TextView>  
  50.   
  51.             <ToggleButton  
  52.                 android:id="@+id/button_led1"  
  53.                 android:layout_width="wrap_content"  
  54.                 android:layout_height="wrap_content"   
  55.                 android:textOff="@string/textoff"  
  56.                 android:textOn="@string/texton" >  
  57.             </ToggleButton>  
  58.         </LinearLayout>  
  59.   
  60.         <LinearLayout  
  61.             android:id="@+id/linear_led2"  
  62.             android:layout_width="wrap_content"  
  63.             android:layout_height="wrap_content"  
  64.             android:layout_margin="5dip"  
  65.             android:orientation="vertical" >  
  66.   
  67.             <TextView  
  68.                 android:id="@+id/textview_led2"  
  69.                 android:layout_width="wrap_content"  
  70.                 android:layout_height="wrap_content"  
  71.                 android:layout_gravity="center"  
  72.                 android:text="@string/led2" >  
  73.             </TextView>  
  74.   
  75.             <ToggleButton  
  76.                 android:id="@+id/button_led2"  
  77.                 android:layout_width="wrap_content"  
  78.                 android:layout_height="wrap_content"   
  79.                 android:textOff="@string/textoff"  
  80.                 android:textOn="@string/texton" >  
  81.             </ToggleButton>  
  82.         </LinearLayout>  
  83.   
  84.         <LinearLayout  
  85.             android:id="@+id/linear_led3"  
  86.             android:layout_width="wrap_content"  
  87.             android:layout_height="wrap_content"  
  88.             android:layout_margin="5dip"  
  89.             android:orientation="vertical" >  
  90.   
  91.             <TextView  
  92.                 android:id="@+id/textview_led3"  
  93.                 android:layout_width="wrap_content"  
  94.                 android:layout_height="wrap_content"  
  95.                 android:layout_gravity="center"  
  96.                 android:text="@string/led3" >  
  97.             </TextView>  
  98.   
  99.             <ToggleButton  
  100.                 android:id="@+id/button_led3"  
  101.                 android:layout_width="wrap_content"  
  102.                 android:layout_height="wrap_content"  
  103.                 android:textOff="@string/textoff"  
  104.                 android:textOn="@string/texton" >  
  105.             </ToggleButton>  
  106.         </LinearLayout>  
  107.     </LinearLayout>  
  108.   
  109.     <LinearLayout  
  110.         android:id="@+id/linear_ledrandom"  
  111.         android:layout_width="wrap_content"  
  112.         android:layout_height="wrap_content"  
  113.         android:layout_margin="5dip"  
  114.         android:orientation="vertical" >  
  115.   
  116.         <TextView  
  117.             android:id="@+id/textview_ledrandom"  
  118.             android:layout_width="wrap_content"  
  119.             android:layout_height="wrap_content"  
  120.             android:layout_gravity="center"  
  121.             android:text="@string/ledrandom" >  
  122.         </TextView>  
  123.   
  124.         <ToggleButton  
  125.             android:id="@+id/button_ledrandom"  
  126.             android:layout_width="wrap_content"  
  127.             android:layout_height="wrap_content"  
  128.             android:layout_gravity="center"  
  129.             android:textOff="@string/textoff"  
  130.             android:textOn="@string/texton" >  
  131.         </ToggleButton>  
  132.     </LinearLayout>  
  133.   
  134. </LinearLayout>  

预览效果:

  • 1
  • 2
  • 3
  • 4
  • 下一页
【内容导航】
第1页:LED Demo 第2页:PWM Demo
第3页:ADC Demo 第4页:EEPROM Demo

相关内容