在运行Android的DragonBoard 410C上访问GPIO低功耗连接器 [英] accessing GPIO low power connector on DragonBoard 410C running Android

查看:91
本文介绍了在运行Android的DragonBoard 410C上访问GPIO低功耗连接器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与最近购买的运行Android 5.1操作系统的DragonBoard 410C一起使用,并将Android Studio与Kotlin结合使用,以生成用于探索某些硬件(例如40针低功率连接器)的示例应用程序.

I'm working with a recently purchased DragonBoard 410C running the Android 5.1 operating system and using Android Studio with Kotlin for generating a sample application that explores some of the hardware such as the 40 pin low power connector.

我的问题是如何使用Kotlin和Android Studio通过GPIO引脚访问40引脚低功率连接器.

My question is how to access the 40 pin low power connector with the GPIO pins using Kotlin and Android Studio.

到目前为止,从我的研究来看,mraa库似乎是通往成功的道路,但是我无法找到有关在Kotlin上使用该库的任何文档.

From my research thus far, it appears that the mraa library is the path to success however I have been unable to find any documentation on using the library with Kotlin.

我如何开始使用Kotlin的mraa库访问40针低功率连接器?

How do I get started with the mraa library with Kotlin to access the 40 pin low power connector?

还是有其他方法?

我的第一个示例是LED应用程序的简单闪烁,但是我不知道如何使用Kotlin访问低功率连接器的引脚.

My first example is a simple blink the LED application however I have no idea as to how to access pins of the low power connector using Kotlin.

注释和资源

mraa文档页面

Libmraa是一个C/C ++库,具有对Python,Javascript和 Java与Galileo,Edison& amp;的I/O接口其他平台, 使用结构合理的API,其中端口名称/编号与 登上您所在的位置.使用libmraa不会使您局限于特定对象 在运行时完成电路板检测的硬件,您可以创建便携式 可以在支持的平台上运行的代码.

Libmraa is a C/C++ library with bindings to Python, Javascript and Java to interface with the I/O on Galileo, Edison & other platforms, with a structured and sane API where port names/numbering matches the board that you are on. Use of libmraa does not tie you to specific hardware with board detection done at runtime you can create portable code that will work across the supported platforms.

用于mraa GitHub存储库的upm库

UPM存储库提供了适用于各种 常用的传感器和执行器.这些软件驱动程序进行交互 与底层的硬件平台(或微控制器),以及 通过对MRAA API的调用来连接附加的传感器.

The UPM repository provides software drivers for a wide variety of commonly used sensors and actuators. These software drivers interact with the underlying hardware platform (or microcontroller), as well as with the attached sensors, through calls to MRAA APIs.

哪个Android运行哪个Linux内核? https://android.stackexchange.com/questions/51651/which-android-运行哪个linux内核

Which Android runs which Linux kernel? https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel

Android Version    |API Level  |Linux Version in AOSP                    |Header Version
----------------------------------------------------------------------------------------
4.4   Kit Kat      |19, 20     |(3.10)                                   |2.6.18
5.x   Lollipop     |21, 22     |(3.16.1)                                 |3.14.0
6.0   Marshmallow  |23         |(3.18.10)                                |3.18.10

这基本上是代码,它将取代我们的Python DHT驱动程序,并且 具有与任何其他Linux板向前兼容的优势 运行4.8+内核.我们将逐步替换其他CircuitPython 使用libgpiod的代码,以便我们可以广泛支持 在Raspberry Pi,BeagleBone或Onion.io上使用CircuitPython.

This is basically code that will replace our Python DHT driver, and has the benefit of being forward compatible with any other Linux board that runs a 4.8+ kernel. We’ll slowly be replacing other CircuitPython code to use libgpiod, so that we can have broad support for CircuitPython on a Raspberry Pi, BeagleBone or Onion.io.

那里没有很多libgpiod代码,而libgpiod没有 发行了Linux发行版的股票,这可能就是为什么它要花点时间 一会儿赶上.有C和Python的绑定.这是一个脚本 通过为您编译它可以帮助您入门 https://github.com/adafruit/Raspberry- Pi-Installer-Scripts/blob/master/libgpiod.sh

There’s not a lot of libgpiod code out there, and libgpiod doesn’t come stock on Linux distros yet which may be why its taking a little while to catch on. There’s bindings for C and Python. Here’s a script that can help you get started by compiling it for you https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh

GitHub上的DragonBoard 410C GPIO库 https://github.com/IOT-410c/DragonBoard410c_GpioLibrary 用Java编写,并使用"/sys/class/gpio" Linux访问GPIO引脚的方法.看起来这是一组用于物联网的Coursera课程的存储库中的存储库,其中一些使用DragonBoard 410C.

A DragonBoard 410C GPIO library on GitHub https://github.com/IOT-410c/DragonBoard410c_GpioLibrary which is written in Java and uses the "/sys/class/gpio" method of Linux to access GPIO pins. This looks like this is a repository in a set of repositories used for Coursera courses on Internet of things, some of which use the DragonBoard 410C.

Linux内核:旧版GPIO接口

这概述了Linux上的GPIO访问约定.

This provides an overview of GPIO access conventions on Linux.

这些调用使用gpio_ *命名前缀.没有其他电话可以使用 该前缀,或相关的_ gpio *前缀.

These calls use the gpio_* naming prefix. No other calls should use that prefix, or the related _gpio* prefix.

Android Studio和adb

Android Studio是用于开发Android应用程序的应用程序.可以从 https://developer.android.com/studio/releases 下载进行安装.

Android Studio is the application for developing Android apps. It can be downloaded for installation from https://developer.android.com/studio/releases

此外,还有单独下载的Android平台工具. adb shell应用程序是这些工具的一部分.可以从 SDK平台工具发行说明中下载该工具进行安装.选择您的操作系统(Windows,Linux,MacOS)所需的特定版本.

In addition there are the Android Platform Tools which are a separate download. The adb shell application is part of these tools. The tools can be downloaded for installation from SDK Platform Tools release notes. Pick the particular version needed for your operating system (Windows, Linux, MacOS).

Android Things

尽管Android Things看起来很有帮助,但实际上似乎仅支持两块板,而DragonBoard 410C却不是其中之一.而且我不确定Android Things是否仍能与Android 5.1配合使用.

While Android Things looks helpful, it appears only a couple of boards are actually supported and the DragonBoard 410C is not one of those. And I'm not sure that Android Things would work with Android 5.1 anyway.

https://developer.android.com/things/get-started

不过,DragonBoard 410C有一个Brillo(现在为Android Things)端口

However there was a Brillo (now Android Things) port for DragonBoard 410C https://discuss.96boards.org/t/android-things-on-the-dragonboard/1128

Android开发人员>文件> Android Things>指南> GPIO

要打开与GPIO端口的连接,您需要了解 唯一的端口名称.在开发的初始阶段,或何时 将应用程序移植到新硬件上,发现所有 PeripheralManager使用getGpioList()的可用端口名称:

In order to open a connection to a GPIO port, you need to know the unique port name. During the initial stages of development, or when porting an app to new hardware, it's helpful to discover all the available port names from PeripheralManager using getGpioList():

Android Things GitHub存储库 https://github.com/androidthings/

Android Things GitHub repositories https://github.com/androidthings/

另请参阅以下关于该主题的stackoverflow帖子.另请参见stackoverflow中的[android-things]标签.

See also the following stackoverflow posts which have something to say on this subject. See tag [android-things] in stackoverflow as well.

GpioCallback如何注册"false"?连续两次?

Android Things Pin命名约定

PeripheralManagerService抛出NoClassDefFoundError

推荐答案

查看了许多替代方案之后,似乎访问运行Android 5.1的DragonBoard 410C GPIO引脚的最简单方法是使用旧版sysfs专用设备文件方法.

After reviewing a number of alternatives, it appears that the easiest approach for accessing the GPIO pins of the DragonBoard 410C running Android 5.1 was to use the legacy sysfs special device files method.

我不确定这是否是唯一可行的解​​决方案.与Android 5.1相比,使用Android Things和libgpiod似乎都需要更新的Linux内核.

I'm not sure if this is the only workable solution. Using Android Things as well as using libgpiod both seem to require a more recent Linux kernel than Android 5.1 uses.

我在CodeProject.com上写了一篇文章,提供了有关解决此解决方案的详细信息.请参见将Windows 10与DragonBoard 410C和更高版本一起使用进行开发Android .

I have written an article on CodeProject.com providing details about working up this solution. See Using Windows 10 for Development with DragonBoard 410C and Android.

哪个Android运行哪个Linux内核? https://android.stackexchange.com/questions/51651/which-android-运行哪个linux内核

Which Android runs which Linux kernel? https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel

Android Version    |API Level  |Linux Version in AOSP                    |Header Version
----------------------------------------------------------------------------------------
4.4   Kit Kat      |19, 20     |(3.10)                                   |2.6.18
5.x   Lollipop     |21, 22     |(3.16.1)                                 |3.14.0
6.0   Marshmallow  |23         |(3.18.10)                                |3.18.10

该方法似乎也是最简单的方法,因为所使用的库也是用Kotlin编写的.

This method also seems to be the easiest in that the library used is written in Kotlin as well.

使用旧版sysfs专用设备GPIO接口

有关Linux伪文件和特殊设备文件以及GPIO引脚的旧版sysfs接口,请参见此StackOverFlow帖子,

See this StackOverFlow post about Linux pseudo files and special device files and the legacy sysfs interface for GPIO pins, what is the /sys/class/gpio/export and `/sys/class/gpio/unexport mechanism and what is the underlying sysfs functionality? .

我找到了一个简单的用Java编写的GPIO库,它提供了必要的源代码. Android Studio有一个将Java转换为Kotlin的工具,我将其包含在项目中.源代码在下面的文件Gpio.kt和GpioProcessor.kt中.

I found a simple GPIO library written in Java that provided necessary source code. Android Studio has a tool that converted the Java to Kotlin which I included into my project. The source code is below in files Gpio.kt and GpioProcessor.kt.

但是,要使其正常工作,我必须对DragonBoard启动脚本进行更改,以确保创建了必要的特殊设备文件并以适当的权限提供它们,从而允许用户程序操作GPIO引脚.

However in order for this to work I had to make a change to my DragonBoard startup scripts in order to make sure that the necessary special device files were created and available with the proper permissions allowing a user program to manipulate the GPIO pins.

以下过程来自 Coursera类物联网:传感和从设备驱动,第5a课:通过程序(Android)视频#2,修改启动脚本访问GPIO.程序是:

The following procedure is from the Coursera class Internet of Things: Sensing and Actuation from Devices, Lesson 5a: Access GPIO through programs (Android) video #2, Modify boot script. The procedure was to:

  • 使用adb将/etc/init.qcom.post_boot.sh 的副本从DragonBoard中拉到我的PC中
  • 使用记事本修改Shell脚本以创建特殊设备文件
  • 使用adb将修改后的副本推回到Dragonboard
  • 使用adb重启DragonBoard
  • use adb to pull a copy of /etc/init.qcom.post_boot.sh from the DragonBoard to my PC
  • use Notepad to modify the shell script to create the special device files
  • use adb to push the modified copy back to the Dragonboard
  • use adb to reboot the DragonBoard

要添加到/etc/init.qcom.post_boot.sh底部的其他Shell代码如下. 但是这些特殊设备文件仅适用于Android 5.1. Linux使用不同的GPIO引脚名称.

The additional shell code to add to the bottom of /etc/init.qcom.post_boot.sh is as follows. However these special device files are for Android 5.1 only. Linux uses different GPIO pin names.

set -A pins 938 915 1017 926 937 930 914 971 901 936 935
for i in 0 1 2 3 4 5 6 7 8 9 10
do
    echo ${pins[i]} > /sys/class/gpio/export;
    chmod 777 /sys/class/gpio/gpio${pins[i]};
    chmod 777 /sys/class/gpio/gpio${pins[i]}/value;
    chmod 777 /sys/class/gpio/gpio${pins[i]}/direction;
done

有关sysfs设备属性的说明

以下是 GPIO Sysfs接口的一些文档来自kernel.org的用户空间.除了我使用的两个属性directionvalue,还有其他一些属性,例如edgeactive_low.

Here is some documentation on the GPIO Sysfs Inferface for Userspace from kernel.org. In addition to the two attributes that I use, direction and value, there are several others such as edge and active_low.

方向" …读为入"或出".该值通常可以 被写.默认写为"out",将值初始化为 低的.为了确保无故障运行,可以将值低"和高"设置为 编写以将GPIO配置为具有该初始值的输出.

"direction" … reads as either "in" or "out". This value may normally be written. Writing as "out" defaults to initializing the value as low. To ensure glitch free operation, values "low" and "high" may be written to configure the GPIO as an output with that initial value.

请注意,如果内核不支持此属性,则该属性将不存在 更改GPIO的方向,或者它已由内核代码导出 并没有明确允许用户空间重新配置此GPIO 方向.

Note that this attribute will not exist if the kernel doesn’t support changing the direction of a GPIO, or it was exported by kernel code that didn’t explicitly allow userspace to reconfigure this GPIO’s direction.

值" …读为0(低)或1(高).如果GPIO是 配置为输出,可以写入该值;任何非零值 被视为高.

"value" … reads as either 0 (low) or 1 (high). If the GPIO is configured as an output, this value may be written; any nonzero value is treated as high.

该引脚是否可以配置为产生中断的中断,以及 它已配置为生成中断(请参见 "edge"),则可以对该文件进行poll(2),并且poll(2)会在任何时候返回 中断被触发.如果使用poll(2),请设置事件 POLLPRI和POLLERR.如果使用select(2),请在以下位置设置文件描述符 除了fds.在poll(2)返回之后,或者lseek(2)到以下地址的开头 sysfs文件并读取新值或关闭文件然后重新打开 读取值.

If the pin can be configured as interrupt-generating interrupt and if it has been configured to generate interrupts (see the description of "edge"), you can poll(2) on that file and poll(2) will return whenever the interrupt was triggered. If you use poll(2), set the events POLLPRI and POLLERR. If you use select(2), set the file descriptor in exceptfds. After poll(2) returns, either lseek(2) to the beginning of the sysfs file and read the new value or close the file and re-open it to read the value.

边缘" ……读作无",上升",下降"或两者".写 这些字符串选择将使poll(2)亮起的信号边沿 值"文件返回.

"edge" … reads as either "none", "rising", "falling", or "both". Write these strings to select the signal edge(s) that will make poll(2) on the "value" file return.

仅当该引脚可以配置为中断时,此文件才存在 生成输入引脚.

This file exists only if the pin can be configured as an interrupt generating input pin.

"active_low" …读为0(假)或1(真).写任何 非零值,可同时反转value属性以进行读取和 写作.现有和后续的poll(2)支持通过 边缘属性的上升"和下降"边缘将遵循此 设置.

"active_low" … reads as either 0 (false) or 1 (true). Write any nonzero value to invert the value attribute both for reading and writing. Existing and subsequent poll(2) support configuration via the edge attribute for "rising" and "falling" edges will follow this setting.

使用sysfs的Kotlin源代码

我用来探讨将DragonBoard 410C与Android结合使用的完整测试应用程序位于我的GitHub存储库中, https://github.com/RichardChambers/dragonboard_410c

The complete testing application I am using to explore this topic of using the DragonBoard 410C with Android is in my GitHub repository, https://github.com/RichardChambers/dragonboard_410c

文件Gpio.kt的来源

package com.example.myapplication

import java.io.*

/**
 * Created by Ara on 7/21/15.
 * From https://www.instructables.com/id/DragonBoard-How-to-Access-GPIOs-Using-Java/
 *   Java source from the article was converted to Kotlin using Android Studio.
 *
 * See as well https://github.com/IOT-410c/DragonBoard410c_GpioLibrary
 *
 */
class Gpio(pin: Int) {
    private val pin: Int

    /*
     *  The GPIO pins are represented by folders in the Linux file system
     *  within the folder /sys/class/gpio. Each pin is represented by a folder
     *  whose name is the prefix "gpio" followed by the pin number.
     *  Within the folder representing the pin are two files, "value" used to
     *  set or get the value of the pin and "direction" used to set or get
     *  the direction of the pin.
     *
     *  This function creates the path to the Linux file which represents a particular
     *  GPIO pin function, "value" or "direction".
     */
    private fun MakeFileName(pin: Int, op: String): String {
        return "/sys/class/gpio/gpio$pin$op"
    }

    /*
     * Get or set the current direction of a pin.
     * A pin may be either an Input pin or an Output pin.
     */
    var direction: String
        get() {
            println("Getting Direction")
            var line = ""
            try {
                val br = BufferedReader(FileReader(MakeFileName(pin, "/direction")))
                line = br.readLine()
                br.close()
            } catch (e: Exception) {
                println("Error: " + e.message)
            }
            return line
        }
        private set(direction) {
            println("Setting Direction")
            try {
                val out = BufferedWriter(FileWriter(MakeFileName(pin, "/direction"), false))
                out.write(direction)
                out.close()
            } catch (e: IOException) {
                println("Error: " + e.message)
            }
        }

    /**
     * Get or Set pin value.
     * @param value Value of pin.
     * 0 -> Low Level.
     * 1 -> High Level
     */
    var value: Int
        get() {
            println("Getting Value")
            var line = ""
            try {
                val br = BufferedReader(FileReader(MakeFileName(pin, "/value")))
                line = br.readLine()
                br.close()
            } catch (e: Exception) {
                println("Error: " + e.message)
            }
            return line.toInt()
        }
        private set(value) {
            println("Setting Value")
            try {
                val out = BufferedWriter(FileWriter(MakeFileName(pin, "/value"), false))
                out.write(Integer.toString(value))
                out.close()
            } catch (e: IOException) {
                println("Error: " + e.message)
            }
        }

    /**
     * Set pin as high.
     */
    fun pinHigh() {
        value = HIGH
    }

    /**
     * Set pin as low.
     */
    fun pinLow() {
        value = LOW
    }

    /**
     * Set pin as output.
     */
    fun pinOut() {
        direction = "out"
    }

    /**
     * Set pin as input.
     * @param pin - Desirable pin.
     */
    fun pinIn() {
        direction = "in"
    }

    fun exportPin() {
        println("Exporting Ping")
        try {
            val out = BufferedWriter(FileWriter("$PATH/export", false))
            out.write(pin.toString())
            out.close()
        } catch (e: IOException) {
            println("Error: " + e.message)
        }
    }

    /**
     * Disable access to GPIO.
     * @param pin GPIO pin to disable access.
     */
    fun unexportPin() {
        println("unExporting Ping")
        try {
            val out = BufferedWriter(FileWriter("$PATH/unexport", false))
            out.write(pin.toString())
            out.close()
        } catch (e: IOException) {
            println("Error: " + e.message)
        }
    }

    companion object {
        const val HIGH = 1
        const val LOW = 0
        private const val PATH = "/sys/class/gpio"
    }

    /**
     * Set desirable pin for the GPIO class.
     */
    init {
        println("Initializing pin $pin")
        this.pin = pin
    }
}

GpioProcessor.kt的来源

package com.example.myapplication

import java.io.BufferedWriter
import java.io.FileWriter
import java.io.IOException
import java.util.*

/**
 * Created by Ara on 7/21/15.
 * From https://www.instructables.com/id/DragonBoard-How-to-Access-GPIOs-Using-Java/
 *   Java source from the article was converted to Kotlin using Android Studio.
 *
 * See as well https://github.com/IOT-410c/DragonBoard410c_GpioLibrary
 *
 * Simple example main()
 *
 * public class Main {
 *
 * public static void main(String[] args) {
 * int count = 0;
 * int buttonValue = 0;
 *
 * GpioProcessor gpioProcessor = new GpioProcessor();
 *
 * // Get reference of GPIO27 and GPIO29.
 *
 * Gpio gpioPin27 = gpioProcessor.getPin27();
 * Gpio gpioPin29 = gpioProcessor.getPin29();
 *
 * // Set GPIO27 as output.Set GPIO29 as input.
 * gpioPin27.pinOut();
 * gpioPin29.pinIn();
 *
 * while(count<20){
 * count++;
 * // Read value of GPIO29.
 * buttonValue=gpioPin29.getValue();
 *
 * if(buttonValue == 0){
 * // Set GPIO27 as low level.
 * gpioPin27.pinLow();
 * } else{
 * // Set GPIO27 as high level.
 * gpioPin27.pinHigh();
 * }
 *
 * try {
 * Thread.sleep(1000);
 * } catch(InterruptedException e){
 * // TODO Auto-generated catch block
 * e.printStackTrace();
 * }
 * }
 *
 * // Disable access GPIO27 and GPIO29.
 * gpioProcessor.closePins();
 * }
 * }
 */ /*
 This class abstracts the use of the gpio pins. This class can be utilized on any linux operating
 system that has gpio pins defined in the /sys/class/gpio directory. It is required that the gpio
 pins themselves are available for access by the user of this application, and may require a
 change of permissions.
 */
class GpioProcessor {
    private val PATH = "/sys/class/gpio"
    private val pins: MutableList<Int> = ArrayList()

    // mapping of physical pin number to GPIO file number.
    // the mapping varies depending on the operating system
    private val  androidPin23 = 938
    private val  androidPin24 = 914
    private val  androidPin25 = 915
    private val  androidPin26 = 971
    private val  androidPin27 = 1017
    private val  androidPin28 = 901   // GPIO pin borrowed from MPP. supports PWM. support analog I/O.
    private val  androidPin29 = 926   // (input only)
    private val  androidPin30 = 927
    private val  androidPin31 = 937
    private val  androidPin32 = 936
    private val  androidPin33 = 930
    private val  androidPin34 = 935

    private val  linuxPin23 = 36
    private val  linuxPin24 = 12
    private val  linuxPin25 = 13
    private val  linuxPin26 = 69
    private val  linuxPin27 = 115
    private val  linuxPin28 = 4     // GPIO pin borrowed from MPP. supports PWM. support analog I/O.
    private val  linuxPin29 = 24    // (input only)
    private val  linuxPin30 = 25
    private val  linuxPin31 = 35
    private val  linuxPin32 = 34
    private val  linuxPin33 = 28
    private val  linuxPin34 = 33

    private val  physicalPin23 = androidPin23
    private val  physicalPin24 = androidPin24
    private val  physicalPin25 = androidPin25
    private val  physicalPin26 = androidPin26
    private val  physicalPin27 = androidPin27
    private val  physicalPin28 = androidPin28    // GPIO pin borrowed from MPP. supports PWM. support analog I/O.
    private val  physicalPin29 = androidPin29    // (input only)
    private val  physicalPin30 = androidPin30
    private val  physicalPin31 = androidPin31
    private val  physicalPin32 = androidPin32
    private val  physicalPin33 = androidPin33
    private val  physicalPin34 = androidPin34

    /**
     * Get function of specific pin.
     * @param pin Desirable pin.
     */
    fun getPin(pin: Int): Gpio {
        exportPin(pin)
        pins.add(pin)
        return Gpio(pin)
    }

    /**
     * Get pin 23;
     * @returns {Gpio}
     */
    val pin23: Gpio
        get() = getPin(physicalPin23)

    /**
     * Get pin 24.
     * @returns {Gpio}
     */
    val pin24: Gpio
        get() = getPin(physicalPin24)

    /**
     * Get pin 25.
     * @returns {Gpio}
     */
    val pin25: Gpio
        get() = getPin(physicalPin25)

    /**
     * Get pin 26.
     * @returns {Gpio}
     */
    val pin26: Gpio
        get() = getPin(physicalPin26)

    /**
     * Get pin 27.
     * @returns {Gpio}
     */
    val pin27: Gpio
        get() = getPin(physicalPin27)

    /**
     * Get pin 28.
     * @returns {Gpio}
     */
    val pin28: Gpio
        get() = getPin(physicalPin28)

    /**
     * Get pin 29.
     * @returns {Gpio}
     */
    val pin29: Gpio
        get() = getPin(physicalPin29)

    /**
     * Get pin 30.
     * @returns {Gpio}
     */
    val pin30: Gpio
        get() = getPin(physicalPin30)

    /**
     * Get pin 31.
     * @returns {Gpio}
     */
    val pin31: Gpio
        get() = getPin(physicalPin31)

    /**
     * Get pin 32.
     * @returns {Gpio}
     */
    val pin32: Gpio
        get() = getPin(physicalPin32)

    /**
     * Get pin 33.
     * @returns {Gpio}
     */
    val pin33: Gpio
        get() = getPin(physicalPin33)

    /**
     * Get pin 34.
     * @returns {Gpio}
     */
    val pin34: Gpio
        get() = getPin(physicalPin34)

    /**
     * Get all GPIO's pins.
     * @return List of pins.
     */
    val allPins: Array<Gpio?>
        get() {
            val allPins = arrayOfNulls<Gpio>(12)   // android       linux
            allPins[0] = pin23                          // GPIO 938     GPIO 36
            allPins[1] = pin24                          // GPIO 914     GPIO 12
            allPins[2] = pin25                          // GPIO 915     GPIO 13
            allPins[3] = pin26                          // GPIO 971     GPIO 69
            allPins[4] = pin27                          // GPIO 1017    GPIO 115
            allPins[5] = pin28                          // Reserved
            allPins[6] = pin29                          // GPIO 926     GPIO 24 (input only)
            allPins[7] = pin30                          // GPIO 927     GPIO 25
            allPins[8] = pin31                          // GPIO 937     GPIO 35
            allPins[9] = pin32                          // GPIO 936     GPIO 34
            allPins[10] = pin33                         // GPIO 930     GPIO 28
            allPins[11] = pin34                         // GPIO 935     GPIO 33
            return allPins
        }

    /**
     * Enable access to GPIO.
     * @param pin GPIO pin to access.
     */
    private fun exportPin(pin: Int) {
        println("Exporting Ping")
        try {
            val out = BufferedWriter(FileWriter("$PATH/export", false))
            out.write(pin.toString())
            out.close()
        } catch (e: IOException) {
            println("Error: " + e.message)
        }
    }

    /**
     * Disable access to GPIO.
     * @param pin GPIO pin to disable access.
     */
    private fun unexportPin(pin: Int) {
        println("unExporting Ping")
        try {
            val out = BufferedWriter(FileWriter("$PATH/unexport", false))
            out.write(pin.toString())
            out.close()
        } catch (e: IOException) {
            println("Error: " + e.message)
        }
    }

    fun closePins() {
        for (pin in pins) {
            unexportPin(pin)
        }
        pins.clear()
    }

    companion object {
        const val TAG = "GpioProcessor"
    }
}

使用GpioProcessor类的示例源

我通过将按钮按下链接到侦听器,在一个片段中的一个Android应用程序中使用了GPIO sysfs接口库.我有两个按钮,一个按钮通过将引脚驱动为高电平来打开LED,第二个按钮通过将引脚驱动为低电平来关闭LED.

I used the GPIO sysfs interface library in an Android app within a fragment by linking a button press to a listener. I have two buttons, one to turn the LED on by driving a pin high and a second to turn the LED off by driving a pin low.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    view.findViewById<Button>(R.id.button_second).setOnClickListener {
        findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
    }

    val txtScroll = view.findViewById(R.id.LedStatus) as TextView

    // find the button whose id is button_Location and then set an listener for
    // any clicks on that button. In the following listener we are going to have
    // the "Location" button, defined in the file fragment_first.xml, generate a
    // list of the GPS service providers by creatinga LocationManager object to
    // generate a list.
        val gpioProcessor_x =  GpioProcessor()
        // Get reference of GPIO23.
        val gpioPin23_x = gpioProcessor_x.pin23
        gpioPin23_x.exportPin()

    view.findViewById<Button>(R.id.button_led_off).setOnClickListener {
        val gpioProcessor =  GpioProcessor()
        // Get reference of GPIO27.
        val gpioPin23 = gpioProcessor.pin23

        // Set GPIO23 as output.
        gpioPin23.pinOut()
        gpioPin23.pinLow()    // drive pin low to turn off LED.
        txtScroll.append("LED Off\n")
    }

    view.findViewById<Button>(R.id.button_led_on).setOnClickListener {
        val gpioProcessor =  GpioProcessor()
        // Get reference of GPIO27.
        val gpioPin23 = gpioProcessor.pin23

        // Set GPIO23 as output.
        gpioPin23.pinOut()
        gpioPin23.pinHigh()    // drive pin high to turn on LED
        txtScroll.append("LED On\n")
    }
}

这篇关于在运行Android的DragonBoard 410C上访问GPIO低功耗连接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆