diff --git a/app/src/main/java/com/example/androidthings/assistant/AssistantActivity.java b/app/src/main/java/com/example/androidthings/assistant/AssistantActivity.java index 0deb0d3..b69d372 100644 --- a/app/src/main/java/com/example/androidthings/assistant/AssistantActivity.java +++ b/app/src/main/java/com/example/androidthings/assistant/AssistantActivity.java @@ -38,6 +38,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; + import org.json.JSONException; public class AssistantActivity extends Activity implements Button.OnButtonEventListener { @@ -70,6 +71,7 @@ public class AssistantActivity extends Activity implements Button.OnButtonEventL private EmbeddedAssistant mEmbeddedAssistant; private ArrayList mAssistantRequests = new ArrayList<>(); private ArrayAdapter mAssistantRequestsAdapter; + private LedBlinkThread mLedBlinkThread; @Override protected void onCreate(Bundle savedInstanceState) { @@ -77,7 +79,7 @@ protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "starting assistant demo"); setContentView(R.layout.activity_main); - ListView assistantRequestsListView = (ListView)findViewById(R.id.assistantRequestsListView); + ListView assistantRequestsListView = (ListView) findViewById(R.id.assistantRequestsListView); mAssistantRequestsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mAssistantRequests); @@ -107,10 +109,16 @@ protected void onCreate(Bundle savedInstanceState) { Button.LogicState.PRESSED_WHEN_LOW); mButton.setDebounceDelay(BUTTON_DEBOUNCE_DELAY_MS); mButton.setOnButtonEventListener(this); + PeripheralManagerService pioService = new PeripheralManagerService(); mLed = pioService.openGpio(BoardDefaults.getGPIOForLED()); mLed.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); mLed.setActiveType(Gpio.ACTIVE_LOW); + + if (mLed != null) { + mLedBlinkThread = new LedBlinkThread(mLed); + mLedBlinkThread.start(); + } } catch (IOException e) { Log.e(TAG, "error configuring peripherals:", e); return; @@ -153,16 +161,22 @@ public void run() { @Override public void onConversationEvent(EventType eventType) { Log.d(TAG, "converse response event: " + eventType); + if (EventType.END_OF_UTTERANCE.equals(eventType)) { + if (mLed != null) { + try { + mLed.setValue(true); + } catch (IOException e) { + Log.e(TAG, "error turning off LED:", e); + } + } + } } @Override public void onAudioSample(ByteBuffer audioSample) { - if (mLed != null) { - try { - mLed.setValue(!mLed.getValue()); - } catch (IOException e) { - Log.w(TAG, "error toggling LED:", e); - } + Log.i(TAG, "onAudioSample"); + if (mLedBlinkThread != null) { + mLedBlinkThread.blink(); } } @@ -192,7 +206,7 @@ public void onConversationFinished() { Log.i(TAG, "assistant conversation finished"); if (mLed != null) { try { - mLed.setValue(false); + mLed.setValue(true); } catch (IOException e) { Log.e(TAG, "error turning off LED:", e); } @@ -221,6 +235,8 @@ public void onButtonEvent(Button button, boolean pressed) { protected void onDestroy() { super.onDestroy(); Log.i(TAG, "destroying assistant demo"); + mLedBlinkThread.close(); + if (mLed != null) { try { mLed.close(); @@ -229,6 +245,7 @@ protected void onDestroy() { } mLed = null; } + if (mButton != null) { try { mButton.close(); diff --git a/app/src/main/java/com/example/androidthings/assistant/LedBlinkThread.java b/app/src/main/java/com/example/androidthings/assistant/LedBlinkThread.java new file mode 100644 index 0000000..a969a70 --- /dev/null +++ b/app/src/main/java/com/example/androidthings/assistant/LedBlinkThread.java @@ -0,0 +1,68 @@ +package com.example.androidthings.assistant; + +import android.util.Log; + +import com.google.android.things.pio.Gpio; + +import java.io.IOException; +import java.util.Random; + +public class LedBlinkThread extends Thread { + private static final String TAG = LedBlinkThread.class.getSimpleName(); + private static final long BLINK_INTERVAL = 200; + + private final Gpio mLed; + private final Random mRandom; + private boolean mBlinking = false; + private boolean mClose = false; + + public LedBlinkThread(Gpio led) { + mLed = led; + mRandom = new Random(); + } + + public void blink() { + if (mClose) { + return; + } + mBlinking = true; + synchronized (this) { + notify(); + } + } + + public void close() { + mClose = true; + mBlinking = false; + synchronized (this) { + notify(); + } + } + + @Override + public void run() { + do { + synchronized (this) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + while (mBlinking) { + mBlinking = false; + try { + mLed.setValue(false); + sleep(BLINK_INTERVAL); + mLed.setValue(true); + sleep(BLINK_INTERVAL); + } catch (IOException e) { + Log.e(TAG, "Error accessing the LED", e); + } catch (InterruptedException e) { + Log.e(TAG, "Error while sleeping", e); + } + } + } + while (!mClose); + } +} \ No newline at end of file