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);
}
}
}
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;
}
Aggregations