From 9ab2886b93a7eda993274be9e30de48e3e705f71 Mon Sep 17 00:00:00 2001 From: Shawn Hanna Date: Wed, 23 May 2018 22:54:49 -0400 Subject: [PATCH 1/2] added gamepad support. had to move from joystickview to teleoppanel because the event wasn't propogating when inside joystickview --- .../com/platypus/android/tablet/DrawView.java | 82 +++++++++++++ .../android/tablet/Joystick/JoystickView.java | 9 ++ .../platypus/android/tablet/Path/Region.java | 1 + .../platypus/android/tablet/TeleOpPanel.java | 111 ++++++++++++++++++ 4 files changed, 203 insertions(+) create mode 100644 app/src/main/java/com/platypus/android/tablet/DrawView.java diff --git a/app/src/main/java/com/platypus/android/tablet/DrawView.java b/app/src/main/java/com/platypus/android/tablet/DrawView.java new file mode 100644 index 0000000..fc712b3 --- /dev/null +++ b/app/src/main/java/com/platypus/android/tablet/DrawView.java @@ -0,0 +1,82 @@ +package com.platypus.android.tablet; + +/** + * Created by shenty on 1/16/16. + */ + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.DashPathEffect; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.PointF; +import android.util.AttributeSet; +import android.view.View; + + +import java.util.ArrayList; +import java.util.List; + +//taken from stack overflow answer.. +public class DrawView extends View { + private Paint paint; + private Path path; + private boolean close = false; + ArrayList points; + + public DrawView(Context c){ + this(c, null); + } + + public DrawView(Context c, AttributeSet s) { + super(c, s); + + paint=new Paint(); + + paint.setColor(Color.BLACK); + paint.setStrokeWidth(5); + paint.setAntiAlias(true); + paint.setStyle(Paint.Style.STROKE); + } + public void setPaint (String color, float width, boolean dotted){ + switch (color){ + case "blue": + paint.setColor(Color.BLUE); + break; + case "green": + paint.setColor(Color.GREEN); + break; + default: + paint.setColor(Color.GRAY); + } + paint.setStrokeWidth(width); + if(dotted){ + paint.setPathEffect(new DashPathEffect(new float[]{10,10}, 5)); + } + } + public void setClose (boolean c){ + close = c; + } + + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + //canvas.drawLine(0,0,1000,1000,black); + if (points == null || points.size() < 2) + { + return; + } + path = new Path(); + //path.reset(); + path.moveTo(points.get(0).x, points.get(0).y); + for (int i = 1; i < points.size(); i++) { + path.lineTo(points.get(i).x, points.get(i).y); + } + + if(close){ + path.close(); + } + + canvas.drawPath(path, paint); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java b/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java index d4b7469..700b997 100644 --- a/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java +++ b/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java @@ -8,9 +8,12 @@ import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; +import android.util.Log; import android.view.HapticFeedbackConstants; +import android.view.InputDevice; import android.view.MotionEvent; import android.view.View; +import java.util.ArrayList; public class JoystickView extends View { public static final int INVALID_POINTER_ID = -1; @@ -140,6 +143,12 @@ private void initJoystickView() { yAxisInverted = true; userCoordinateSystem = COORDINATE_CARTESIAN; autoReturnToCenter = true; +// +// // see if a gamepad is connected +// ArrayList gameControllerIds = getGameControllerIds(); +// if (gameControllerIds.size() > 0) { +// Log.d(TAG, "Number of controllers connected: " + gameControllerIds.size()); +// } } public void setYAxisInverted(boolean yAxisInverted) { diff --git a/app/src/main/java/com/platypus/android/tablet/Path/Region.java b/app/src/main/java/com/platypus/android/tablet/Path/Region.java index 5337a28..75389a9 100644 --- a/app/src/main/java/com/platypus/android/tablet/Path/Region.java +++ b/app/src/main/java/com/platypus/android/tablet/Path/Region.java @@ -4,6 +4,7 @@ import com.mapbox.mapboxsdk.geometry.LatLng; + //TODO what is causing the random lines across the polygon that occur in spiral mode? //TODO ok caused by the previous polygon has points that get added for some reason //TODO fix the random lines diff --git a/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java b/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java index 4497066..e8e27c5 100644 --- a/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java +++ b/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java @@ -64,13 +64,16 @@ import android.os.Environment; import android.os.Handler; import android.os.Looper; + import android.preference.PreferenceManager; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import android.support.v4.content.ContextCompat; import android.util.JsonReader; +import android.view.InputDevice; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import com.platypus.android.tablet.Path.AreaType; @@ -2178,4 +2181,112 @@ public void calculatePathDistance() } path_length_value.setText(Long.toString(Math.round(total_distance))); } + + + + public ArrayList getGameControllerIds() { + ArrayList gameControllerDeviceIds = new ArrayList(); + int[] deviceIds = InputDevice.getDeviceIds(); + for (int deviceId : deviceIds) { + InputDevice dev = InputDevice.getDevice(deviceId); + int sources = dev.getSources(); + + // Verify that the device has gamepad buttons, control sticks, or both. + if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) + || ((sources & InputDevice.SOURCE_JOYSTICK) + == InputDevice.SOURCE_JOYSTICK)) { + // This device is a game controller. Store its device ID. + if (!gameControllerDeviceIds.contains(deviceId)) { + gameControllerDeviceIds.add(deviceId); + } + } + } + return gameControllerDeviceIds; + } + + @Override + public boolean onGenericMotionEvent(MotionEvent event) { + Log.d("JoystickView", "got event: " + event); + + // Check that the event came from a game controller + if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == + InputDevice.SOURCE_JOYSTICK && + event.getAction() == MotionEvent.ACTION_MOVE) { + + // Process all historical movement samples in the batch + final int historySize = event.getHistorySize(); + + // Process the movements starting from the + // earliest historical position in the batch + for (int i = 0; i < historySize; i++) { + // Process the event at historical position i + processJoystickInput(event, i); + } + + // Process the current movement sample in the batch (position -1) + processJoystickInput(event, -1); + return true; + } + return super.onGenericMotionEvent(event); + } + + private static float getCenteredAxis(MotionEvent event, + InputDevice device, int axis, int historyPos) { + final InputDevice.MotionRange range = + device.getMotionRange(axis, event.getSource()); + + // A joystick at rest does not always report an absolute position of + // (0,0). Use the getFlat() method to determine the range of values + // bounding the joystick axis center. + if (range != null) { + final float flat = range.getFlat(); + final float value = + historyPos < 0 ? event.getAxisValue(axis): + event.getHistoricalAxisValue(axis, historyPos); + + // Ignore axis values that are within the 'flat' region of the + // joystick axis center. + if (Math.abs(value) > flat) { + return value; + } + } + return 0; + } + + private void processJoystickInput(MotionEvent event, + int historyPos) { + + InputDevice mInputDevice = event.getDevice(); + + // Calculate the horizontal distance to move by + // using the input value from one of these physical controls: + // the left control stick, hat axis, or the right control stick. + float x = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_X, historyPos); + if (x == 0) { + x = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_HAT_X, historyPos); + } + if (x == 0) { + x = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_Z, historyPos); + } + + // Calculate the vertical distance to move by + // using the input value from one of these physical controls: + // the left control stick, hat switch, or the right control stick. + float y = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_Y, historyPos); + if (y == 0) { + y = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_HAT_Y, historyPos); + } + if (y == 0) { + y = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_RZ, historyPos); + } + + joystick_moved_listener.OnMoved((int) (x * 10), (int)(y * 10)); + } + } From f631580ea91591521989cd2c2e8aafa0dfd30fd9 Mon Sep 17 00:00:00 2001 From: Shawn Hanna Date: Wed, 23 May 2018 22:58:26 -0400 Subject: [PATCH 2/2] removed unused DrawView class that was accidentaly added last commit. cleanup and moved code from joystick view to teleoppanel. fixed indent on last lines of telopview --- .idea/.name | 1 - .idea/misc.xml | 15 +- .idea/modules.xml | 1 + .../com/platypus/android/tablet/DrawView.java | 82 ------- .../android/tablet/Joystick/JoystickView.java | 10 +- .../platypus/android/tablet/TeleOpPanel.java | 215 +++++++++--------- 6 files changed, 114 insertions(+), 210 deletions(-) delete mode 100644 .idea/.name delete mode 100644 app/src/main/java/com/platypus/android/tablet/DrawView.java diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 65526af..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -tablet \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981..ba7052b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,5 @@ - - - - - - - - - - - - - - + diff --git a/.idea/modules.xml b/.idea/modules.xml index dec9681..9420233 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/app/src/main/java/com/platypus/android/tablet/DrawView.java b/app/src/main/java/com/platypus/android/tablet/DrawView.java deleted file mode 100644 index fc712b3..0000000 --- a/app/src/main/java/com/platypus/android/tablet/DrawView.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.platypus.android.tablet; - -/** - * Created by shenty on 1/16/16. - */ - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.DashPathEffect; -import android.graphics.Paint; -import android.graphics.Path; -import android.graphics.PointF; -import android.util.AttributeSet; -import android.view.View; - - -import java.util.ArrayList; -import java.util.List; - -//taken from stack overflow answer.. -public class DrawView extends View { - private Paint paint; - private Path path; - private boolean close = false; - ArrayList points; - - public DrawView(Context c){ - this(c, null); - } - - public DrawView(Context c, AttributeSet s) { - super(c, s); - - paint=new Paint(); - - paint.setColor(Color.BLACK); - paint.setStrokeWidth(5); - paint.setAntiAlias(true); - paint.setStyle(Paint.Style.STROKE); - } - public void setPaint (String color, float width, boolean dotted){ - switch (color){ - case "blue": - paint.setColor(Color.BLUE); - break; - case "green": - paint.setColor(Color.GREEN); - break; - default: - paint.setColor(Color.GRAY); - } - paint.setStrokeWidth(width); - if(dotted){ - paint.setPathEffect(new DashPathEffect(new float[]{10,10}, 5)); - } - } - public void setClose (boolean c){ - close = c; - } - - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - //canvas.drawLine(0,0,1000,1000,black); - if (points == null || points.size() < 2) - { - return; - } - path = new Path(); - //path.reset(); - path.moveTo(points.get(0).x, points.get(0).y); - for (int i = 1; i < points.size(); i++) { - path.lineTo(points.get(i).x, points.get(i).y); - } - - if(close){ - path.close(); - } - - canvas.drawPath(path, paint); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java b/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java index 700b997..edf63cf 100644 --- a/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java +++ b/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java @@ -3,17 +3,15 @@ /** * Created by zeshengxi on 10/28/15. */ + import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; -import android.util.Log; import android.view.HapticFeedbackConstants; -import android.view.InputDevice; import android.view.MotionEvent; import android.view.View; -import java.util.ArrayList; public class JoystickView extends View { public static final int INVALID_POINTER_ID = -1; @@ -143,12 +141,6 @@ private void initJoystickView() { yAxisInverted = true; userCoordinateSystem = COORDINATE_CARTESIAN; autoReturnToCenter = true; -// -// // see if a gamepad is connected -// ArrayList gameControllerIds = getGameControllerIds(); -// if (gameControllerIds.size() > 0) { -// Log.d(TAG, "Number of controllers connected: " + gameControllerIds.size()); -// } } public void setYAxisInverted(boolean yAxisInverted) { diff --git a/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java b/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java index e8e27c5..6eb3376 100644 --- a/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java +++ b/app/src/main/java/com/platypus/android/tablet/TeleOpPanel.java @@ -981,6 +981,13 @@ public void onNothingSelected(AdapterView parent) joystick.setOnJostickMovedListener(joystick_moved_listener); joystick.setOnJostickClickedListener(null); + + // see if a gamepad is connected + ArrayList gameControllerIds = getGameControllerIds(); + if (gameControllerIds.size() > 0) { + Log.d(TAG, "Number of controllers connected: " + gameControllerIds.size()); + } + senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); senAccelerometer = senSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); @@ -2184,109 +2191,109 @@ public void calculatePathDistance() - public ArrayList getGameControllerIds() { - ArrayList gameControllerDeviceIds = new ArrayList(); - int[] deviceIds = InputDevice.getDeviceIds(); - for (int deviceId : deviceIds) { - InputDevice dev = InputDevice.getDevice(deviceId); - int sources = dev.getSources(); - - // Verify that the device has gamepad buttons, control sticks, or both. - if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) - || ((sources & InputDevice.SOURCE_JOYSTICK) - == InputDevice.SOURCE_JOYSTICK)) { - // This device is a game controller. Store its device ID. - if (!gameControllerDeviceIds.contains(deviceId)) { - gameControllerDeviceIds.add(deviceId); - } - } - } - return gameControllerDeviceIds; - } - - @Override - public boolean onGenericMotionEvent(MotionEvent event) { - Log.d("JoystickView", "got event: " + event); - - // Check that the event came from a game controller - if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == - InputDevice.SOURCE_JOYSTICK && - event.getAction() == MotionEvent.ACTION_MOVE) { - - // Process all historical movement samples in the batch - final int historySize = event.getHistorySize(); - - // Process the movements starting from the - // earliest historical position in the batch - for (int i = 0; i < historySize; i++) { - // Process the event at historical position i - processJoystickInput(event, i); - } - - // Process the current movement sample in the batch (position -1) - processJoystickInput(event, -1); - return true; - } - return super.onGenericMotionEvent(event); - } - - private static float getCenteredAxis(MotionEvent event, - InputDevice device, int axis, int historyPos) { - final InputDevice.MotionRange range = - device.getMotionRange(axis, event.getSource()); - - // A joystick at rest does not always report an absolute position of - // (0,0). Use the getFlat() method to determine the range of values - // bounding the joystick axis center. - if (range != null) { - final float flat = range.getFlat(); - final float value = - historyPos < 0 ? event.getAxisValue(axis): - event.getHistoricalAxisValue(axis, historyPos); - - // Ignore axis values that are within the 'flat' region of the - // joystick axis center. - if (Math.abs(value) > flat) { - return value; - } - } - return 0; - } - - private void processJoystickInput(MotionEvent event, - int historyPos) { - - InputDevice mInputDevice = event.getDevice(); - - // Calculate the horizontal distance to move by - // using the input value from one of these physical controls: - // the left control stick, hat axis, or the right control stick. - float x = getCenteredAxis(event, mInputDevice, - MotionEvent.AXIS_X, historyPos); - if (x == 0) { - x = getCenteredAxis(event, mInputDevice, - MotionEvent.AXIS_HAT_X, historyPos); - } - if (x == 0) { - x = getCenteredAxis(event, mInputDevice, - MotionEvent.AXIS_Z, historyPos); - } - - // Calculate the vertical distance to move by - // using the input value from one of these physical controls: - // the left control stick, hat switch, or the right control stick. - float y = getCenteredAxis(event, mInputDevice, - MotionEvent.AXIS_Y, historyPos); - if (y == 0) { - y = getCenteredAxis(event, mInputDevice, - MotionEvent.AXIS_HAT_Y, historyPos); - } - if (y == 0) { - y = getCenteredAxis(event, mInputDevice, - MotionEvent.AXIS_RZ, historyPos); - } - - joystick_moved_listener.OnMoved((int) (x * 10), (int)(y * 10)); - } + public ArrayList getGameControllerIds() { + ArrayList gameControllerDeviceIds = new ArrayList(); + int[] deviceIds = InputDevice.getDeviceIds(); + for (int deviceId : deviceIds) { + InputDevice dev = InputDevice.getDevice(deviceId); + int sources = dev.getSources(); + + // Verify that the device has gamepad buttons, control sticks, or both. + if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) + || ((sources & InputDevice.SOURCE_JOYSTICK) + == InputDevice.SOURCE_JOYSTICK)) { + // This device is a game controller. Store its device ID. + if (!gameControllerDeviceIds.contains(deviceId)) { + gameControllerDeviceIds.add(deviceId); + } + } + } + return gameControllerDeviceIds; + } + + @Override + public boolean onGenericMotionEvent(MotionEvent event) { + Log.d("JoystickView", "got event: " + event); + + // Check that the event came from a game controller + if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == + InputDevice.SOURCE_JOYSTICK && + event.getAction() == MotionEvent.ACTION_MOVE) { + + // Process all historical movement samples in the batch + final int historySize = event.getHistorySize(); + + // Process the movements starting from the + // earliest historical position in the batch + for (int i = 0; i < historySize; i++) { + // Process the event at historical position i + processJoystickInput(event, i); + } + + // Process the current movement sample in the batch (position -1) + processJoystickInput(event, -1); + return true; + } + return super.onGenericMotionEvent(event); + } + + private static float getCenteredAxis(MotionEvent event, + InputDevice device, int axis, int historyPos) { + final InputDevice.MotionRange range = + device.getMotionRange(axis, event.getSource()); + + // A joystick at rest does not always report an absolute position of + // (0,0). Use the getFlat() method to determine the range of values + // bounding the joystick axis center. + if (range != null) { + final float flat = range.getFlat(); + final float value = + historyPos < 0 ? event.getAxisValue(axis): + event.getHistoricalAxisValue(axis, historyPos); + + // Ignore axis values that are within the 'flat' region of the + // joystick axis center. + if (Math.abs(value) > flat) { + return value; + } + } + return 0; + } + + private void processJoystickInput(MotionEvent event, + int historyPos) { + + InputDevice mInputDevice = event.getDevice(); + + // Calculate the horizontal distance to move by + // using the input value from one of these physical controls: + // the left control stick, hat axis, or the right control stick. + float x = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_X, historyPos); + if (x == 0) { + x = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_HAT_X, historyPos); + } + if (x == 0) { + x = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_Z, historyPos); + } + + // Calculate the vertical distance to move by + // using the input value from one of these physical controls: + // the left control stick, hat switch, or the right control stick. + float y = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_Y, historyPos); + if (y == 0) { + y = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_HAT_Y, historyPos); + } + if (y == 0) { + y = getCenteredAxis(event, mInputDevice, + MotionEvent.AXIS_RZ, historyPos); + } + + joystick_moved_listener.OnMoved((int) (x * 10), (int)(y * 10)); + } }