-
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 5 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,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.w(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.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 |
||
if (mLedBlinkThread != null) { | ||
mLedBlinkThread.blink(); | ||
} | ||
} | ||
|
||
|
@@ -192,9 +206,9 @@ public void onConversationFinished() { | |
Log.i(TAG, "assistant conversation finished"); | ||
if (mLed != null) { | ||
try { | ||
mLed.setValue(false); | ||
mLed.setValue(true); | ||
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. Shouldn't we turn off the LED in this case? 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. Since the LED is configured as ACTIVE_LOW, true is actually off (I said that as a note on the description) I find it confusing, but I did not want to change the original behaviour. 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. The 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. In my case led.setValue(false) does turn it on, I'm not sure if that is a configuration problem, a hardware difference or something that changed on the OS since this project was originally made 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. Actually the blinking is following this pattern, first on, then off, so at the end of each blink, it stays off. Let me know if you want me to revert all of them and change the active mode. 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 would revert the changes so that In my experience, the LED is 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 would be helpful if someone else could verify the LED state on different AIY voice kits and Android Things versions. Until then, I'm not inclined to change anything. IMHO opening the LED with the correct mode should be a responsibility of the voiceHAT driver. 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. Actually, after one more test / reading of the code, onButtonEvent the LED is off when pressed and turns on when released (pressed == false) The code is mLed.setValue(pressed) And that is code I have not touched. Unless it does not work as I described for you, then we clearly have some inconsistency on hardware / operating system. 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 do agree that the LED should be set up through the VoiceHat. Until that change is made and published to the driver, we should refrain from making this workaround. |
||
} catch (IOException e) { | ||
Log.e(TAG, "error turning off LED:", e); | ||
Log.w(TAG, "error turning off LED:", e); | ||
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 think keeping the level as error is preferable. 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 missed that 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. Did you make the 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. Sorry I missed that one. Fixed now |
||
} | ||
} | ||
} | ||
|
@@ -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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
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. Typically we don't have an empty line |
||
private static final String TAG = "LedBlinkingThread"; | ||
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. Typically we use 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 changed it. |
||
|
||
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(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) { | ||
Log.e(TAG, "Error accessing the LED", e); | ||
} catch (InterruptedException e) { | ||
Log.e(TAG, "Error while sleeping", e); | ||
} | ||
} | ||
} | ||
while (!mClose); | ||
} | ||
} |
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?