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/Joystick/JoystickView.java b/app/src/main/java/com/platypus/android/tablet/Joystick/JoystickView.java index d4b7469..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,6 +3,7 @@ /** * Created by zeshengxi on 10/28/15. */ + import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; 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..6eb3376 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; @@ -978,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); @@ -2178,4 +2188,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)); + } + }