content: /*
   * Copyright 2016, The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.example.androidthings.simplepio;
  
  import android.app.Activity;
  import com.google.android.things.pio.Gpio;
  import com.google.android.things.pio.PeripheralManager;
  import android.os.Bundle;
  import android.os.Handler;
  import android.util.Log;
  
  import java.io.IOException;
  
  /**
   * Sample usage of the Gpio API that blinks an LED at a fixed interval defined in
   * {@link #INTERVAL_BETWEEN_BLINKS_MS}.
   *
   * Some boards, like Intel Edison, have onboard LEDs linked to specific GPIO pins.
   * The preferred GPIO pin to use on each board is in the {@link BoardDefaults} class.
   *
   */
  public class BlinkActivity extends Activity {
      private static final String TAG = BlinkActivity.class.getSimpleName();
      private static final int INTERVAL_BETWEEN_BLINKS_MS = 1000;
  
      private Handler mHandler = new Handler();
      private Gpio mLedGpio;
      private boolean mLedState = false;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Log.i(TAG, "Starting BlinkActivity");
  
          try {
              String pinName = BoardDefaults.getGPIOForLED();
              mLedGpio = PeripheralManager.getInstance().openGpio(pinName);
              mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
              Log.i(TAG, "Start blinking LED GPIO pin");
              // Post a Runnable that continuously switch the state of the GPIO, blinking the
              // corresponding LED
              mHandler.post(mBlinkRunnable);
          } catch (IOException e) {
              Log.e(TAG, "Error on PeripheralIO API", e);
          }
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          // Remove pending blink Runnable from the handler.
          mHandler.removeCallbacks(mBlinkRunnable);
          // Close the Gpio pin.
          Log.i(TAG, "Closing LED GPIO pin");
          try {
              mLedGpio.close();
          } catch (IOException e) {
              Log.e(TAG, "Error on PeripheralIO API", e);
          } finally {
              mLedGpio = null;
          }
      }
  
      private Runnable mBlinkRunnable = new Runnable() {
          @Override
          public void run() {
              // Exit Runnable if the GPIO is already closed
              if (mLedGpio == null) {
                  return;
              }
              try {
                  // Toggle the GPIO state
                  mLedState = !mLedState;
                  mLedGpio.setValue(mLedState);
                  Log.d(TAG, "State set to " + mLedState);
  
                  // Reschedule the same runnable in {#INTERVAL_BETWEEN_BLINKS_MS} milliseconds
                  mHandler.postDelayed(mBlinkRunnable, INTERVAL_BETWEEN_BLINKS_MS);
              } catch (IOException e) {
                  Log.e(TAG, "Error on PeripheralIO API", e);
              }
          }
      };
  }
  