OrangePi通過sysfs控制GPIO接口
什么是sysfs
Sysfs 是 Linux 2.6 所提供的一種虛擬文件系統(tǒng)翘骂。這個(gè)文件系統(tǒng)不僅可以把設(shè)備
(devices)和驅(qū)動程序(drivers) 的信息從內(nèi)核輸出到 用戶空間再膳,也可以用來對設(shè)備和驅(qū)
動程序做設(shè)置。
sysfs 的目的是把一些原本在 procfs 中的吧碾,關(guān)于設(shè)備的部份俺孙,獨(dú)立出來辣卒,以‘設(shè)備層次結(jié)構(gòu)
架構(gòu)’(device tree)的形式呈現(xiàn)。這個(gè)文件系統(tǒng)由 Patrick Mochel 所寫睛榄,稍后
Maneesh Soni 撰寫 "sysfs backing store path"荣茫,以降低在大型系統(tǒng)中對存儲器的需求量.
### 使用的硬件
這里使用的是Orange Pi One,有40Pin的擴(kuò)展接口场靴,類似樹莓派啡莉,使用方法也類似。
系統(tǒng)是使用的安卓4.4
導(dǎo)出Gpio接口:
echo XX > /sys/class/gpio/export(其中XX為你要導(dǎo)出的GPIO引腳編號旨剥,后面會說到引腳編號計(jì)算方法)
如果成功的話咧欣,這一步會在/sys/class/gpio目錄下生成 /sys/class/gpio/gpioXX
設(shè)定IO方向:
echo out > /sys/class/gpio/gpioXX/direction(輸出)
echo in > /sys/class/gpio/gpioXX/direction(輸入)
設(shè)定輸出值:
echo 1 > /sys/class/gpio/gpioXX/value
echo 0 > /sys/class/gpio/gpioXX/value
或者:
echo high > /sys/class/gpio/gpioXX/direction(輸出,同時(shí)置高)
echo low > /sys/class/gpio/gpioXX/direction(輸出轨帜,同時(shí)置低)
取消導(dǎo)出:
echo XX > /sys/class/gpio/unexport
### 引腳映射計(jì)算
Orange Pi One 外設(shè)的GPIO如下:
![20151019002607_51900.jpg](http://upload-images.jianshu.io/upload_images/3088365-143e6e024df32a23.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
計(jì)算公式:
(P后面的字母在字母表中的位置 - 1) * 32 + 后面的數(shù)字
例如:
PA0計(jì)算得到:0 ------>(1-1)*32+0
PA1計(jì)算得到:1 ------->(1-1)*32+1
PA15計(jì)算得到:15 ------->(1-1)*32+15
PD14計(jì)算得到:110 ------->(4-1)*32+14
PC1計(jì)算得到:65 ------->(3-1)*32+1
### 在Java層控制
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 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 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;
}
}
}