Search code examples
javaandroidandroid-activitygpio

How to use Java class for GPIO control to make an application that runs on Android OS


I am trying to develop an app that would allow GPIO testing, such as cat /sys/class/gpio/%s/direction, on a custom board with a touchscreen display.

I'm don't have too much experience with android development, and I'm not sure how I can implement this app.


Solution

  • If you are using the GPIO class like the one you provided you want to make a new instance for each GPIO you want to test. You don't have to create a direction class or anything like this - everything can be implemented in the MainActivity.java.

    package com.pckg.gpio;
    
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    
    public class MainActivity extends AppCompatActivity {
    
    private final String TAG = "GpioTest";
    private GPIO gpio27;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gpio27 = new GPIO(27) // new Instance of gpio 27
    
    
    final Button Direction = (Button) findViewById(R.id.ButtonDir);
        Direction.setOnClickListener(new View.OnClickListener() {
                                         public void onClick(View view) {
                                            Log.d(TAG, "direction for pin 27:" + gpio27.getInOut();
                                         }
        });
    }
    

    In this small example, you can see in the logs the directions for pin 27. I have created a new instance using the contractor in the GPIO class. You can do this for multiple gpios just create new instance.