GPIO0对应系统节点/sys/class/backlight/rk28_bl/gpio0
GPIO1对应系统节点/sys/class/backlight/rk28_bl/gpio1
GPIO2对应系统节点/sys/class/backlight/rk28_bl/gpio2
GPIO3对应系统节点/sys/class/backlight/rk28_bl/gpio3
public static BufferedReader reader = null;
//读GPIO
private String getGpioString(String path) {
String defString = "0";// 默认值
try {
if (reader == null){
reader = new BufferedReader(new FileReader(path));
}
defString = reader.readLine();
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
return defString;
}
getGpioString("/sys/class/backlight/rk28_bl/gpio0") 返回字符串“0” 或者“1”
//将GPIO口设置为输出的时候,默认是输出,调用下面的方法即可变成默认输入
public boolean gpio_input0(View v) {
return RootCommand("echo 2 > /sys/class/backlight/rk28_bl/gpio0");
}
public boolean gpio_input1(View v) {
return RootCommand("echo 2 > /sys/class/backlight/rk28_bl/gpio1");
}
public boolean gpio_input2(View v) {
return RootCommand("echo 2 > /sys/class/backlight/rk28_bl/gpio2");
}
//当GPIO口为输出的时候,通过以下的办法来控制高低电平
private boolean set_gpio0_high() { //拉高
boolean FLAG = RootCommand("echo 1 > /sys/class/backlight/rk28_bl/gpio0");
return FLAG;
}
public boolean set_gpio0_low() { //拉低
boolean FLAG = RootCommand("echo 0 >/sys/class/backlight/rk28_bl/gpio0");
return FLAG;
}
#### 其他的GPIO口都是一样的方法(gpio0、gpio1、gpio2、gpio3)
//下面的是执行的方法
private boolean RootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
Android GPIO工具类
最新推荐文章于 2024-07-18 10:54:55 发布