Interact with Peripherals

与外设进行交互

这一部分内容将告诉您

  1. List Available Peripherals

  2. 列出可用外设

  3. Communicate with GPIO Ports

  4. 与 GPIO 接口进行通信

试试这个

The power of Android Things is realized when developers begin connecting hardware peripherals directly to apps.

当 Android 开发者开始将硬件外设直接连接到应用程序时,Android Things 的功能就实现了。

In this lesson, you will learn how to use the basic Peripheral I/O APIs to discover and communicate with General Purpose Input Ouput (GPIO) ports.

在这节课中,您将学习怎样利用基础的外设 I/O API 去发现通用输入输出接口(GPIO)设备,并与其通信。

List available peripherals

列出可用外设


The system service responsible for managing peripheral connections is PeripheralManagerService. You can use this service to list the available ports for all known peripheral types.

负责管理外设链接的系统服务是 PeripheralManagerService。您可以利用这个服务来列出目前处于可用状态的已知类型的外设。

The following code writes the list of available GPIO ports to logcat:

下面的代码将可用的 GPIO 接口写入到日志中:

  1. public class HomeActivity extends Activity {
  2. private static final String TAG = "HomeActivity";
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. PeripheralManagerService service = new PeripheralManagerService();
  7. Log.d(TAG, "Available GPIO: " + service.getGpioList());
  8. }
  9. }

Handle button events

处理按钮事件


To receive events when a button connected to GPIO is pressed:

为了接收到一个已经连接 GPIO 接口被按下时所触发的事件,您需要做以下工作:

  1. Use PeripheralManagerService to open a connection with the GPIO port wired to the button.

  2. 利用 PeripheralManagerService 开启一个 GPIO 端口与按键的连接。

  3. Configure the port with DIRECTION_IN.

  4. 配置端口为 DIRECTION_IN 模式。

  5. Configure which state transitions will generate callback events with setEdgeTriggerType().

  6. 利用 setEdgeTriggerType() 来配置哪些状态转换将触发回调事件。

  7. Register a GpioCallback to receive edge trigger events.

  8. 利用 GpioCallback 注册一个函数来接收边缘触发事件。

  9. Return true within onGpioEdge() to continue receiving future edge trigger events.

  10. onGpioEdge() 函数中返回 true 来确保能继续接收到后续的边缘触发事件。

  11. When the application no longer needs the GPIO connection, close the Gpio resource.

  12. 当应用程序不再需要 GPIO 连接时,关闭 GPIO 资源。

  1. public class ButtonActivity extends Activity {
  2. private static final String TAG = "ButtonActivity";
  3. private static final String BUTTON_PIN_NAME = ...; // GPIO port wired to the button
  4. private Gpio mButtonGpio;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. PeripheralManagerService service = new PeripheralManagerService();
  9. try {
  10. // Step 1. Create GPIO connection.
  11. mButtonGpio = service.openGpio(BUTTON_PIN_NAME);
  12. // Step 2. Configure as an input.
  13. mButtonGpio.setDirection(Gpio.DIRECTION_IN);
  14. // Step 3. Enable edge trigger events.
  15. mButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
  16. // Step 4. Register an event callback.
  17. mButtonGpio.registerGpioCallback(mCallback);
  18. } catch (IOException e) {
  19. Log.e(TAG, "Error on PeripheralIO API", e);
  20. }
  21. }
  22. // Step 4. Register an event callback.
  23. private GpioCallback mCallback = new GpioCallback() {
  24. @Override
  25. public boolean onGpioEdge(Gpio gpio) {
  26. Log.i(TAG, "GPIO changed, button pressed");
  27. // Step 5. Return true to keep callback active.
  28. return true;
  29. }
  30. };
  31. @Override
  32. protected void onDestroy() {
  33. super.onDestroy();
  34. // Step 6. Close the resource
  35. if (mButtonGpio != null) {
  36. mButtonGpio.unregisterGpioCallback(mCallback);
  37. try {
  38. mButtonGpio.close();
  39. } catch (IOException e) {
  40. Log.e(TAG, "Error on PeripheralIO API", e);
  41. }
  42. }
  43. }
  44. }

点亮 LED

Lightening LED


To execute a blinking pattern on an LED connected to GPIO:

为了让一个已经连接到 GPIO 接口的 LED 进入闪烁模式,您需要:

  1. Use PeripheralManagerService to open a connection with the GPIO port wired to the LED.

  2. 利用 PeripheralManagerService 开启 GPIO 端口与 LED 的连接。

  3. Configure the port with DIRECTION_OUT_INITIALLY_LOW.

  4. 配置端口为 DIRECTION_OUT_INITIALLY_LOW 模式。

  5. Toggle the state of the LED by passing the inverse of getValue() to the setValue() method.

  6. 通过 getValue() 对应的 setValue() 函数来切换 LED 的状态。

  7. Use a Handler to schedule an event to toggle the GPIO again after a short delay.

  8. 利用一个 Handler 来设定一个在短暂的延迟之后再次切换 GPIO 状态的事件。

  9. When the application no longer needs the GPIO connection, close the Gpio resource.

  10. 当应用程序不再需要 GPIO 连接时,关闭 GPIO 资源。

  1. public class BlinkActivity extends Activity {
  2. private static final String TAG = "BlinkActivity";
  3. private static final int INTERVAL_BETWEEN_BLINKS_MS = 1000;
  4. private static final String LED_PIN_NAME = ...; // GPIO port wired to the LED
  5. private Handler mHandler = new Handler();
  6. private Gpio mLedGpio;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. // Step 1. Create GPIO connection.
  11. PeripheralManagerService service = new PeripheralManagerService();
  12. try {
  13. mLedGpio = service.openGpio(LED_PIN_NAME);
  14. // Step 2. Configure as an output.
  15. mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
  16. // Step 4. Repeat using a handler.
  17. mHandler.post(mBlinkRunnable);
  18. } catch (IOException e) {
  19. Log.e(TAG, "Error on PeripheralIO API", e);
  20. }
  21. }
  22. @Override
  23. protected void onDestroy() {
  24. super.onDestroy();
  25. // Step 4. Remove handler events on close.
  26. mHandler.removeCallbacks(mBlinkRunnable);
  27. // Step 5. Close the resource.
  28. if (mLedGpio != null) {
  29. try {
  30. mLedGpio.close();
  31. } catch (IOException e) {
  32. Log.e(TAG, "Error on PeripheralIO API", e);
  33. }
  34. }
  35. }
  36. private Runnable mBlinkRunnable = new Runnable() {
  37. @Override
  38. public void run() {
  39. // Exit if the GPIO is already closed
  40. if (mLedGpio == null) {
  41. return;
  42. }
  43. try {
  44. // Step 3. Toggle the LED state
  45. mLedGpio.setValue(!mLedGpio.getValue());
  46. // Step 4. Schedule another event after delay.
  47. mHandler.postDelayed(mBlinkRunnable, INTERVAL_BETWEEN_BLINKS_MS);
  48. } catch (IOException e) {
  49. Log.e(TAG, "Error on PeripheralIO API", e);
  50. }
  51. }
  52. };
  53. }