-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #43 #49
fixes #43 #49
Changes from 3 commits
5adb615
6ddab48
9752d96
71d83cc
78b76c5
4196ed1
709227d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,14 +71,15 @@ public class AssistantActivity extends Activity implements Button.OnButtonEventL | |
private EmbeddedAssistant mEmbeddedAssistant; | ||
private ArrayList<String> mAssistantRequests = new ArrayList<>(); | ||
private ArrayAdapter<String> mAssistantRequestsAdapter; | ||
private LedBlinkThread mLedBlinkThread; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(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,14 @@ 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); | ||
|
||
mLedBlinkThread = new LedBlinkThread(mLed); | ||
mLedBlinkThread.start(); | ||
} catch (IOException e) { | ||
Log.e(TAG, "error configuring peripherals:", e); | ||
return; | ||
|
@@ -153,19 +159,21 @@ public void run() { | |
@Override | ||
public void onConversationEvent(EventType eventType) { | ||
Log.d(TAG, "converse response event: " + eventType); | ||
} | ||
|
||
@Override | ||
public void onAudioSample(ByteBuffer audioSample) { | ||
if (mLed != null) { | ||
if ("END_OF_UTTERANCE".equals(eventType.toString())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure this is a constant |
||
try { | ||
mLed.setValue(!mLed.getValue()); | ||
mLed.setValue(true); | ||
} catch (IOException e) { | ||
Log.w(TAG, "error toggling LED:", e); | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep the current error message? |
||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onAudioSample(ByteBuffer audioSample) { | ||
Log.e(TAG, "onAudioSample"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change this to not an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, I'll make it info like the other ones |
||
mLedBlinkThread.setBlinking(true); | ||
} | ||
|
||
@Override | ||
public void onConversationError(Status error) { | ||
Log.e(TAG, "converse response error: " + error); | ||
|
@@ -190,13 +198,12 @@ public void onVolumeChanged(int percentage) { | |
@Override | ||
public void onConversationFinished() { | ||
Log.i(TAG, "assistant conversation finished"); | ||
if (mLed != null) { | ||
try { | ||
mLed.setValue(false); | ||
} catch (IOException e) { | ||
Log.e(TAG, "error turning off LED:", e); | ||
} | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep the current null checker |
||
mLed.setValue(true); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep the previous error log |
||
} | ||
mLedBlinkThread.setBlinking(false); | ||
} | ||
}) | ||
.build(); | ||
|
@@ -221,6 +228,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 +238,7 @@ protected void onDestroy() { | |
} | ||
mLed = null; | ||
} | ||
|
||
if (mButton != null) { | ||
try { | ||
mButton.close(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.example.androidthings.assistant; | ||
|
||
import com.google.android.things.pio.Gpio; | ||
|
||
import java.io.IOException; | ||
import java.util.Random; | ||
|
||
public class LedBlinkThread extends Thread { | ||
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 setBlinking(boolean b) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if doing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I originally used the boolean on the way that if you passed false it stopped, but I couldn't find an "end of speech" trigger, so I changed the behaviour but did not rename the method. |
||
if (mClose) { | ||
return; | ||
} | ||
if (mBlinking != b) { | ||
mBlinking = b; | ||
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(150+mRandom.nextInt(100)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if random blinking delays are ideal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes it feel a bit more dynamic, IMHO if the interval is always the same it feels too static There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide a short video/gif of each one as a comparison? I'm not convinced that the random blinking is better. Consistency is not bad. This project is meant to be a sample and not a highly polished and complex project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just changed it to a constant |
||
mLed.setValue(true); | ||
sleep(150+mRandom.nextInt(100)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do the |
||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do the |
||
} | ||
} | ||
} | ||
while (!mClose); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ buildscript { | |
jcenter() | ||
} | ||
dependencies { | ||
classpath 'com.android.tools.build:gradle:2.3.0' | ||
classpath 'com.android.tools.build:gradle:2.3.1' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any specific reason for this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing special besides Android Studio prompting for it. |
||
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0" | ||
|
||
// NOTE: Do not place your application dependencies here; they belong | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep this in, just in case there is no connected LED?