diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java index b053d97ae..3f06bfcbe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/ControlActivity.java @@ -2,61 +2,91 @@ package nodomain.freeyourgadget.gadgetbridge.devices.supercars; import android.content.Intent; import android.os.Bundle; -import android.view.MotionEvent; -import android.view.View; +import android.widget.CheckBox; +import android.widget.CompoundButton; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity; import nodomain.freeyourgadget.gadgetbridge.service.devices.supercars.SuperCarsSupport; -public class ControlActivity extends AbstractGBActivity { +public class ControlActivity extends AbstractGBActivity implements JoystickView.JoystickListener { + private static final Logger LOG = LoggerFactory.getLogger(ControlActivity.class); LocalBroadcastManager localBroadcastManager; + boolean lights = false; + boolean turbo = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_supercars_control); localBroadcastManager = LocalBroadcastManager.getInstance(this); + CheckBox turboMode = findViewById(R.id.turboMode); + CheckBox lightsOn = findViewById(R.id.lightsOn); - View.OnTouchListener controlTouchListener = new View.OnTouchListener() { + turboMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override - public boolean onTouch(View v, MotionEvent event) { - String command = "idle"; - if (v.getId() == R.id.supercars_control_button_left_up) { - command = "left_up"; - } else if (v.getId() == R.id.supercars_control_button_center_up) { - command = "center_up"; - } else if (v.getId() == R.id.supercars_control_button_right_up) { - command = "right_up"; - } - if (v.getId() == R.id.supercars_control_button_left_down) { - command = "left_down"; - } else if (v.getId() == R.id.supercars_control_button_center_down) { - command = "center_down"; - } else if (v.getId() == R.id.supercars_control_button_right_down) { - command = "right_down"; - } - Intent intent = new Intent(SuperCarsSupport.COMMAND_DRIVE_CONTROL); - intent.putExtra(SuperCarsSupport.EXTRA_DIRECTION, command); - sendLocalBroadcast(intent); - return true; + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + turbo = isChecked; } - }; - - findViewById(R.id.supercars_control_button_left_up).setOnTouchListener(controlTouchListener); - findViewById(R.id.supercars_control_button_center_up).setOnTouchListener(controlTouchListener); - findViewById(R.id.supercars_control_button_right_up).setOnTouchListener(controlTouchListener); - findViewById(R.id.supercars_control_button_left_down).setOnTouchListener(controlTouchListener); - findViewById(R.id.supercars_control_button_center_down).setOnTouchListener(controlTouchListener); - findViewById(R.id.supercars_control_button_right_down).setOnTouchListener(controlTouchListener); - + }); + lightsOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + lights = isChecked; + } + }); } private void sendLocalBroadcast(Intent intent) { localBroadcastManager.sendBroadcast(intent); } + @Override + + public void onJoystickMoved(float xPercent, float yPercent, int id) { + if (yPercent != 0 && yPercent != 0) { + SuperCarsConstants.Directions command; + SuperCarsConstants.SpeedModes mode; + + if (yPercent < 0) { + command = SuperCarsConstants.Directions.UP; + if (xPercent < -0.5) { + command = SuperCarsConstants.Directions.UP_LEFT; + } else if (xPercent > 0.5) { + command = SuperCarsConstants.Directions.UP_RIGHT; + } + } else { + command = SuperCarsConstants.Directions.DOWN; + if (xPercent < -0.5) { + command = SuperCarsConstants.Directions.DOWN_LEFT; + } else if (xPercent > 0.5) { + command = SuperCarsConstants.Directions.DOWN_RIGHT; + } + } + + mode = SuperCarsConstants.SpeedModes.NORMAL; + if (lights) { + mode = SuperCarsConstants.SpeedModes.LIGHTS; + if (turbo) { + mode = SuperCarsConstants.SpeedModes.TURBO_LIGHTS; + } + } else { + if (turbo) { + mode = SuperCarsConstants.SpeedModes.TURBO; + } + } + + Intent intent = new Intent(SuperCarsSupport.COMMAND_DRIVE_CONTROL); + intent.putExtra(SuperCarsSupport.EXTRA_DIRECTION, command); + intent.putExtra(SuperCarsSupport.EXTRA_MODE, mode); + sendLocalBroadcast(intent); + } + + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/JoystickView.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/JoystickView.java new file mode 100644 index 000000000..a94676283 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/JoystickView.java @@ -0,0 +1,122 @@ +//GPL-3.0 license +//https://github.com/efficientisoceles/JoystickView + +package nodomain.freeyourgadget.gadgetbridge.devices.supercars; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.View; + +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.R; + +public class JoystickView extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener { + private float centerX; + private float centerY; + private float baseRadius; + private float hatRadius; + private JoystickListener joystickCallback; + private final int ratio = 5; + + private void setupDimensions() { + centerX = getWidth() / 2; + centerY = getHeight() / 2; + baseRadius = Math.min(getWidth(), getHeight()) / 3; + hatRadius = Math.min(getWidth(), getHeight()) / 5; + } + + public JoystickView(Context context) { + super(context); + getHolder().addCallback(this); + setOnTouchListener(this); + if (context instanceof JoystickListener) + joystickCallback = (JoystickListener) context; + } + + public JoystickView(Context context, AttributeSet attributes, int style) { + super(context, attributes, style); + getHolder().addCallback(this); + setOnTouchListener(this); + if (context instanceof JoystickListener) + joystickCallback = (JoystickListener) context; + } + + public JoystickView(Context context, AttributeSet attributes) { + super(context, attributes); + getHolder().addCallback(this); + setOnTouchListener(this); + if (context instanceof JoystickListener) + joystickCallback = (JoystickListener) context; + } + + private void drawJoystick(float newX, float newY) { + if (getHolder().getSurface().isValid()) { + Canvas myCanvas = this.getHolder().lockCanvas(); + Paint colors = new Paint(); + myCanvas.drawColor(GBApplication.getWindowBackgroundColor(getContext())); + + float hypotenuse = (float) Math.sqrt(Math.pow(newX - centerX, 2) + Math.pow(newY - centerY, 2)); + float sin = (newY - centerY) / hypotenuse; //sin = o/h + float cos = (newX - centerX) / hypotenuse; //cos = a/h + + colors.setColor(this.getResources().getColor(R.color.primarydark_light)); + myCanvas.drawCircle(centerX, centerY, baseRadius, colors); + + colors.setColor(this.getResources().getColor(R.color.chart_deep_sleep_light)); + + myCanvas.drawCircle(newX - cos * hypotenuse * (ratio / baseRadius) * 30, + newY - sin * hypotenuse * (ratio / baseRadius) * 30, 100, colors); + + colors.setColor(this.getResources().getColor(R.color.accent)); + myCanvas.drawCircle(newX, newY, hatRadius, colors); + + getHolder().unlockCanvasAndPost(myCanvas); + } + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + setupDimensions(); + drawJoystick(centerX, centerY); + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + + } + + public boolean onTouch(View v, MotionEvent e) { + if (v.equals(this)) { + if (e.getAction() != MotionEvent.ACTION_UP) { + float displacement = (float) Math.sqrt((Math.pow(e.getX() - centerX, 2)) + Math.pow(e.getY() - centerY, 2)); + if (displacement < baseRadius) { + drawJoystick(e.getX(), e.getY()); + joystickCallback.onJoystickMoved((e.getX() - centerX) / baseRadius, (e.getY() - centerY) / baseRadius, getId()); + } else { + float ratio = baseRadius / displacement; + float constrainedX = centerX + (e.getX() - centerX) * ratio; + float constrainedY = centerY + (e.getY() - centerY) * ratio; + drawJoystick(constrainedX, constrainedY); + joystickCallback.onJoystickMoved((constrainedX - centerX) / baseRadius, (constrainedY - centerY) / baseRadius, getId()); + } + } else + drawJoystick(centerX, centerY); + joystickCallback.onJoystickMoved(0, 0, getId()); + } + return true; + } + + public interface JoystickListener { + void onJoystickMoved(float xPercent, float yPercent, int id); + } +} \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java index 8a621c95e..aabed9ba0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/supercars/SuperCarsConstants.java @@ -14,15 +14,118 @@ public class SuperCarsConstants { public static final UUID CHARACTERISTIC_UUID_FD1 = UUID.fromString("0000fd01-0000-1000-8000-00805f9b34fb"); public static final UUID CHARACTERISTIC_UUID_FD2 = UUID.fromString("0000fd02-0000-1000-8000-00805f9b34fb"); - public static final byte[] idle_data = new byte[]{0x02, 0x5e, 0x69, 0x5a, 0x48, (byte) 0xff, 0x2a, 0x43, (byte) 0x8c, (byte) 0xa6, (byte) 0x80, (byte) 0xf8, 0x3e, 0x04, (byte) 0xe4, 0x5d}; - public static final byte[] up_data = new byte[]{0x29, 0x60, (byte) 0x9c, 0x66, 0x48, 0x52, (byte) 0xcf, (byte) 0xf1, (byte) 0xb0, (byte) 0xf0, (byte) 0xcb, (byte) 0xb9, (byte) 0x80, 0x14, (byte) 0xbd, 0x2c}; - public static final byte[] down_data = new byte[]{0x03, 0x20, (byte) 0x99, 0x09, (byte) 0xba, (byte) 0x9d, (byte) 0xa1, (byte) 0xc8, (byte) 0xb9, (byte) 0x86, 0x16, 0x3c, 0x6d, 0x48, 0x46, 0x55}; + public static final byte[] idle_normal = new byte[]{0x02, 0x5e, 0x69, 0x5a, 0x48, (byte) 0xff, 0x2a, 0x43, (byte) 0x8c, (byte) 0xa6, (byte) 0x80, (byte) 0xf8, 0x3e, 0x04, (byte) 0xe4, 0x5d}; + public static final byte[] up_normal = new byte[]{0x29, 0x60, (byte) 0x9c, 0x66, 0x48, 0x52, (byte) 0xcf, (byte) 0xf1, (byte) 0xb0, (byte) 0xf0, (byte) 0xcb, (byte) 0xb9, (byte) 0x80, 0x14, (byte) 0xbd, 0x2c}; + public static final byte[] down_normal = new byte[]{0x03, 0x20, (byte) 0x99, 0x09, (byte) 0xba, (byte) 0x9d, (byte) 0xa1, (byte) 0xc8, (byte) 0xb9, (byte) 0x86, 0x16, 0x3c, 0x6d, 0x48, 0x46, 0x55}; + public static final byte[] up_left_normal = new byte[]{(byte) 0x99, 0x28, (byte) 0xe5, (byte) 0x90, (byte) 0xdf, (byte) 0xe8, 0x21, 0x48, 0x5f, 0x41, 0x4f, (byte) 0xbb, 0x63, 0x3d, 0x5c, 0x4e}; + public static final byte[] up_right_normal = new byte[]{0x0f, 0x2c, (byte) 0xe5, 0x66, 0x62, (byte) 0xd4, (byte) 0xfd, (byte) 0x9d, 0x32, (byte) 0xa4, 0x4f, 0x10, 0x2b, (byte) 0xf2, 0x0a, (byte) 0xa7}; + public static final byte[] down_left_normal = new byte[]{(byte) 0x98, (byte) 0xce, (byte) 0x98, 0x1d, 0x58, (byte) 0xd1, 0x15, (byte) 0xaf, (byte) 0xe1, 0x19, 0x60, (byte) 0xbf, 0x46, 0x13, (byte) 0x92, 0x5c}; + public static final byte[] down_right_normal = new byte[]{(byte) 0xf2, 0x52, 0x0f, (byte) 0xba, 0x31, 0x44, (byte) 0xfb, 0x11, 0x46, (byte) 0x8f, (byte) 0xe0, (byte) 0x80, (byte) 0xc6, (byte) 0xc2, (byte) 0xc2, 0x3c}; + + public static final byte[] idle_lights = new byte[]{0x39, (byte) 0xb5, 0x3b, (byte) 0x9b, (byte) 0xb7, (byte) 0xa0, (byte) 0xe0, (byte) 0xd6, 0x52, 0x54, (byte) 0xf9, (byte) 0xea, (byte) 0x84, 0x5d, (byte) 0xde, (byte) 0xee}; + public static final byte[] up_lights = new byte[]{0x2e, (byte) 0x90, (byte) 0xb1, (byte) 0xd3, 0x6b, (byte) 0x8f, (byte) 0xad, 0x5f, (byte) 0x96, 0x7d, (byte) 0xb3, 0x2e, 0x6e, 0x6c, (byte) 0xfd, 0x6e}; + public static final byte[] down_lights = new byte[]{0x2b, 0x41, 0x0a, 0x47, (byte) 0xce, 0x03, 0x59, (byte) 0xe0, 0x4d, 0x75, (byte) 0xfd, 0x0e, (byte) 0xe3, (byte) 0x9f, (byte) 0xe1, (byte) 0xbd}; + public static final byte[] up_right_lights = new byte[]{0x06, 0x4e, 0x40, (byte) 0x8c, 0x32, 0x52, (byte) 0xfb, 0x32, 0x7b, (byte) 0xd9, (byte) 0xfc, 0x54, (byte) 0x9b, 0x53, (byte) 0xee, 0x3e}; + public static final byte[] up_left_lights = new byte[]{0x23, 0x77, 0x1d, (byte) 0xc2, 0x2a, 0x73, (byte) 0x89, (byte) 0x99, 0x2f, 0x53, (byte) 0xac, 0x59, 0x38, (byte) 0xc1, 0x78, (byte) 0x91}; + public static final byte[] down_right_lights = new byte[]{(byte) 0xbb, (byte) 0xd6, 0x2a, (byte) 0xac, 0x32, (byte) 0x8c, (byte) 0x9e, 0x31, 0x65, 0x33, (byte) 0xc8, 0x0e, (byte) 0x9a, (byte) 0xcb, (byte) 0xf6, 0x4b}; + public static final byte[] down_left_lights = new byte[]{0x6e, (byte) 0xaa, (byte) 0xf0, (byte) 0xc0, (byte) 0x85, (byte) 0x8f, 0x14, 0x77, 0x6f, (byte) 0xd8, (byte) 0xf0, 0x71, 0x39, (byte) 0xa0, 0x08, (byte) 0xf2}; + + public static final byte[] idle_turbo = new byte[]{(byte) 0xd9, (byte) 0x94, (byte) 0xb3, (byte) 0x71, (byte) 0xc3, (byte) 0xbe, (byte) 0x2a, (byte) 0x9a, (byte) 0x9d, (byte) 0x04, (byte) 0x88, (byte) 0xa1, (byte) 0x04, (byte) 0x4b, (byte) 0x7f, (byte) 0x67}; + public static final byte[] up_turbo = new byte[]{(byte) 0xe6, (byte) 0x55, (byte) 0x67, (byte) 0xda, (byte) 0x8e, (byte) 0x6c, (byte) 0x56, (byte) 0x0d, (byte) 0x09, (byte) 0xd3, (byte) 0x73, (byte) 0x3a, (byte) 0x7f, (byte) 0x47, (byte) 0xff, (byte) 0x06}; + public static final byte[] down_turbo = new byte[]{(byte) 0xce, (byte) 0xc2, (byte) 0xff, (byte) 0x1d, (byte) 0x7a, (byte) 0xcc, (byte) 0x16, (byte) 0x3c, (byte) 0xd1, (byte) 0x3b, (byte) 0x7e, (byte) 0x61, (byte) 0x53, (byte) 0xad, (byte) 0x5c, (byte) 0x45}; + public static final byte[] up_right_turbo = new byte[]{(byte) 0xfb, (byte) 0x97, (byte) 0x6f, (byte) 0xba, (byte) 0x04, (byte) 0xaf, (byte) 0x87, (byte) 0x02, (byte) 0x22, (byte) 0x26, (byte) 0xec, (byte) 0x50, (byte) 0xae, (byte) 0x82, (byte) 0xf8, (byte) 0xc4}; + public static final byte[] up_left_turbo = new byte[]{0x59, (byte) 0x23, (byte) 0x81, (byte) 0xc9, (byte) 0x43, (byte) 0xa4, (byte) 0x17, (byte) 0xca, (byte) 0x1b, (byte) 0xc3, (byte) 0xb5, (byte) 0x94, (byte) 0x00, (byte) 0xe0, (byte) 0xfc, (byte) 0x12}; + public static final byte[] down_right_turbo = new byte[]{(byte) 0x80, (byte) 0xdf, (byte) 0xb2, (byte) 0x16, (byte) 0x5f, (byte) 0x32, (byte) 0x60, (byte) 0xf1, (byte) 0xd9, (byte) 0x83, (byte) 0x77, (byte) 0x50, (byte) 0xf4, (byte) 0x3a, (byte) 0x43, (byte) 0xda}; + public static final byte[] down_left_turbo = new byte[]{(byte) 0xd5, (byte) 0x4a, (byte) 0xd5, (byte) 0x58, (byte) 0x57, (byte) 0xd3, (byte) 0x27, (byte) 0x74, (byte) 0x5f, (byte) 0x14, (byte) 0x1d, (byte) 0xd0, (byte) 0x0d, (byte) 0x67, (byte) 0x15, (byte) 0x95}; + + public static final byte[] idle_turbo_lights = new byte[]{(byte) 0x1a, (byte) 0x01, (byte) 0x1e, (byte) 0x9e, (byte) 0x6e, (byte) 0xfc, (byte) 0xce, (byte) 0x22, (byte) 0xbe, (byte) 0x8e, (byte) 0xb7, (byte) 0xff, (byte) 0xb6, (byte) 0x29, (byte) 0xfa, (byte) 0x75}; + public static final byte[] up_turbo_lights = new byte[]{(byte) 0xd6, (byte) 0xc7, (byte) 0x63, (byte) 0x8e, (byte) 0x0e, (byte) 0x9a, (byte) 0x80, (byte) 0xbe, (byte) 0x60, (byte) 0x88, (byte) 0xfc, (byte) 0x44, (byte) 0x43, (byte) 0x07, (byte) 0xa1, (byte) 0x78}; + public static final byte[] down_turbo_lights = new byte[]{(byte) 0x0d, (byte) 0x4f, (byte) 0xf8, (byte) 0x23, (byte) 0xac, (byte) 0xf9, (byte) 0xb7, (byte) 0xef, (byte) 0x1c, (byte) 0x26, (byte) 0xd4, (byte) 0xb4, (byte) 0x56, (byte) 0x51, (byte) 0x59, (byte) 0x52}; + public static final byte[] up_right_turbo_lights = new byte[]{(byte) 0x83, (byte) 0xe6, (byte) 0x59, (byte) 0x42, (byte) 0x3d, (byte) 0x4b, (byte) 0x78, (byte) 0x48, (byte) 0x14, (byte) 0x5d, (byte) 0x86, (byte) 0xa1, (byte) 0x7b, (byte) 0x54, (byte) 0x7e, (byte) 0x58}; + public static final byte[] up_left_turbo_lights = new byte[]{(byte) 0xd1, (byte) 0x7f, (byte) 0x6c, (byte) 0x5e, (byte) 0xe6, (byte) 0xba, (byte) 0x81, (byte) 0xe2, (byte) 0xb5, (byte) 0x80, (byte) 0x90, (byte) 0xa3, (byte) 0xcc, (byte) 0x76, (byte) 0x37, (byte) 0x9f}; + public static final byte[] down_right_turbo_lights = new byte[]{(byte) 0xd4, (byte) 0x5d, (byte) 0xc9, (byte) 0x8f, (byte) 0x76, (byte) 0x58, (byte) 0xf9, (byte) 0x02, (byte) 0x0f, (byte) 0x93, (byte) 0xa0, (byte) 0xfd, (byte) 0x80, (byte) 0xe2, (byte) 0x2d, (byte) 0x45}; + public static final byte[] down_left_turbo_lights = new byte[]{(byte) 0x96, (byte) 0xc0, (byte) 0x35, (byte) 0x77, (byte) 0xe9, (byte) 0xcd, (byte) 0xa8, (byte) 0xb9, (byte) 0x70, (byte) 0x21, (byte) 0x5f, (byte) 0xaf, (byte) 0x35, (byte) 0x00, (byte) 0x3b, (byte) 0x74}; + public static final byte[] left_data = new byte[]{0x51, 0x38, 0x21, 0x12, 0x13, 0x5c, (byte) 0xcc, (byte) 0xdb, (byte) 0x46, (byte) 0xcf, (byte) 0x89, 0x21, (byte) 0xb7, 0x05, 0x49, (byte) 0x9a}; public static final byte[] right_data = new byte[]{0x1b, 0x57, 0x69, (byte) 0xcd, (byte) 0xf1, 0x3e, (byte) 0x8a, (byte) 0xb6, 0x27, 0x08, 0x0f, (byte) 0xf3, (byte) 0xce, (byte) 0xfc, 0x3b, (byte) 0xc0}; - public static final byte[] up_left_data = new byte[]{(byte) 0x99, 0x28, (byte) 0xe5, (byte) 0x90, (byte) 0xdf, (byte) 0xe8, 0x21, 0x48, 0x5f, 0x41, 0x4f, (byte) 0xbb, 0x63, 0x3d, 0x5c, 0x4e}; - public static final byte[] up_right_data = new byte[]{0x0f, 0x2c, (byte) 0xe5, 0x66, 0x62, (byte) 0xd4, (byte) 0xfd, (byte) 0x9d, 0x32, (byte) 0xa4, 0x4f, 0x10, 0x2b, (byte) 0xf2, 0x0a, (byte) 0xa7}; - public static final byte[] down_left_data = new byte[]{(byte) 0x98, (byte) 0xce, (byte) 0x98, 0x1d, 0x58, (byte) 0xd1, 0x15, (byte) 0xaf, (byte) 0xe1, 0x19, 0x60, (byte) 0xbf, 0x46, 0x13, (byte) 0x92, 0x5c}; - public static final byte[] down_right_data = new byte[]{(byte) 0xf2, 0x52, 0x0f, (byte) 0xba, 0x31, 0x44, (byte) 0xfb, 0x11, 0x46, (byte) 0x8f, (byte) 0xe0, (byte) 0x80, (byte) 0xc6, (byte) 0xc2, (byte) 0xc2, 0x3c}; + public enum SpeedModes { + NORMAL, TURBO, LIGHTS, TURBO_LIGHTS + } + + public enum Directions { + UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT + } + + public static byte[] get_directions_data(SpeedModes speedModes, Directions directions) { + switch (speedModes) { + case NORMAL: + switch (directions) { + case UP: + return up_normal; + case UP_LEFT: + return up_left_normal; + case UP_RIGHT: + return up_right_normal; + case DOWN: + return down_normal; + case DOWN_LEFT: + return down_left_normal; + case DOWN_RIGHT: + return down_right_normal; + } + return idle_normal; + case TURBO: + switch (directions) { + case UP: + return up_turbo; + case UP_LEFT: + return up_left_turbo; + case UP_RIGHT: + return up_right_turbo; + case DOWN: + return down_turbo; + case DOWN_LEFT: + return down_left_turbo; + case DOWN_RIGHT: + return down_right_turbo; + } + return idle_turbo; + case LIGHTS: + switch (directions) { + case UP: + return up_lights; + case UP_LEFT: + return up_left_lights; + case UP_RIGHT: + return up_right_lights; + case DOWN: + return down_lights; + case DOWN_LEFT: + return down_left_lights; + case DOWN_RIGHT: + return down_right_lights; + } + return idle_lights; + case TURBO_LIGHTS: + switch (directions) { + case UP: + return up_turbo_lights; + case UP_LEFT: + return up_left_turbo_lights; + case UP_RIGHT: + return up_right_turbo_lights; + case DOWN: + return down_turbo_lights; + case DOWN_LEFT: + return down_left_turbo_lights; + case DOWN_RIGHT: + return down_right_turbo_lights; + } + return idle_turbo_lights; + } + return idle_lights; + } } + diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java index ae25a0573..ffc7d8d38 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/supercars/SuperCarsSupport.java @@ -28,12 +28,12 @@ import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction; -import nodomain.freeyourgadget.gadgetbridge.service.devices.fitpro.FitProDeviceSupport; public class SuperCarsSupport extends AbstractBTLEDeviceSupport { private static final Logger LOG = LoggerFactory.getLogger(SuperCarsSupport.class); public static final String COMMAND_DRIVE_CONTROL = "nodomain.freeyourgadget.gadgetbridge.supercars.command.DRIVE_CONTROL"; public static final String EXTRA_DIRECTION = "EXTRA_DIRECTION"; + public static final String EXTRA_MODE = "EXTRA_MODE"; public SuperCarsSupport() { super(LOG); @@ -50,7 +50,7 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { broadcastManager.registerReceiver(commandReceiver, filter); builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext())); - LOG.debug("name " + gbDevice.getName()); + LOG.debug("name " + gbDevice.getName()); return builder; } @@ -59,7 +59,9 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(COMMAND_DRIVE_CONTROL)) { SuperCarsSupport.this.setDirection( - intent.getStringExtra(EXTRA_DIRECTION) + (SuperCarsConstants.SpeedModes) intent.getSerializableExtra(EXTRA_MODE), + (SuperCarsConstants.Directions) intent.getSerializableExtra(EXTRA_DIRECTION) + ); } } @@ -219,36 +221,13 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport { return false; } - private void setDirection(String direction) { - byte[] command = SuperCarsConstants.idle_data; + private void setDirection(SuperCarsConstants.SpeedModes speedModes, SuperCarsConstants.Directions direction) { - switch (direction) { - case "left_up": - command = SuperCarsConstants.up_left_data; - break; - case "center_up": - command = SuperCarsConstants.up_data; - break; - case "right_up": - command = SuperCarsConstants.up_right_data; - break; - case "left_down": - command = SuperCarsConstants.down_left_data; - break; - case "center_down": - command = SuperCarsConstants.down_data; - break; - case "right_down": - command = SuperCarsConstants.down_right_data; - break; - default: - command = SuperCarsConstants.idle_data; - } - TransactionBuilder builder = new TransactionBuilder("test"); + byte[] command = SuperCarsConstants.get_directions_data(speedModes, direction); + TransactionBuilder builder = new TransactionBuilder("setDirections"); BluetoothGattCharacteristic writeCharacteristic = getCharacteristic(SuperCarsConstants.CHARACTERISTIC_UUID_FFF1); builder.write(writeCharacteristic, command); builder.queue(getQueue()); - } @Override diff --git a/app/src/main/res/layout/activity_supercars_control.xml b/app/src/main/res/layout/activity_supercars_control.xml index f9e10c054..4e090ee80 100644 --- a/app/src/main/res/layout/activity_supercars_control.xml +++ b/app/src/main/res/layout/activity_supercars_control.xml @@ -1,74 +1,41 @@ + android:orientation="vertical" + tools:ignore="MissingConstraints"> + + -