OrangePi通过sysfs控制GPIO接口

什么是sysfs

Sysfs 是 Linux 2.6 所提供的一种虚拟文件系统。这个文件系统不仅可以把设备
(devices)和驱动程序(drivers) 的信息从内核输出到 用户空间,也可以用来对设备和驱
动程序做设置。
sysfs 的目的是把一些原本在 procfs 中的,关于设备的部份,独立出来,以‘设备层次结构
架构’(device tree)的形式呈现。这个文件系统由 Patrick Mochel 所写,稍后 
Maneesh Soni 撰写 "sysfs backing store path",以降低在大型系统中对存储器的需
求量。

使用的硬件

这里使用的是Orange Pi One,有40Pin的扩展接口,类似树莓派,使用方法也类似。
系统是使用的安卓4.4
导出Gpio接口:
echo XX > /sys/class/gpio/export(其中XX为你要导出的GPIO引脚编号,后面会说到引脚编号计算方法)
如果成功的话,这一步会在/sys/class/gpio目录下生成 /sys/class/gpio/gpioXX
设定IO方向:
echo "out" > /sys/class/gpio/gpioXX/direction(输出)
echo "in" > /sys/class/gpio/gpioXX/direction(输入)

设定输出值:
echo 1 > /sys/class/gpio/gpioXX/value
echo 0 > /sys/class/gpio/gpioXX/value

或者:
echo "high" > /sys/class/gpio/gpioXX/direction(输出,同时置高)
echo "low" > /sys/class/gpio/gpioXX/direction(输出,同时置低)

取消导出:
echo XX > /sys/class/gpio/unexport

引脚映射计算

Orange Pi One 外设的GPIO如下:
20151019002607_51900
计算公式:

(P后面的字母在字母表中的位置 - 1) * 32 + 后面的数字
例如:
PA0计算得到:0    ------>(1-1)*32+0
PA1计算得到:1       ------->(1-1)*32+1
PA15计算得到:15   ------->(1-1)*32+15
PD14计算得到:110  ------->(4-1)*32+14
PC1计算得到:65   ------->(3-1)*32+1

在Java层控制

package com.donute.robot.entities;

import com.donute.robot.utils.ShellUtils;

/**
 * Created by zhouyufei on 2017/1/4.
 */

public class Gpio {
    public static final int HIGH=1;
    public static final int LOW=0;
    public static final String IN="in";
    public static final String OUT="out";
    public static final String DISABLE="disable";
    private String path;
    private String direction=DISABLE;
    private int number;

    public Gpio(int number) {
        this.number = number;
        ShellUtils.execCommand("echo "+number+" > /sys/class/gpio/export",true);
        path="/sys/class/gpio/gpio"+number;
    }
    public Gpio openAsOut(){
        direction=OUT;
        ShellUtils.execCommand("echo "+direction+" > "+path+"/direction",true);
        return this;
    }
    public Gpio openAsIn(){
        direction=IN;
        ShellUtils.execCommand("echo "+direction+" > "+path+"/direction",true);
        return this;
    }
    public Gpio close(){
        direction=DISABLE;
        ShellUtils.execCommand("echo "+number+" > /sys/class/gpio/unexport",true);
        return this;
    }
    public void write(int value){
        ShellUtils.execCommand("echo "+value+" > "+path+"/value",true);
    }
    public int read(){
        ShellUtils.CommandResult result=ShellUtils.execCommand("cat "+path+"/value",true,true);
        if (result.result==0){
            try{
                return Integer.parseInt(result.successMsg);
            }catch (Exception e){
                return -1;
            }
        }else {
            return -1;
        }
    }

    public String getDirection() {
        return direction;
    }

    public int getNumber() {
        return number;
    }

}

 

ShellUtil代码

package com.donute.robot.utils;

/**
 * Created by zhouyufei on 16/7/31.
 */
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * ShellUtils
 * <ul>
 * <strong>Check root</strong>
 * <li>{@link ShellUtils#checkRootPermission()}</li>
 * </ul>
 * <ul>
 * <strong>Execte command</strong>
 * <li>{@link ShellUtils#execCommand(String, boolean)}</li>
 * <li>{@link ShellUtils#execCommand(String, boolean, boolean)}</li>
 * <li>{@link ShellUtils#execCommand(List, boolean)}</li>
 * <li>{@link ShellUtils#execCommand(List, boolean, boolean)}</li>
 * <li>{@link ShellUtils#execCommand(String[], boolean)}</li>
 * <li>{@link ShellUtils#execCommand(String[], boolean, boolean)}</li>
 * </ul>
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
 */
public class ShellUtils {

    public static final String COMMAND_SU       = "su";
    public static final String COMMAND_SH       = "sh";
    public static final String COMMAND_EXIT     = "exit\n";
    public static final String COMMAND_LINE_END = "\n";

    private ShellUtils() {
        throw new AssertionError();
    }

    /**
     * check whether has root permission
     *
     * @return
     */
    public static boolean checkRootPermission() {
        return execCommand("echo root", true, false).result == 0;
    }

    /**
     * execute shell command, default return result msg
     *
     * @param command command
     * @param isRoot whether need to run with root
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(String command, boolean isRoot) {
        return execCommand(new String[] {command}, isRoot, true);
    }

    /**
     * execute shell commands, default return result msg
     *
     * @param commands command list
     * @param isRoot whether need to run with root
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(List<String> commands, boolean isRoot) {
        return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, true);
    }

    /**
     * execute shell commands, default return result msg
     *
     * @param commands command array
     * @param isRoot whether need to run with root
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(String[] commands, boolean isRoot) {
        return execCommand(commands, isRoot, true);
    }

    /**
     * execute shell command
     *
     * @param command command
     * @param isRoot whether need to run with root
     * @param isNeedResultMsg whether need result msg
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
        return execCommand(new String[] {command}, isRoot, isNeedResultMsg);
    }

    /**
     * execute shell commands
     *
     * @param commands command list
     * @param isRoot whether need to run with root
     * @param isNeedResultMsg whether need result msg
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
        return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, isNeedResultMsg);
    }

    /**
     * execute shell commands
     *
     * @param commands command array
     * @param isRoot whether need to run with root
     * @param isNeedResultMsg whether need result msg
     * @return <ul>
     *         <li>if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and
     *         {@link CommandResult#errorMsg} is null.</li>
     *         <li>if {@link CommandResult#result} is -1, there maybe some excepiton.</li>
     *         </ul>
     */
    public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
        int result = -1;
        if (commands == null || commands.length == 0) {
            return new CommandResult(result, null, null);
        }

        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;

        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
            os = new DataOutputStream(process.getOutputStream());
            for (String command : commands) {
                if (command == null) {
                    continue;
                }

                // donnot use os.writeBytes(commmand), avoid chinese charset error
                os.write(command.getBytes());
                os.writeBytes(COMMAND_LINE_END);
                os.flush();
            }
            os.writeBytes(COMMAND_EXIT);
            os.flush();

            result = process.waitFor();
            // get command result
            if (isNeedResultMsg) {
                successMsg = new StringBuilder();
                errorMsg = new StringBuilder();
                successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
                errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                String s;
                while ((s = successResult.readLine()) != null) {
                    successMsg.append(s);
                }
                while ((s = errorResult.readLine()) != null) {
                    errorMsg.append(s);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (process != null) {
                process.destroy();
            }
        }
        return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
                : errorMsg.toString());
    }

    /**
     * result of command
     * <ul>
     * <li>{@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in
     * linux shell</li>
     * <li>{@link CommandResult#successMsg} means success message of command result</li>
     * <li>{@link CommandResult#errorMsg} means error message of command result</li>
     * </ul>
     *
     * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
     */
    public static class CommandResult {

        /** result of command **/
        public int    result;
        /** success message of command result **/
        public String successMsg;
        /** error message of command result **/
        public String errorMsg;

        public CommandResult(int result) {
            this.result = result;
        }

        public CommandResult(int result, String successMsg, String errorMsg) {
            this.result = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }
    }
}