feat(android): add more interfaces
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package com.slightech.mynteye.demo;
|
||||
|
||||
import android.app.Application;
|
||||
//import com.stericson.RootShell.RootShell;
|
||||
import timber.log.Timber;
|
||||
|
||||
//import com.stericson.RootShell.RootShell;
|
||||
|
||||
public class MyApplication extends Application {
|
||||
|
||||
static {
|
||||
|
||||
@@ -4,12 +4,17 @@ import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import com.slightech.mynteye.Device;
|
||||
import com.slightech.mynteye.DeviceUsbInfo;
|
||||
import com.slightech.mynteye.Info;
|
||||
import com.slightech.mynteye.MotionData;
|
||||
import com.slightech.mynteye.MotionIntrinsics;
|
||||
import com.slightech.mynteye.Option;
|
||||
import com.slightech.mynteye.Source;
|
||||
import com.slightech.mynteye.Stream;
|
||||
import com.slightech.mynteye.StreamData;
|
||||
import com.slightech.mynteye.StreamRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import timber.log.Timber;
|
||||
|
||||
public final class Mynteye implements Runnable {
|
||||
|
||||
@@ -19,6 +24,7 @@ public final class Mynteye implements Runnable {
|
||||
private Handler mBackgroundHandler;
|
||||
|
||||
private boolean mOpened;
|
||||
private boolean mImuEnabled;
|
||||
|
||||
public interface OnStreamDataReceiveListener {
|
||||
void onStreamDataReceive(Stream stream, StreamData data, Handler handler);
|
||||
@@ -33,9 +39,12 @@ public final class Mynteye implements Runnable {
|
||||
private OnStreamDataReceiveListener mOnStreamDataReceiveListener;
|
||||
private OnMotionDataReceiveListener mOnMotionDataReceiveListener;
|
||||
|
||||
private StreamRequest mStreamRequest;
|
||||
|
||||
public Mynteye(DeviceUsbInfo info) {
|
||||
mDevice = Device.create(info);
|
||||
mOpened = false;
|
||||
mImuEnabled = false;
|
||||
}
|
||||
|
||||
public void setOnStreamDataReceiveListener(OnStreamDataReceiveListener l) {
|
||||
@@ -50,15 +59,95 @@ public final class Mynteye implements Runnable {
|
||||
return mDevice.getStreamRequests();
|
||||
}
|
||||
|
||||
public String getDeviceInfos() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (Info info : Info.values()) {
|
||||
sb.append(info.toString());
|
||||
sb.append(": ");
|
||||
sb.append(mDevice.getInfo(info));
|
||||
sb.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String getImageParams() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(Stream.LEFT).append('\n').append(mDevice.getIntrinsics(Stream.LEFT));
|
||||
sb.append("\n\n");
|
||||
sb.append(Stream.RIGHT).append('\n').append(mDevice.getIntrinsics(Stream.RIGHT));
|
||||
sb.append("\n\n");
|
||||
sb.append(Stream.LEFT).append(" > ").append(Stream.RIGHT);
|
||||
sb.append('\n');
|
||||
sb.append(mDevice.getExtrinsics(Stream.LEFT, Stream.RIGHT));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String getImuParams() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
MotionIntrinsics in = mDevice.getMotionIntrinsics();
|
||||
sb.append("Accel\n").append(in.getAccel());
|
||||
sb.append("\n\n");
|
||||
sb.append("Gyro\n").append(in.getGyro());
|
||||
sb.append("\n\n");
|
||||
sb.append("Imu > ").append(Stream.LEFT).append('\n')
|
||||
.append(mDevice.getMotionExtrinsics(Stream.LEFT));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String getOptionInfos() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (Option op : Option.values()) {
|
||||
if (!mDevice.supportsOption(op)) {
|
||||
continue;
|
||||
}
|
||||
sb.append(op.toString());
|
||||
sb.append(": ");
|
||||
sb.append(mDevice.getOptionValue(op));
|
||||
sb.append("\n ");
|
||||
sb.append(mDevice.getOptionInfo(op));
|
||||
sb.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public boolean isOpened() {
|
||||
return mOpened;
|
||||
}
|
||||
|
||||
public boolean isImuEnabled() {
|
||||
return mImuEnabled;
|
||||
}
|
||||
|
||||
public void setImuEnabled(boolean enabled) {
|
||||
mImuEnabled = enabled;
|
||||
if (mOpened) {
|
||||
Timber.w("Will enable imu when open next time");
|
||||
}
|
||||
}
|
||||
|
||||
public void open() {
|
||||
if (mOpened) return;
|
||||
if (mStreamRequest == null) {
|
||||
Timber.w("Should open with stream request");
|
||||
return;
|
||||
}
|
||||
open(mStreamRequest);
|
||||
}
|
||||
|
||||
public void open(StreamRequest request) {
|
||||
if (mOpened) return;
|
||||
mOpened = true;
|
||||
mStreamRequest = request;
|
||||
|
||||
startBackgroundThread();
|
||||
|
||||
mDevice.configStreamRequest(request);
|
||||
//mDevice.enableMotionDatas(Integer.MAX_VALUE);
|
||||
//mDevice.start(Source.ALL);
|
||||
mDevice.start(Source.VIDEO_STREAMING);
|
||||
if (mImuEnabled) {
|
||||
mDevice.enableMotionDatas(Integer.MAX_VALUE);
|
||||
mDevice.start(Source.ALL);
|
||||
} else {
|
||||
mDevice.start(Source.VIDEO_STREAMING);
|
||||
}
|
||||
|
||||
mBackgroundHandler.post(this);
|
||||
}
|
||||
@@ -92,14 +181,12 @@ public final class Mynteye implements Runnable {
|
||||
}
|
||||
|
||||
//Timber.i("get motions");
|
||||
/*
|
||||
{
|
||||
if (mImuEnabled) {
|
||||
ArrayList<MotionData> datas = mDevice.getMotionDatas();
|
||||
if (mOnMotionDataReceiveListener != null) {
|
||||
mOnMotionDataReceiveListener.onMotionDataReceive(datas, mBackgroundHandler);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (mOpened) mBackgroundHandler.post(this);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.slightech.mynteye.demo.ui;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.ArrayAdapter;
|
||||
@@ -15,10 +13,8 @@ import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import com.slightech.mynteye.Device;
|
||||
import com.slightech.mynteye.DeviceUsbInfo;
|
||||
import com.slightech.mynteye.Frame;
|
||||
import com.slightech.mynteye.MotionData;
|
||||
@@ -27,13 +23,11 @@ import com.slightech.mynteye.StreamData;
|
||||
import com.slightech.mynteye.StreamRequest;
|
||||
import com.slightech.mynteye.demo.R;
|
||||
import com.slightech.mynteye.demo.camera.Mynteye;
|
||||
import com.slightech.mynteye.demo.util.RootUtils;
|
||||
import com.slightech.mynteye.usb.CameraDialog;
|
||||
import com.slightech.mynteye.usb.USBMonitor;
|
||||
import com.slightech.mynteye.usb.USBMonitor.OnDeviceConnectListener;
|
||||
import com.slightech.mynteye.usb.USBMonitor.UsbControlBlock;
|
||||
import com.slightech.mynteye.util.BitmapUtils;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
import timber.log.Timber;
|
||||
@@ -50,12 +44,13 @@ public class MainActivity extends BaseActivity implements CameraDialog.CameraDia
|
||||
private Mynteye mMynteye;
|
||||
private Bitmap mLeftBitmap, mRightBitmap;
|
||||
|
||||
private boolean mImuEnabled;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
mUSBMonitor = new USBMonitor(this, mOnDeviceConnectListener);
|
||||
}
|
||||
|
||||
@@ -63,6 +58,9 @@ public class MainActivity extends BaseActivity implements CameraDialog.CameraDia
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
mUSBMonitor.register();
|
||||
if (mMynteye == null) {
|
||||
//actionOpen();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -144,29 +142,72 @@ public class MainActivity extends BaseActivity implements CameraDialog.CameraDia
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
||||
MenuItem item = menu.findItem(R.id.action_open);
|
||||
if (item != null) {
|
||||
item.setEnabled(false);
|
||||
actionOpen(() -> item.setEnabled(true));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
if (mMynteye == null) {
|
||||
menu.findItem(R.id.action_open).setVisible(true);
|
||||
menu.findItem(R.id.action_close).setVisible(false);
|
||||
} else {
|
||||
menu.findItem(R.id.action_open).setVisible(!mMynteye.isOpened());
|
||||
menu.findItem(R.id.action_close).setVisible(mMynteye.isOpened());
|
||||
}
|
||||
menu.findItem(R.id.check_imu_data).setChecked(mImuEnabled);
|
||||
boolean featuresUsable = mMynteye != null && mMynteye.isOpened();
|
||||
menu.findItem(R.id.show_device_infos).setVisible(featuresUsable);
|
||||
menu.findItem(R.id.show_image_params).setVisible(featuresUsable);
|
||||
menu.findItem(R.id.show_imu_params).setVisible(featuresUsable);
|
||||
menu.findItem(R.id.show_option_infos).setVisible(featuresUsable);
|
||||
return super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_open:
|
||||
item.setEnabled(false);
|
||||
actionOpen(() -> item.setEnabled(true));
|
||||
actionOpen();
|
||||
return true;
|
||||
case R.id.action_close:
|
||||
actionClose();
|
||||
return true;
|
||||
case R.id.check_imu_data:
|
||||
mImuEnabled = !item.isChecked();
|
||||
item.setChecked(mImuEnabled);
|
||||
return true;
|
||||
case R.id.show_device_infos:
|
||||
alert(R.string.device_infos, mMynteye.getDeviceInfos());
|
||||
return true;
|
||||
case R.id.show_image_params:
|
||||
alert(R.string.image_params, mMynteye.getImageParams());
|
||||
return true;
|
||||
case R.id.show_imu_params:
|
||||
alert(R.string.imu_params, mMynteye.getImuParams());
|
||||
return true;
|
||||
case R.id.show_option_infos:
|
||||
alert(R.string.option_infos, mMynteye.getOptionInfos());
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
private void actionOpen(final Runnable completeEvent) {
|
||||
if (completeEvent != null) completeEvent.run();
|
||||
CameraDialog.showDialog(this);
|
||||
private void actionOpen() {
|
||||
if (mMynteye == null) {
|
||||
CameraDialog.showDialog(this);
|
||||
} else {
|
||||
mMynteye.setImuEnabled(mImuEnabled);
|
||||
mMynteye.open();
|
||||
}
|
||||
}
|
||||
|
||||
private void actionClose() {
|
||||
if (mMynteye != null) {
|
||||
mMynteye.close();
|
||||
mMynteye = null;
|
||||
}
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
private void openDevice(DeviceUsbInfo info) {
|
||||
@@ -174,6 +215,7 @@ public class MainActivity extends BaseActivity implements CameraDialog.CameraDia
|
||||
ArrayList<StreamRequest> requests = mMynteye.getStreamRequests();
|
||||
if (requests.isEmpty()) {
|
||||
alert("Warning", "There are no streams to request :(");
|
||||
mMynteye = null;
|
||||
} else {
|
||||
ArrayList<String> items = new ArrayList<>();
|
||||
for (StreamRequest req : requests) {
|
||||
@@ -189,7 +231,12 @@ public class MainActivity extends BaseActivity implements CameraDialog.CameraDia
|
||||
dialog.dismiss();
|
||||
mMynteye.setOnStreamDataReceiveListener(this);
|
||||
mMynteye.setOnMotionDataReceiveListener(this);
|
||||
mMynteye.setImuEnabled(mImuEnabled);
|
||||
mMynteye.open(requests.get(position));
|
||||
invalidateOptionsMenu();
|
||||
});
|
||||
dialog.setOnCancelListener(dlg -> {
|
||||
mMynteye = null;
|
||||
});
|
||||
dialog.setView(listView);
|
||||
dialog.show();
|
||||
@@ -237,10 +284,18 @@ public class MainActivity extends BaseActivity implements CameraDialog.CameraDia
|
||||
mTextView.post(() -> mTextView.setText(datas.get(0).imu().toString()));
|
||||
}
|
||||
|
||||
private void toast(int textId) {
|
||||
toast(getString(textId));
|
||||
}
|
||||
|
||||
private void toast(CharSequence text) {
|
||||
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
private void alert(int titleId, CharSequence message) {
|
||||
alert(getString(titleId), message);
|
||||
}
|
||||
|
||||
private void alert(CharSequence title, CharSequence message) {
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle(title)
|
||||
|
||||
@@ -3,7 +3,37 @@
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/action_open"
|
||||
android:orderInCategory="0"
|
||||
android:title="@string/open"
|
||||
app:showAsAction="ifRoom|withText" />
|
||||
<item
|
||||
android:id="@+id/action_close"
|
||||
android:title="@string/close"
|
||||
app:showAsAction="ifRoom|withText" />
|
||||
|
||||
<item android:title="Features"
|
||||
android:enabled="false" />
|
||||
|
||||
<item
|
||||
android:id="@+id/check_imu_data"
|
||||
android:title="@string/imu_data"
|
||||
android:checkable="true"
|
||||
android:checked="false"
|
||||
app:showAsAction="never" />
|
||||
|
||||
<item
|
||||
android:id="@+id/show_device_infos"
|
||||
android:title="@string/device_infos"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/show_image_params"
|
||||
android:title="@string/image_params"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/show_imu_params"
|
||||
android:title="@string/imu_params"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/show_option_infos"
|
||||
android:title="@string/option_infos"
|
||||
app:showAsAction="never" />
|
||||
</menu>
|
||||
|
||||
@@ -2,4 +2,12 @@
|
||||
<string name="app_name">mynteye</string>
|
||||
|
||||
<string name="open">Open</string>
|
||||
<string name="close">Close</string>
|
||||
|
||||
<string name="imu_data">Imu Data</string>
|
||||
|
||||
<string name="device_infos">Device Infos</string>
|
||||
<string name="image_params">Image Params</string>
|
||||
<string name="imu_params">Imu Params</string>
|
||||
<string name="option_infos">Option Infos</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user