自己写的GPIO 类的读写操作 。
package cn.com.mdj.robot;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
public class GpioHelper {
private static final String TAG = GpioHelper.class.getSimpleName();
private static boolean MdgSetGpioMode(int gpio, boolean output) {
String name = "/sys/class/gpio/gpio" + gpio + "/direction";
try {
File file = new File(name );
if(file.exists()) {
BufferedWriter out = new BufferedWriter(new FileWriter(name), 32);
try {
if(output)
out.write("out");
else
out.write("in");
}
finally {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static String MdgGetGpioValue(String cmd) {
File file = new File("/sys/devices/virtual/ctf/demo_gpio/gpio");
if(!file.exists())
file = new File("/sys/devices/virtual/ctf/demo_gpio/gpio");
// String cmd = "r4d010";
boolean r_en = 'r' == cmd.charAt(0) || 'R' == cmd.charAt(0);
byte r_val;
try {
RandomAccessFile rdf = null;
rdf = new RandomAccessFile(file,"rw");
rdf.writeBytes(cmd);
if(r_en)
{
Thread.sleep(1);
rdf.seek(0);
r_val = rdf.readByte();
String s= Byte.toString(r_val);
ASCIIToConvert(s);
if(ASCIIToConvert(s).equals("1")){
return "1";
}else {
return "0";
}
}
rdf.close();
} catch (Exception e) {
return "-1";
}
return "0";
}
public static String ASCIIToConvert(String value){
StringBuffer sbu = new StringBuffer();
String[] chars = value.split(" ");
for (int i = 0; i < chars.length; i++) {
sbu.append((char) Integer.parseInt(chars[i]));
}
return sbu.toString();
}
public static void MdgSetGpioValue( String cmd) {
File file = new File("/sys/devices/virtual/ctf/demo_gpio/gpio");
if(!file.exists())
file = new File("/sys/devices/virtual/ctf/demo_gpio/gpio");
boolean r_en = 'w' == cmd.charAt(0) || 'W' == cmd.charAt(0);
byte r_val;
try {
RandomAccessFile rdf = null;
rdf = new RandomAccessFile(file,"rw");
rdf.writeBytes(cmd);
Log.e("fjasmin", "MdgSetGpioValue: "+ cmd);
if(r_en)
{
Thread.sleep(1);
rdf.seek(0);
r_val = rdf.readByte();
System.out.println(r_val);
}
rdf.close();
} catch (Exception e) {
}
}
public static int MdgGetGpioValueProc(String gpio) {
String name = "/proc/oem_gpio/" + gpio;
try {
String str = null;
File file = new File(name );
if(file.exists()) {
BufferedReader in = new BufferedReader(new FileReader(name), 32);
try {
str = in.readLine();
if(str != null) {
if(str.equals("0"))
return 0;
else
return 1;
}
}
finally {
in.close();
}
}
} catch (Exception e) {
e.printStackTrace();
return -1;
}
return 0;
}
public static void MdgSetGpioValueProc(String gpio, int value) {
String name = "/proc/oem_gpio/" + gpio;
try {
File file = new File(name);
if(file.exists()) {
BufferedWriter out = new BufferedWriter(new FileWriter(name), 32);
try {
if(value != 0)
out.write("1");
else
out.write("0");
}
finally {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//控制GPIO的路径
public static void SetHightGpioValueProc(String gpio, int value) {
String name = "/sys/class/gpio/" + gpio+"/value";
Log.e(TAG, "SetHightGpioValueProc: " + name );
try {
File file = new File(name);
if(file.exists()) {
BufferedWriter out = new BufferedWriter(new FileWriter(name), 32);
try {
if(value != 0)
out.write("1");
else
out.write("0");
}
finally {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//通过对/sys/class/leds/brightness节点写数据来驱动LED灯打开或关闭。
//adb shell
//echo 125 > /sys/class/leds/brightness (打开LED灯,1-255,值越大,LED越亮)
//echo 0 > /sys/class/leds/brightness (关闭LED灯)
/**
rk3326_mid:/ $ echo 1 > sys/class/bat_charge_op_class/bat_charge_gpio
rk3326_mid:/ $ cat sys/class/bat_charge_op_class/bat_charge_gpio
car is allowed to charge
rk3326_mid:/ $ echo 0 > sys/class/bat_charge_op_class/bat_charge_gpio
rk3326_mid:/ $ cat sys/class/bat_charge_op_class/bat_charge_gpio
car is not allowed to charge
*/
public static void cmdPowerOpen() {
String cmd = "echo 1 > sys/class/bat_charge_op_class/bat_charge_gpio " + "\n";
try {
Process exeEcho = Runtime.getRuntime().exec("sh");
exeEcho.getOutputStream().write(cmd.getBytes());
exeEcho.getOutputStream().flush();
Log.d(TAG, " " + cmd);
Log.e(TAG,"打开充电");
} catch (Exception e) {
Log.d(TAG, "cmdRelayOpen faild");
}
}
public static void cmdPowerClose() {
String cmd = "echo 0 > sys/class/bat_charge_op_class/bat_charge_gpio " + "\n";
try {
Process exeEcho = Runtime.getRuntime().exec("sh");
exeEcho.getOutputStream().write(cmd.getBytes());
exeEcho.getOutputStream().flush();
Log.d(TAG, " " + cmd);
Log.e(TAG,"关闭充电");
} catch (Exception e) {
Log.d(TAG, "cmdRelayClose faild");
}
}
public static String readFileContent(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return sbf.toString();
}
//读GPIO
private String getGpioString(String path) {
String defString = "0";// 默认值
try {
@SuppressWarnings("resource")
BufferedReader reader = new BufferedReader(new FileReader(path));
defString = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return defString;
}
//将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口为输出的时候,通过以下的办法来控制高低电平
public boolean set_gpio0_high(View v) { //拉高
boolean FLAG = RootCommand("echo 1 > /sys/class/backlight/rk28_bl/gpio1");
Log.e("123high", String.valueOf(FLAG));
return FLAG;
}
public boolean set_gpio0_low(View v) { //拉低
boolean FLAG = RootCommand("echo 0 > /sys/class/backlight/rk28_bl/gpio1");
Log.e("123low", String.valueOf(FLAG));
// read_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;
}
}
网友评论