Skip to content
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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

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?

if ("END_OF_UTTERANCE".equals(eventType.toString())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to not an error?

Copy link
Author

@plattysoft plattysoft Nov 14, 2017

Choose a reason for hiding this comment

The 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);
Expand All @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the previous error log

}
mLedBlinkThread.setBlinking(false);
}
})
.build();
Expand All @@ -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();
Expand All @@ -229,6 +238,7 @@ protected void onDestroy() {
}
mLed = null;
}

if (mButton != null) {
try {
mButton.close();
Expand Down
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if doing a setBlinking(boolean) would be the right structure. Instead, doing something like blink() handle the logic in the class would be better.

Copy link
Author

Choose a reason for hiding this comment

The 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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if random blinking delays are ideal.

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the Log.e(TAG, <message>, e) instead?

} catch (InterruptedException e) {
e.printStackTrace();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the Log.e(TAG, <message>, e) instead?

}
}
}
while (!mClose);
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason for this change?

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down