Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TextView;
- import com.google.android.things.pio.Gpio;
- import com.google.android.things.pio.GpioCallback;
- import com.google.android.things.pio.PeripheralManager;
- import java.io.IOException;
- /**
- * Skeleton of an Android Things activity.
- * <p>
- * Android Things peripheral APIs are accessible through the PeripheralManager
- * For example, the snippet below will open a GPIO pin and set it to HIGH:
- * <p>
- * PeripheralManager manager = PeripheralManager.getInstance();
- * try {
- * Gpio gpio = manager.openGpio("BCM6");
- * gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
- * gpio.setValue(true);
- * } catch (IOException e) {
- * Log.e(TAG, "Unable to access GPIO");
- * }
- * <p>
- * You can find additional examples on GitHub: https://github.com/androidthings
- */
- public class MainActivity extends Activity {
- private Gpio mREDLedGpio, mBlueLedGpio, mGREENLedGpio, mREDButtonGpio,
- mBlueButtonGpio, mGREENButtonGpio;
- ;
- boolean redPressed = false;
- boolean greenPressed = false;
- boolean bluePressed = false;
- static TextView text1;
- private GpioCallback mREDCallback = new GpioCallback() {
- @Override
- public boolean onGpioEdge(Gpio gpio) {
- try {
- //text1 = findViewById(R.id.text1);
- redPressed = !redPressed;
- mREDLedGpio.setValue(redPressed);
- //text1.setTextColor(Color.RED);
- //text1.setText("RED");
- Log.i("GpioCallback", "Red LED button was pressed");
- } catch (IOException e) {
- Log.e("onKeyDown", "Error on PeripheralIO API", e);
- }
- // Step 5. Return true to keep callback active.
- return true;
- }
- };
- private GpioCallback mBLUECallback = new GpioCallback() {
- @Override
- public boolean onGpioEdge(Gpio gpio) {
- try {
- //text1 = findViewById(R.id.text1);
- bluePressed = !bluePressed;
- mBlueLedGpio.setValue(bluePressed);
- MainActivity.text1.setTextColor(Color.RED);
- MainActivity.text1.setText("RED");
- Log.i("GpioCallback", "Red LED button was pressed");
- } catch (IOException e) {
- Log.e("onKeyDown", "Error on PeripheralIO API", e);
- }
- // Step 5. Return true to keep callback active.
- return true;
- }
- };
- private GpioCallback mGREENCallback = new GpioCallback() {
- @Override
- public boolean onGpioEdge(Gpio gpio) {
- try {
- //text1 = findViewById(R.id.text1);
- greenPressed = !greenPressed;
- mGREENLedGpio.setValue(greenPressed);
- //text1.setTextColor(Color.RED);
- //text1.setText("RED");
- Log.i("GpioCallback", "Red LED button was pressed");
- } catch (IOException e) {
- Log.e("onKeyDown", "Error on PeripheralIO API", e);
- }
- // Step 5. Return true to keep callback active.
- return true;
- }
- };
- @Override
- protected void onDestroy() {
- super.onDestroy();
- // Step 6. Close the resource
- if (mREDButtonGpio != null) {
- mREDButtonGpio.unregisterGpioCallback(mREDCallback);
- try {
- mREDButtonGpio.close();
- } catch (IOException e) {
- Log.e("onDestroy", "Error on PeripheralIO API", e);
- }
- }
- if (mREDLedGpio != null) {
- try {
- mREDLedGpio.close();
- } catch (IOException e) {
- Log.e("onDestroy", "Error on PeripheralIO API", e);
- }
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- text1 = findViewById(R.id.text1);
- PeripheralManager manager = PeripheralManager.getInstance();
- try {
- mREDLedGpio = manager.openGpio("BCM19"); //red color
- // Step 2. Configure as an output.
- mREDLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); //is lit
- // Step 1. Create GPIO connection.
- mREDButtonGpio = manager.openGpio("BCM4"); //red color button
- // Step 2. Configure as an input.
- mREDButtonGpio.setDirection(Gpio.DIRECTION_IN);
- // Step 3. Enable edge trigger events.
- mREDButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
- // Step 4. Register an event callback.
- mREDButtonGpio.registerGpioCallback(mREDCallback);
- } catch (IOException e) {
- Log.e("onCreate", "Error on PeripheralIO API", e);
- }
- try {
- mBlueLedGpio = manager.openGpio("BCM16"); //red color
- // Step 2. Configure as an output.
- mBlueLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); //is lit
- // Step 1. Create GPIO connection.
- mBlueButtonGpio = manager.openGpio("BCM17"); //red color button
- // Step 2. Configure as an input.
- mBlueButtonGpio.setDirection(Gpio.DIRECTION_IN);
- // Step 3. Enable edge trigger events.
- mBlueButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
- // Step 4. Register an event callback.
- mBlueButtonGpio.registerGpioCallback(mBLUECallback);
- } catch (IOException e) {
- Log.e("onCreate", "Error on PeripheralIO API", e);
- }
- try {
- mGREENLedGpio = manager.openGpio("BCM26"); //red color
- // Step 2. Configure as an output.
- mGREENLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); //is lit
- // Step 1. Create GPIO connection.
- mGREENButtonGpio = manager.openGpio("BCM27"); //red color button
- // Step 2. Configure as an input.
- mGREENButtonGpio.setDirection(Gpio.DIRECTION_IN);
- // Step 3. Enable edge trigger events.
- mGREENButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
- // Step 4. Register an event callback.
- mGREENButtonGpio.registerGpioCallback(mGREENCallback);
- } catch (IOException e) {
- Log.e("onCreate", "Error on PeripheralIO API", e);
- }
- }
- }
Add Comment
Please, Sign In to add comment