歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 友善之臂Mini6410之Android開發學習筆記

友善之臂Mini6410之Android開發學習筆記

日期:2017/3/1 10:26:27   编辑:Linux編程

友善之臂Mini6410之Android開發學習筆記源碼同步更新,請使用git工具進行同步。

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

LEDActivity.java

  1. package com.mini6410.LED;
  2. import com.friendlyarm.AndroidSDK.HardwareControler;
  3. import com.mini6410.R;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.CompoundButton;
  7. import android.widget.ToggleButton;
  8. /**
  9. *
  10. * ClassName:LEDActivity
  11. * Reason: LED Demo
  12. *
  13. * @author snowdream
  14. * @version
  15. * @since Ver 1.1
  16. * @Date 2011 2012-03-11 16:07
  17. *
  18. * @see
  19. */
  20. public class LEDActivity extends Activity implements ToggleButton.OnCheckedChangeListener {
  21. /*四個LED燈,編號ID依次為:LED 0,LED_1,LED_2,LED_3*/
  22. public static final int LED_0 = 0;
  23. public static final int LED_1 = 1;
  24. public static final int LED_2 = 2;
  25. public static final int LED_3 = 3;
  26. /*LED燈的狀態: ON 表示點亮, OFF表示熄滅*/
  27. public static final int OFF = 0;
  28. public static final int ON = 1;
  29. private int mledID = LED_0;
  30. private int mledState = OFF;
  31. private boolean mStop = false;
  32. /*LED編號數組*/
  33. private int[] mleds = new int[]{LED_0,LED_1,LED_2,LED_3};
  34. /*5個開關按鈕*/
  35. private ToggleButton mToggleButton_led0 = null;
  36. private ToggleButton mToggleButton_led1 = null;
  37. private ToggleButton mToggleButton_led2 = null;
  38. private ToggleButton mToggleButton_led3 = null;
  39. private ToggleButton mToggleButton_ledrandom = null;
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.leddemo);
  44. initUI();
  45. }
  46. /**
  47. *
  48. * initUI: 初始化UI
  49. *
  50. * @param
  51. * @return
  52. * @throws
  53. */
  54. public void initUI(){
  55. mToggleButton_led0 = (ToggleButton)findViewById(R.id.button_led0);
  56. mToggleButton_led1 = (ToggleButton)findViewById(R.id.button_led1);
  57. mToggleButton_led2 = (ToggleButton)findViewById(R.id.button_led2);
  58. mToggleButton_led3 = (ToggleButton)findViewById(R.id.button_led3);
  59. mToggleButton_ledrandom = (ToggleButton)findViewById(R.id.button_ledrandom);
  60. mToggleButton_led0.setOnCheckedChangeListener(this);
  61. mToggleButton_led1.setOnCheckedChangeListener(this);
  62. mToggleButton_led2.setOnCheckedChangeListener(this);
  63. mToggleButton_led3.setOnCheckedChangeListener(this);
  64. mToggleButton_ledrandom.setOnCheckedChangeListener(this);
  65. }
  66. /**
  67. *
  68. * onCheckedChanged: 開關按鈕監聽器
  69. *
  70. * @param buttonView 當前被按下的按鈕對象;isChecked表示該按鈕的開關狀態
  71. * @return
  72. * @throws
  73. */
  74. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  75. ToggleButton mToggleButton = (ToggleButton)buttonView;
  76. if(isChecked)
  77. mledState = ON;
  78. else
  79. mledState = OFF;
  80. switch (mToggleButton.getId()) {
  81. case R.id.button_led0:
  82. mledID = LED_0;
  83. setLedState(mledID, mledState);
  84. break;
  85. case R.id.button_led1:
  86. mledID = LED_1;
  87. setLedState(mledID, mledState);
  88. break;
  89. case R.id.button_led2:
  90. mledID = LED_2;
  91. setLedState(mledID, mledState);
  92. break;
  93. case R.id.button_led3:
  94. mledID = LED_3;
  95. setLedState(mledID, mledState);
  96. break;
  97. case R.id.button_ledrandom:
  98. if(isChecked){
  99. mStop = false;
  100. RandomLight();
  101. }else{
  102. mStop = true;
  103. setALlLightsOff();
  104. }
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. /**
  111. *
  112. * setLedState: 設置LED燈的開關
  113. *
  114. * @param ledID LED燈編號;ledState LED燈的開關狀態
  115. * @return true,表示操作成功;否則返回 false。
  116. * @throws
  117. */
  118. public boolean setLedState(int ledID, int ledState){
  119. boolean ret = false;
  120. int result = -1;
  121. result = HardwareControler.setLedState(ledID, ledState);
  122. if(result == 0)
  123. ret = true;
  124. else
  125. ret = false;
  126. return ret;
  127. }
  128. /**
  129. *
  130. * RandomLight: 隨機點亮LED燈
  131. *
  132. * @param
  133. * @return
  134. * @throws
  135. */
  136. public void RandomLight(){
  137. new Thread(){
  138. int mledNum = mleds.length;
  139. int mrandom = 0;
  140. @Override
  141. public void run() {
  142. while(!mStop){
  143. /*從0 1 2 3范圍內產生一個整數隨機數*/
  144. mrandom = (int)(Math.random()*(mledNum));
  145. /*隨機點亮一盞LED燈,然後關閉其他的LED燈*/
  146. for(int i = 0; i <mleds.length; i++){
  147. if(i == mrandom){
  148. setLedState(mleds[i], ON);
  149. }else{
  150. setLedState(mleds[i], OFF);
  151. }
  152. }
  153. try {
  154. sleep(200);
  155. } catch (InterruptedException e) {
  156. e.printStackTrace();
  157. }
  158. }
  159. }}.start();
  160. }
  161. /**
  162. *
  163. * setALlLightsOff: 熄滅全部的LED燈
  164. *
  165. * @param
  166. * @return
  167. * @throws
  168. */
  169. public void setALlLightsOff(){
  170. for(int i = 0; i <mleds.length; i++){
  171. setLedState(mleds[i], OFF);
  172. }
  173. }
  174. /**
  175. *
  176. * setALlLightsOn: 點亮全部的LED燈
  177. *
  178. * @param
  179. * @return
  180. * @throws
  181. */
  182. public void setALlLightsOn(){
  183. for(int i = 0; i <mleds.length; i++){
  184. setLedState(mleds[i], ON);
  185. }
  186. }
  187. }

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

預覽效果:

Copyright © Linux教程網 All Rights Reserved