AmidamaruZXC

Untitled

Oct 15th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.60 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.app.Activity;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.widget.TextView;
  10.  
  11. import com.google.android.things.pio.Gpio;
  12. import com.google.android.things.pio.GpioCallback;
  13. import com.google.android.things.pio.PeripheralManager;
  14.  
  15. import java.io.IOException;
  16.  
  17. /**
  18.  * Skeleton of an Android Things activity.
  19.  * <p>
  20.  * Android Things peripheral APIs are accessible through the PeripheralManager
  21.  * For example, the snippet below will open a GPIO pin and set it to HIGH:
  22.  * <p>
  23.  * PeripheralManager manager = PeripheralManager.getInstance();
  24.  * try {
  25.  * Gpio gpio = manager.openGpio("BCM6");
  26.  * gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
  27.  * gpio.setValue(true);
  28.  * } catch (IOException e) {
  29.  * Log.e(TAG, "Unable to access GPIO");
  30.  * }
  31.  * <p>
  32.  * You can find additional examples on GitHub: https://github.com/androidthings
  33.  */
  34. public class MainActivity extends Activity {
  35.  
  36.     private Gpio mREDLedGpio, mBlueLedGpio, mGREENLedGpio, mREDButtonGpio,
  37.             mBlueButtonGpio, mGREENButtonGpio;
  38.     ;
  39.     boolean redPressed = false;
  40.     boolean greenPressed = false;
  41.     boolean bluePressed = false;
  42.     static TextView text1;
  43.  
  44.     private GpioCallback mREDCallback = new GpioCallback() {
  45.         @Override
  46.         public boolean onGpioEdge(Gpio gpio) {
  47.             try {
  48.                 //text1 = findViewById(R.id.text1);
  49.                 redPressed = !redPressed;
  50.                 mREDLedGpio.setValue(redPressed);
  51.                 //text1.setTextColor(Color.RED);
  52.                 //text1.setText("RED");
  53.                 Log.i("GpioCallback", "Red LED button was pressed");
  54.             } catch (IOException e) {
  55.                 Log.e("onKeyDown", "Error on PeripheralIO API", e);
  56.             }
  57.             // Step 5. Return true to keep callback active.
  58.             return true;
  59.         }
  60.     };
  61.  
  62.     private GpioCallback mBLUECallback = new GpioCallback() {
  63.         @Override
  64.         public boolean onGpioEdge(Gpio gpio) {
  65.             try {
  66.                 //text1 = findViewById(R.id.text1);
  67.                 bluePressed = !bluePressed;
  68.                 mBlueLedGpio.setValue(bluePressed);
  69.                 MainActivity.text1.setTextColor(Color.RED);
  70.                 MainActivity.text1.setText("RED");
  71.                 Log.i("GpioCallback", "Red LED button was pressed");
  72.             } catch (IOException e) {
  73.                 Log.e("onKeyDown", "Error on PeripheralIO API", e);
  74.             }
  75.             // Step 5. Return true to keep callback active.
  76.             return true;
  77.         }
  78.     };
  79.  
  80.     private GpioCallback mGREENCallback = new GpioCallback() {
  81.         @Override
  82.         public boolean onGpioEdge(Gpio gpio) {
  83.             try {
  84.                 //text1 = findViewById(R.id.text1);
  85.                 greenPressed = !greenPressed;
  86.                 mGREENLedGpio.setValue(greenPressed);
  87.                 //text1.setTextColor(Color.RED);
  88.                 //text1.setText("RED");
  89.                 Log.i("GpioCallback", "Red LED button was pressed");
  90.             } catch (IOException e) {
  91.                 Log.e("onKeyDown", "Error on PeripheralIO API", e);
  92.             }
  93.             // Step 5. Return true to keep callback active.
  94.             return true;
  95.         }
  96.     };
  97.  
  98.     @Override
  99.     protected void onDestroy() {
  100.         super.onDestroy();
  101.         // Step 6. Close the resource
  102.         if (mREDButtonGpio != null) {
  103.             mREDButtonGpio.unregisterGpioCallback(mREDCallback);
  104.             try {
  105.                 mREDButtonGpio.close();
  106.             } catch (IOException e) {
  107.                 Log.e("onDestroy", "Error on PeripheralIO API", e);
  108.             }
  109.         }
  110.         if (mREDLedGpio != null) {
  111.             try {
  112.                 mREDLedGpio.close();
  113.             } catch (IOException e) {
  114.                 Log.e("onDestroy", "Error on PeripheralIO API", e);
  115.             }
  116.         }
  117.     }
  118.  
  119.  
  120.     @Override
  121.     protected void onCreate(Bundle savedInstanceState) {
  122.         super.onCreate(savedInstanceState);
  123.         setContentView(R.layout.activity_main);
  124.         text1 = findViewById(R.id.text1);
  125.         PeripheralManager manager = PeripheralManager.getInstance();
  126.         try {
  127.             mREDLedGpio = manager.openGpio("BCM19"); //red color
  128.             // Step 2. Configure as an output.
  129.             mREDLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); //is lit
  130.             // Step 1. Create GPIO connection.
  131.             mREDButtonGpio = manager.openGpio("BCM4"); //red color button
  132.             // Step 2. Configure as an input.
  133.             mREDButtonGpio.setDirection(Gpio.DIRECTION_IN);
  134.             // Step 3. Enable edge trigger events.
  135.             mREDButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
  136.             // Step 4. Register an event callback.
  137.             mREDButtonGpio.registerGpioCallback(mREDCallback);
  138.         } catch (IOException e) {
  139.             Log.e("onCreate", "Error on PeripheralIO API", e);
  140.         }
  141.         try {
  142.             mBlueLedGpio = manager.openGpio("BCM16"); //red color
  143.             // Step 2. Configure as an output.
  144.             mBlueLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); //is lit
  145.             // Step 1. Create GPIO connection.
  146.             mBlueButtonGpio = manager.openGpio("BCM17"); //red color button
  147.             // Step 2. Configure as an input.
  148.             mBlueButtonGpio.setDirection(Gpio.DIRECTION_IN);
  149.             // Step 3. Enable edge trigger events.
  150.             mBlueButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
  151.             // Step 4. Register an event callback.
  152.             mBlueButtonGpio.registerGpioCallback(mBLUECallback);
  153.         } catch (IOException e) {
  154.             Log.e("onCreate", "Error on PeripheralIO API", e);
  155.         }
  156.         try {
  157.             mGREENLedGpio = manager.openGpio("BCM26"); //red color
  158.             // Step 2. Configure as an output.
  159.             mGREENLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); //is lit
  160.             // Step 1. Create GPIO connection.
  161.             mGREENButtonGpio = manager.openGpio("BCM27"); //red color button
  162.             // Step 2. Configure as an input.
  163.             mGREENButtonGpio.setDirection(Gpio.DIRECTION_IN);
  164.             // Step 3. Enable edge trigger events.
  165.             mGREENButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
  166.             // Step 4. Register an event callback.
  167.             mGREENButtonGpio.registerGpioCallback(mGREENCallback);
  168.         } catch (IOException e) {
  169.             Log.e("onCreate", "Error on PeripheralIO API", e);
  170.         }
  171.  
  172.  
  173.     }
  174. }
  175.  
Add Comment
Please, Sign In to add comment