安卓gpio操作示例

GPIO值在RK3288中的计算方法为:bank×32+pin,如: 
GPIO7A3: 7×32 + 0*8 +3=227 
GPIO0B5: 0×32+ 1*8 +5=13 


1. 导出
/sys/class/gpio# echo 44 > export

2. 设置方向
/sys/class/gpio/gpio44# echo out > direction

3. 查看方向
/sys/class/gpio/gpio44# cat direction

4. 设置输出
/sys/class/gpio/gpio44# echo 1 > value

5. 查看输出值
/sys/class/gpio/gpio44# cat value

6. 取消导出
/sys/class/gpio# echo 44 > unexport

读引脚的value

getGpioString("/sys/class/gpio/gpio256/value")

读方法,如果找不到引脚,进行导出 

    //读GPIO
    public String getGpioString(String path) {
        String defString = "99";// 默认值
        try {
            @SuppressWarnings("resource")
            BufferedReader reader = new BufferedReader(new FileReader(path));
            defString = reader.readLine();
        } catch (IOException e) {
            LogUtils.e(e.getMessage());
            RootCommand("echo  256 > /sys/class/gpio/export");
            e.printStackTrace();
        }
        return defString;
    }
    //  其他的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;
    }

你可能感兴趣的:(安卓)