Android Things之闪烁的LED灯
上节讲了开发Android Things应用时常用的一些操作命令,不明白的可以查看http://www.jianshu.com/p/08f6785ff5aa
本节内容讲如何控制LED灯并闪烁, 先来看下电路连接图和实际效果图
电路连接图
实际效果图
准备材料
- 开发板
- 面包板
- 电阻
- 两根公-母杜邦线
- LED灯
按效果图接好各个组件
创建AS工程并配置项目
在模块下的build.gradle需引入Android Things
provided 'com.google.android.things:androidthings:0.5.1-devpreview'
应用必须引入Android Things库,在<application>节点下配置以下代码
<uses-library android:name="com.google.android.things"/>
在AndroidManifest.xml中,往<activity>节点中配置一个<intent-filter>过滤器,表示设备启动时需要启动的类
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.IOT_LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
具体代码
<application android:allowBackup="true" android:icon="@android:drawable/sym_def_app_icon"
android:label="@string/app_name">
<uses-library android:name="com.google.android.things"/>
<activity android:name=".BlinkActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 设备启动后自动打开Activity -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.IOT_LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
获取开发板的名称
Android Things支持的开发板有多种类型,所以引脚对应的名称也不一样,所以第一步先获取开发板的名称
private static String getBoardVariant() {
if (!sBoardVariant.isEmpty()) {
return sBoardVariant;
}
sBoardVariant = Build.DEVICE;
Log.e(TAG, sBoardVariant);
if (sBoardVariant.equals(DEVICE_EDISON)) {
PeripheralManagerService pioService = new PeripheralManagerService();
List<String> gpioList = pioService.getGpioList();
if (gpioList.size() != 0) {
String pin = gpioList.get(0);
if (pin.startsWith("IO")) {
sBoardVariant = DEVICE_EDISON_ARDUINO;
}
}
}
return sBoardVariant;
}
sBoardVariant是一个静态变量,代表开发板的名称,如果已经有值则返回,没有则用Build.DEVICE获取开发板名字,获取到开发板名称后,如果开发板是edison系列,需要检查引脚前缀,因为edison和edison_arduino开发板用Build.DEVICE获取的名称相同,而引脚名称却不同,所以使用PeripheralManagerService外围设备管理器调用getGpioList()获取设备所有的Gpio引脚名称,在不为空的情况下,检查引脚名称是否以IO开头,如果是,则开发板的是edison_arduino,否则是edison。如果开发板不是edison就不需要检查,Build.DEVICE获取到的名称是开发板的真实名称。
获取连接LED灯的引脚名称
上一个步骤已经获取到开发板的名称,接下来通过开发板的名称获取引脚的名称
public static String getGPIOForLED() {
//getBoardVariant()为开发板的名称
switch (getBoardVariant()) {
case DEVICE_EDISON_ARDUINO:
return "IO13";
case DEVICE_EDISON:
return "GP45";
case DEVICE_JOULE:
return "J6_25";
case DEVICE_RPI3:
return "BCM6";
case DEVICE_IMX6UL_PICO:
return "GPIO4_IO22";
case DEVICE_IMX6UL_VVDN:
return "GPIO3_IO06";
case DEVICE_IMX7D_PICO:
return "GPIO_34";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}
我所使用的开发板是树莓派,所以连接LED灯的引脚名称是BCM6,这些名称如何得知需要看开发板的电路原理图,每个引脚的名称、用途会有说明,学的越来越底层了,~.~
打开引脚
//声明外围设备管理器
PeripheralManagerService service = new PeripheralManagerService();
//引脚名称
String pinName = BoardDefaults.getGPIOForLED();
//打开引脚
mLedGpio = service.openGpio(pinName);
//设置为输出引脚并初始化为低电平
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
//因为初始化输出引脚为低电平,所以LED灯不会亮,这是这是为true
//后LED会亮起来
mLedGpio.setValue(true);
如何让LED灯闪烁起来
使用异步类实现,这里使用Handler
private Runnable mBlinkRunnable = new Runnable() {
@Override
public void run() {
try {
// 切换电平状态
mLedState = !mLedState;
mLedGpio.setValue(mLedState);
Log.d(TAG, "State set to " + mLedState);
// 每隔1000ms调用该接口
mHandler.postDelayed(mBlinkRunnable, 1000);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//运行接口
mHandler.post(mBlinkRunnable);
}
private Handler mHandler = new Handler();
以上实例是部分代码下面贴出完整的代码
获取LED引脚名称代码
import android.os.Build;
import android.util.Log;
import com.google.android.things.pio.PeripheralManagerService;
import java.util.List;
@SuppressWarnings("WeakerAccess")
public class BoardDefaults {
private static final String TAG = BoardDefaults.class.getName();
private static final String DEVICE_EDISON_ARDUINO = "edison_arduino";
private static final String DEVICE_EDISON = "edison";
private static final String DEVICE_JOULE = "joule";
private static final String DEVICE_RPI3 = "rpi3";
private static final String DEVICE_IMX6UL_PICO = "imx6ul_pico";
private static final String DEVICE_IMX6UL_VVDN = "imx6ul_iopb";
private static final String DEVICE_IMX7D_PICO = "imx7d_pico";
private static String sBoardVariant = "";
/**
* Return the GPIO pin that the LED is connected on.
* For example, on Intel Edison Arduino breakout, pin "IO13" is connected to an onboard LED
* that turns on when the GPIO pin is HIGH, and off when low.
*/
public static String getGPIOForLED() {
switch (getBoardVariant()) {
case DEVICE_EDISON_ARDUINO:
return "IO13";
case DEVICE_EDISON:
return "GP45";
case DEVICE_JOULE:
return "J6_25";
case DEVICE_RPI3:
return "BCM6";
case DEVICE_IMX6UL_PICO:
return "GPIO4_IO22";
case DEVICE_IMX6UL_VVDN:
return "GPIO3_IO06";
case DEVICE_IMX7D_PICO:
return "GPIO_34";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}
private static String getBoardVariant() {
if (!sBoardVariant.isEmpty()) {
return sBoardVariant;
}
sBoardVariant = Build.DEVICE;
Log.e(TAG, sBoardVariant);
// For the edison check the pin prefix
// to always return Edison Breakout pin name when applicable.
if (sBoardVariant.equals(DEVICE_EDISON)) {
PeripheralManagerService pioService = new PeripheralManagerService();
List<String> gpioList = pioService.getGpioList();
if (gpioList.size() != 0) {
String pin = gpioList.get(0);
if (pin.startsWith("IO")) {
sBoardVariant = DEVICE_EDISON_ARDUINO;
}
}
}
return sBoardVariant;
}
}
让LED灯闪烁完整代码
/**
* Sample usage of the Gpio API that blinks an LED at a fixed interval defined in
* {@link #INTERVAL_BETWEEN_BLINKS_MS}.
*
* Some boards, like Intel Edison, have onboard LEDs linked to specific GPIO pins.
* The preferred GPIO pin to use on each board is in the {@link BoardDefaults} class.
*
*/
public class BlinkActivity extends Activity {
private static final String TAG = BlinkActivity.class.getSimpleName();
private static final int INTERVAL_BETWEEN_BLINKS_MS = 2000;
private Handler mHandler = new Handler();
private Gpio mLedGpio;
private boolean mLedState = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting BlinkActivity");
PeripheralManagerService service = new PeripheralManagerService();
try {
String pinName = BoardDefaults.getGPIOForLED();
mLedGpio = service.openGpio(pinName);
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
Log.i(TAG, "Start blinking LED GPIO pin");
// Post a Runnable that continuously switch the state of the GPIO, blinking the
// corresponding LED
mHandler.post(mBlinkRunnable);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Remove pending blink Runnable from the handler.
mHandler.removeCallbacks(mBlinkRunnable);
// Close the Gpio pin.
Log.i(TAG, "Closing LED GPIO pin");
try {
mLedGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
} finally {
mLedGpio = null;
}
}
private Runnable mBlinkRunnable = new Runnable() {
@Override
public void run() {
// Exit Runnable if the GPIO is already closed
if (mLedGpio == null) {
return;
}
try {
// Toggle the GPIO state
mLedState = !mLedState;
mLedGpio.setValue(mLedState);
Log.d(TAG, "State set to " + mLedState);
// Reschedule the same runnable in {#INTERVAL_BETWEEN_BLINKS_MS} milliseconds
mHandler.postDelayed(mBlinkRunnable, INTERVAL_BETWEEN_BLINKS_MS);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
};
}
该例子是google官方的例子,这里给出源码地址https://github.com/androidthings/sample-simplepio.git
如果文章对你有帮助,给我点个赞吧!~.~