1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-09-27 16:56:57 +02:00

SuperCars: fix periodicDataSender, add tricks

This commit is contained in:
vanous 2022-10-02 23:21:14 +02:00
parent d0b1e2a23c
commit 244b758743
8 changed files with 239 additions and 10 deletions

View File

@ -29,8 +29,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
public class
BatteryInfoActivity extends AbstractGBActivity {
public class BatteryInfoActivity extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(BatteryInfoActivity.class);
GBDevice gbDevice;
private int timeFrom;

View File

@ -6,8 +6,10 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -24,17 +26,21 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
private static final Logger LOG = LoggerFactory.getLogger(ControlActivity.class);
LocalBroadcastManager localBroadcastManager;
CountDownTimer periodicDataSenderRunner;
boolean periodicDataSenderRunnerIsRunning = false;
private GBDevice device;
TextView batteryPercentage;
boolean lights = false;
boolean blinking = false;
boolean turbo = false;
int stepCounter = 0;
SuperCarsConstants.Direction direction = SuperCarsConstants.Direction.CENTER;
SuperCarsConstants.Movement movement = SuperCarsConstants.Movement.IDLE;
SuperCarsConstants.Speed speed = SuperCarsConstants.Speed.NORMAL;
SuperCarsConstants.Light light = SuperCarsConstants.Light.OFF;
SuperCarsConstants.Tricks tricks = SuperCarsConstants.Tricks.OFF;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -97,9 +103,46 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
}
});
ImageButton trick1 = findViewById(R.id.trick1);
ImageButton trick2 = findViewById(R.id.trick2);
ImageButton trick3 = findViewById(R.id.trick3);
ImageButton trick4 = findViewById(R.id.trick4);
trick1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tricks = SuperCarsConstants.Tricks.CIRCLE_LEFT;
stepCounter = 0;
}
});
trick2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tricks = SuperCarsConstants.Tricks.CIRCLE_RIGHT;
stepCounter = 0;
}
});
trick3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tricks = SuperCarsConstants.Tricks.U_TURN_LEFT;
stepCounter = 0;
}
});
trick4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tricks = SuperCarsConstants.Tricks.U_TURN_RIGHT;
stepCounter = 0;
}
});
//when this activity is open, data is sent continuously every 100ms
periodicDataSender();
periodicDataSenderRunner.start();
if (!periodicDataSenderRunnerIsRunning) {
periodicDataSender();
}
}
private void setLights() {
@ -109,6 +152,12 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
} else {
light = SuperCarsConstants.Light.OFF;
}
} else if (blinking) {
if (light.equals(SuperCarsConstants.Light.ON)) {
light = SuperCarsConstants.Light.OFF;
} else {
light = SuperCarsConstants.Light.ON;
}
}
}
@ -125,12 +174,20 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
periodicDataSenderRunner = new CountDownTimer(Long.MAX_VALUE, 100) {
public void onTick(long millisUntilFinished) {
periodicDataSenderRunnerIsRunning = true;
setLights();
if (blinking) {
if (light.equals(SuperCarsConstants.Light.ON)) {
light = SuperCarsConstants.Light.OFF;
if (tricks != SuperCarsConstants.Tricks.OFF) {
Enum[][] trick_steps = SuperCarsConstants.get_trick(tricks);
int steps = trick_steps.length;
if (stepCounter < steps) {
Enum[] step = trick_steps[stepCounter];
movement = (SuperCarsConstants.Movement) step[0];
direction = (SuperCarsConstants.Direction) step[1];
stepCounter++;
} else {
light = SuperCarsConstants.Light.ON;
tricks = SuperCarsConstants.Tricks.OFF;
stepCounter = 0;
}
}
@ -155,6 +212,9 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
@Override
public void onJoystickMoved(float xPercent, float yPercent, int id) {
tricks = SuperCarsConstants.Tricks.OFF;
stepCounter = 0;
if (yPercent < 0.2 && yPercent != 0) {
movement = SuperCarsConstants.Movement.UP;
} else if (yPercent > 0.2) {
@ -182,19 +242,23 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
protected void onDestroy() {
super.onDestroy();
periodicDataSenderRunner.cancel();
periodicDataSenderRunnerIsRunning = false;
LocalBroadcastManager.getInstance(this).unregisterReceiver(commandReceiver);
}
@Override
protected void onPostResume() {
super.onPostResume();
periodicDataSenderRunner.start();
if (!periodicDataSenderRunnerIsRunning) {
periodicDataSenderRunner.start();
}
}
@Override
protected void onPause() {
super.onPause();
periodicDataSenderRunner.cancel();
periodicDataSenderRunnerIsRunning = false;
}
BroadcastReceiver commandReceiver = new BroadcastReceiver() {

View File

@ -32,5 +32,54 @@ public class SuperCarsConstants {
public enum Direction {
LEFT, RIGHT, CENTER
}
public enum Tricks {
OFF, CIRCLE_RIGHT, CIRCLE_LEFT, U_TURN_LEFT, U_TURN_RIGHT
}
static Enum[] fwd_r = {Movement.UP, Direction.RIGHT};
static Enum[] fwd_l = {Movement.UP, Direction.LEFT};
static Enum[] stop = {Movement.IDLE, Direction.CENTER};
public static final Enum[][] tricks_circle_right = {
fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r,
fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r,
fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r,
stop
};
public static final Enum[][] tricks_circle_left = {
fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l,
fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l,
fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l,
stop
};
public static final Enum[][] tricks_u_turn_right = {
fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r,
fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r, fwd_r,
stop
};
public static final Enum[][] tricks_u_turn_left = {
fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l,
fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l, fwd_l,
stop
};
public static final Enum[][] get_trick(Tricks trick) {
switch (trick) {
case CIRCLE_RIGHT:
return tricks_circle_right;
case CIRCLE_LEFT:
return tricks_circle_left;
case U_TURN_LEFT:
return tricks_u_turn_left;
case U_TURN_RIGHT:
return tricks_u_turn_right;
}
return tricks_circle_right;
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M6,13c0,-1.65 0.67,-3.15 1.76,-4.24L6.34,7.34C4.9,8.79 4,10.79 4,13c0,4.08 3.05,7.44 7,7.93v-2.02C8.17,18.43 6,15.97 6,13zM20,13c0,-4.42 -3.58,-8 -8,-8c-0.06,0 -0.12,0.01 -0.18,0.01l1.09,-1.09L11.5,2.5L8,6l3.5,3.5l1.41,-1.41l-1.08,-1.08C11.89,7.01 11.95,7 12,7c3.31,0 6,2.69 6,6c0,2.97 -2.17,5.43 -5,5.91v2.02C16.95,20.44 20,17.08 20,13z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M18,13C18,11.35 17.33,9.85 16.24,8.76L17.66,7.34C19.1,8.79 20,10.79 20,13c-0,4.08 -3.05,7.44 -7,7.93L13,18.91C15.83,18.43 18,15.97 18,13ZM4,13C4,8.58 7.58,5 12,5 12.06,5 12.12,5.01 12.18,5.01L11.09,3.92 12.5,2.5 16,6 12.5,9.5 11.09,8.09 12.17,7.01C12.11,7.01 12.05,7 12,7c-3.31,0 -6,2.69 -6,6 -0,2.97 2.17,5.43 5,5.91l-0,2.02c-3.95,-0.49 -7,-3.85 -7,-7.93z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M18,9v12h-2V9c0,-2.21 -1.79,-4 -4,-4S8,6.79 8,9v4.17l1.59,-1.59L11,13l-4,4l-4,-4l1.41,-1.41L6,13.17V9c0,-3.31 2.69,-6 6,-6S18,5.69 18,9z"/>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M6,9v12h2V9c0,-2.21 1.79,-4 4,-4s4,1.79 4,4v4.17l-1.59,-1.59L13,13l4,4l4,-4l-1.41,-1.41L18,13.17V9c0,-3.31 -2.69,-6 -6,-6S6,5.69 6,9z"/>
</vector>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -60,6 +61,82 @@
tools:ignore="TouchTargetSizeCheck" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/trick1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:background="@color/accent"
android:minHeight="64dp"
app:srcCompat="@drawable/ic_arrow_circle_left" />
<ImageButton
android:id="@+id/trick2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:background="@color/accent"
android:minHeight="64dp"
app:srcCompat="@drawable/ic_arrow_circle_right" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/trick3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:background="@color/accent"
android:minHeight="64dp"
app:srcCompat="@drawable/ic_u_turn_left" />
<ImageButton
android:id="@+id/trick4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:background="@color/accent"
android:minHeight="64dp"
app:srcCompat="@drawable/ic_u_turn_right" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>