Search in sources :

Example 1 with Gpio

use of com.google.android.things.pio.Gpio in project sample-simpleui by androidthings.

the class SimpleUiActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout gpioPinsView = findViewById(R.id.gpio_list);
    LayoutInflater inflater = getLayoutInflater();
    PeripheralManager pioManager = PeripheralManager.getInstance();
    for (String name : pioManager.getGpioList()) {
        View child = inflater.inflate(R.layout.list_item_gpio, gpioPinsView, false);
        Switch button = child.findViewById(R.id.gpio_switch);
        button.setText(name);
        gpioPinsView.addView(button);
        Log.d(TAG, "Added button for GPIO: " + name);
        try {
            final Gpio ledPin = pioManager.openGpio(name);
            ledPin.setEdgeTriggerType(Gpio.EDGE_NONE);
            ledPin.setActiveType(Gpio.ACTIVE_HIGH);
            ledPin.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
            button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    try {
                        ledPin.setValue(isChecked);
                    } catch (IOException e) {
                        Log.e(TAG, "error toggling gpio:", e);
                        buttonView.setOnCheckedChangeListener(null);
                        // reset button to previous state.
                        buttonView.setChecked(!isChecked);
                        buttonView.setOnCheckedChangeListener(this);
                    }
                }
            });
            mGpioMap.put(name, ledPin);
        } catch (IOException e) {
            Log.e(TAG, "Error initializing GPIO: " + name, e);
            // disable button
            button.setEnabled(false);
        }
    }
}
Also used : Switch(android.widget.Switch) LayoutInflater(android.view.LayoutInflater) PeripheralManager(com.google.android.things.pio.PeripheralManager) IOException(java.io.IOException) View(android.view.View) LinearLayout(android.widget.LinearLayout) Gpio(com.google.android.things.pio.Gpio) CompoundButton(android.widget.CompoundButton)

Example 2 with Gpio

use of com.google.android.things.pio.Gpio in project robocar by androidthings.

the class TricolorLed method createGpio.

private Gpio createGpio(String pin) {
    try {
        Gpio gpio = PeripheralManager.getInstance().openGpio(pin);
        gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH);
        return gpio;
    } catch (IOException e) {
        Log.e(TAG, "Error creating GPIO for pin " + pin, e);
    }
    return null;
}
Also used : IOException(java.io.IOException) Gpio(com.google.android.things.pio.Gpio)

Aggregations

Gpio (com.google.android.things.pio.Gpio)2 IOException (java.io.IOException)2 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 CompoundButton (android.widget.CompoundButton)1 LinearLayout (android.widget.LinearLayout)1 Switch (android.widget.Switch)1 PeripheralManager (com.google.android.things.pio.PeripheralManager)1