這份筆記記錄在 FriendlyARM Mini6410 板子上跑 Android 的過程,以最經典的 LED 點亮/熄滅作為第一個範例。原始碼有持續同步更新,可用 git 取回:
git clone https://code.google.com/p/androiddemoformini6410/
板子上有四顆 LED,編號 0 到 3,透過 FriendlyARM 提供的 HardwareControler 類別操作;介面則用五個 ToggleButton,前四顆各自對應一盞燈,第五個負責隨機閃爍。
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;
public class LEDActivity extends Activity
implements ToggleButton.OnCheckedChangeListener {
/* 四個 LED 燈,編號依次為 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 狀態:OFF 熄滅,ON 點亮 */
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;
private int[] mleds = new int[]{LED_0, LED_1, LED_2, LED_3};
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();
}
/** 初始化介面與監聽器 */
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);
}
/** 開關按鈕的監聽器 */
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ToggleButton mToggleButton = (ToggleButton) buttonView;
mledState = isChecked ? ON : 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;
}
}
/** 設定單一 LED 的開關,回傳 true 表示成功 */
public boolean setLedState(int ledID, int ledState) {
int result = HardwareControler.setLedState(ledID, ledState);
return result == 0;
}
/** 隨機點亮一盞 LED,其餘熄滅 */
public void RandomLight() {
new Thread() {
int mledNum = mleds.length;
int mrandom = 0;
@Override
public void run() {
while (!mStop) {
mrandom = (int) (Math.random() * mledNum);
for (int i = 0; i < mleds.length; i++) {
setLedState(mleds[i], i == mrandom ? ON : OFF);
}
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
public void setALlLightsOff() {
for (int i = 0; i < mleds.length; i++) {
setLedState(mleds[i], OFF);
}
}
public void setALlLightsOn() {
for (int i = 0; i < mleds.length; i++) {
setLedState(mleds[i], ON);
}
}
}
幾個實作上的重點:LED 的實際控制被收斂到 setLedState 一個方法裡,回傳值 0 代表成功,其他層級不必知道硬體細節;隨機閃爍刻意放在背景執行緒,並用 mStop 旗標控制退出,避免無限迴圈關不掉;五個 ToggleButton 共用同一個 listener,靠 getId() 分派,比寫五個匿名類別清爽得多。
版面用垂直 LinearLayout 包一層水平 LinearLayout,四個 LED 各佔一格,每格是「TextView 標籤 + ToggleButton」的垂直組合,最後一列放隨機閃爍的開關:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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" />
<ToggleButton
android:id="@+id/button_led0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="@string/textoff"
android:textOn="@string/texton" />
</LinearLayout>
<!-- linear_led1、linear_led2、linear_led3 結構相同,
僅 id 與 text 指向 @string/led1 ~ led3 -->
</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" />
<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" />
</LinearLayout>
</LinearLayout>
四格 LED 的 XML 幾乎一模一樣,只差 id 與字串資源,因此上面只完整寫出第一格,其餘以註解帶過。記得在 strings.xml 補上 led0 至 led3、ledrandom、texton、textoff 這些資源,否則編譯會直接報錯。
跑起來之後,每按一個開關就切換對應 LED,按下隨機閃爍則會看到四顆燈每 200 毫秒跳一次。這是最小的硬體抽象範例,之後要接蜂鳴器或暫存器操作,都能沿用同樣的結構。