LED Demo

LEDActivity.java

  1. packagecom.mini6410.LED;
  2. importcom.friendlyarm.AndroidSDK.HardwareControler;
  3. importcom.mini6410.R;
  4. importandroid.app.Activity;
  5. importandroid.os.Bundle;
  6. importandroid.widget.CompoundButton;
  7. importandroid.widget.ToggleButton;
  8. /**
  9. *
  10. *ClassName:LEDActivity
  11. *Reason:LEDDemo
  12. *
  13. *@authorsnowdream
  14. *@version
  15. *@sinceVer1.1
  16. *@Date20112012-03-1116:07
  17. *
  18. *@see
  19. */
  20. publicclassLEDActivityextendsActivityimplementsToggleButton.OnCheckedChangeListener{
  21. /*四个LED灯,编号ID依次为:LED0,LED_1,LED_2,LED_3*/
  22. publicstaticfinalintLED_0=0;
  23. publicstaticfinalintLED_1=1;
  24. publicstaticfinalintLED_2=2;
  25. publicstaticfinalintLED_3=3;
  26. /*LED灯的状态:ON表示点亮,OFF表示熄灭*/
  27. publicstaticfinalintOFF=0;
  28. publicstaticfinalintON=1;
  29. privateintmledID=LED_0;
  30. privateintmledState=OFF;
  31. privatebooleanmStop=false;
  32. /*LED编号数组*/
  33. privateint[]mleds=newint[]{LED_0,LED_1,LED_2,LED_3};
  34. /*5个开关按钮*/
  35. privateToggleButtonmToggleButton_led0=null;
  36. privateToggleButtonmToggleButton_led1=null;
  37. privateToggleButtonmToggleButton_led2=null;
  38. privateToggleButtonmToggleButton_led3=null;
  39. privateToggleButtonmToggleButton_ledrandom=null;
  40. @Override
  41. protectedvoidonCreate(BundlesavedInstanceState){
  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. publicvoidinitUI(){
  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. *@parambuttonView当前被按下的按钮对象;isChecked表示该按钮的开关状态
  71. *@return
  72. *@throws
  73. */
  74. publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){
  75. ToggleButtonmToggleButton=(ToggleButton)buttonView;
  76. if(isChecked)
  77. mledState=ON;
  78. else
  79. mledState=OFF;
  80. switch(mToggleButton.getId()){
  81. caseR.id.button_led0:
  82. mledID=LED_0;
  83. setLedState(mledID,mledState);
  84. break;
  85. caseR.id.button_led1:
  86. mledID=LED_1;
  87. setLedState(mledID,mledState);
  88. break;
  89. caseR.id.button_led2:
  90. mledID=LED_2;
  91. setLedState(mledID,mledState);
  92. break;
  93. caseR.id.button_led3:
  94. mledID=LED_3;
  95. setLedState(mledID,mledState);
  96. break;
  97. caseR.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. *@paramledIDLED灯编号;ledStateLED灯的开关状态
  115. *@returntrue,表示操作成功;否则返回false。
  116. *@throws
  117. */
  118. publicbooleansetLedState(intledID,intledState){
  119. booleanret=false;
  120. intresult=-1;
  121. result=HardwareControler.setLedState(ledID,ledState);
  122. if(result==0)
  123. ret=true;
  124. else
  125. ret=false;
  126. returnret;
  127. }
  128. /**
  129. *
  130. *RandomLight:随机点亮LED灯
  131. *
  132. *@param
  133. *@return
  134. *@throws
  135. */
  136. publicvoidRandomLight(){
  137. newThread(){
  138. intmledNum=mleds.length;
  139. intmrandom=0;
  140. @Override
  141. publicvoidrun(){
  142. while(!mStop){
  143. /*从0123范围内产生一个整数随机数*/
  144. mrandom=(int)(Math.random()*(mledNum));
  145. /*随机点亮一盏LED灯,然后关闭其他的LED灯*/
  146. for(inti=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(InterruptedExceptione){
  156. e.printStackTrace();
  157. }
  158. }
  159. }}.start();
  160. }
  161. /**
  162. *
  163. *setALlLightsOff:熄灭全部的LED灯
  164. *
  165. *@param
  166. *@return
  167. *@throws
  168. */
  169. publicvoidsetALlLightsOff(){
  170. for(inti=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. publicvoidsetALlLightsOn(){
  183. for(inti=0;i<mleds.length;i++){
  184. setLedState(mleds[i],ON);
  185. }
  186. }
  187. }

leddemo.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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>

预览效果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值