SuperCars: add blinking function, fine-tune driving

This commit is contained in:
vanous 2022-10-02 16:46:13 +02:00
parent 51b7f28a8b
commit d6383b5b6d
4 changed files with 117 additions and 17 deletions

View File

@ -1,10 +1,14 @@
package nodomain.freeyourgadget.gadgetbridge.devices.supercars; package nodomain.freeyourgadget.gadgetbridge.devices.supercars;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle; import android.os.Bundle;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.TextView;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -13,12 +17,14 @@ import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity; import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.devices.supercars.SuperCarsSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.supercars.SuperCarsSupport;
public class ControlActivity extends AbstractGBActivity implements JoystickView.JoystickListener { public class ControlActivity extends AbstractGBActivity implements JoystickView.JoystickListener {
private static final Logger LOG = LoggerFactory.getLogger(ControlActivity.class); private static final Logger LOG = LoggerFactory.getLogger(ControlActivity.class);
LocalBroadcastManager localBroadcastManager; LocalBroadcastManager localBroadcastManager;
boolean lights = false; boolean lights = false;
boolean blinking = false;
boolean turbo = false; boolean turbo = false;
CountDownTimer periodicDataSenderRunner; CountDownTimer periodicDataSenderRunner;
@ -26,14 +32,32 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
SuperCarsConstants.Movement movement = SuperCarsConstants.Movement.IDLE; SuperCarsConstants.Movement movement = SuperCarsConstants.Movement.IDLE;
SuperCarsConstants.Speed speed = SuperCarsConstants.Speed.NORMAL; SuperCarsConstants.Speed speed = SuperCarsConstants.Speed.NORMAL;
SuperCarsConstants.Light light = SuperCarsConstants.Light.OFF; SuperCarsConstants.Light light = SuperCarsConstants.Light.OFF;
public GBDevice device;
TextView batteryPercentage;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supercars_control); setContentView(R.layout.activity_supercars_control);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
device = bundle.getParcelable(GBDevice.EXTRA_DEVICE);
} else {
throw new IllegalArgumentException("Must provide a device when invoking this activity");
}
CheckBox turboMode = findViewById(R.id.turboMode); CheckBox turboMode = findViewById(R.id.turboMode);
CheckBox lightsOn = findViewById(R.id.lightsOn); CheckBox lightsOn = findViewById(R.id.lightsOn);
CheckBox lightsBlinking = findViewById(R.id.lightsBlinking);
batteryPercentage = findViewById(R.id.battery_percentage_label);
setBatteryLabel();
localBroadcastManager = LocalBroadcastManager.getInstance(ControlActivity.this);
IntentFilter filter = new IntentFilter();
filter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(commandReceiver, filter);
turboMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { turboMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
@ -46,6 +70,24 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
lights = isChecked; lights = isChecked;
setLights();
}
});
lightsBlinking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (lightsOn.isChecked()) {
lightsOn.setChecked(false);
lights = false;
}
}
lightsOn.setEnabled(!isChecked);
blinking = isChecked;
setLights();
} }
}); });
@ -54,14 +96,39 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
periodicDataSenderRunner.start(); periodicDataSenderRunner.start();
} }
private void setLights() {
if (!blinking) {
if (lights) {
light = SuperCarsConstants.Light.ON;
} else {
light = SuperCarsConstants.Light.OFF;
}
}
}
private void setBatteryLabel() {
String level = device.getBatteryLevel() > 0 ? String.format("%1s%%", device.getBatteryLevel()) : device.getName();
//batteryPercentage.setText(level); //the device seems to be cached, thus the value is old...!
}
private void sendLocalBroadcast(Intent intent) { private void sendLocalBroadcast(Intent intent) {
localBroadcastManager.sendBroadcast(intent); localBroadcastManager.sendBroadcast(intent);
} }
public void periodicDataSender() { public void periodicDataSender() {
periodicDataSenderRunner = new CountDownTimer(Long.MAX_VALUE, 200) { periodicDataSenderRunner = new CountDownTimer(Long.MAX_VALUE, 100) {
public void onTick(long millisUntilFinished) { public void onTick(long millisUntilFinished) {
if (blinking) {
if (light.equals(SuperCarsConstants.Light.ON)) {
light = SuperCarsConstants.Light.OFF;
} else {
light = SuperCarsConstants.Light.ON;
}
}
create_intent_with_data(); create_intent_with_data();
} }
@ -83,9 +150,9 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
@Override @Override
public void onJoystickMoved(float xPercent, float yPercent, int id) { public void onJoystickMoved(float xPercent, float yPercent, int id) {
if (yPercent < 0) { if (yPercent < 0.2 && yPercent != 0) {
movement = SuperCarsConstants.Movement.UP; movement = SuperCarsConstants.Movement.UP;
} else if (yPercent > 0) { } else if (yPercent > 0.2) {
movement = SuperCarsConstants.Movement.DOWN; movement = SuperCarsConstants.Movement.DOWN;
} else { } else {
movement = SuperCarsConstants.Movement.IDLE; movement = SuperCarsConstants.Movement.IDLE;
@ -99,11 +166,6 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
direction = SuperCarsConstants.Direction.CENTER; direction = SuperCarsConstants.Direction.CENTER;
} }
if (lights) {
light = SuperCarsConstants.Light.ON;
} else {
light = SuperCarsConstants.Light.OFF;
}
if (turbo) { if (turbo) {
speed = SuperCarsConstants.Speed.TURBO; speed = SuperCarsConstants.Speed.TURBO;
} else { } else {
@ -115,6 +177,7 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
periodicDataSenderRunner.cancel(); periodicDataSenderRunner.cancel();
LocalBroadcastManager.getInstance(this).unregisterReceiver(commandReceiver);
} }
@Override @Override
@ -128,4 +191,15 @@ public class ControlActivity extends AbstractGBActivity implements JoystickView.
super.onPause(); super.onPause();
periodicDataSenderRunner.cancel(); periodicDataSenderRunner.cancel();
} }
BroadcastReceiver commandReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
LOG.debug("device receiver received " + intent.getAction());
if (intent.getAction().equals(GBDevice.ACTION_DEVICE_CHANGED)) {
setBatteryLabel();
}
}
};
} }

View File

@ -67,7 +67,7 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(COMMAND_DRIVE_CONTROL)) { if (intent.getAction().equals(COMMAND_DRIVE_CONTROL)) {
send_data( queueDataToBLE(
(SuperCarsConstants.Speed) intent.getSerializableExtra(EXTRA_SPEED), (SuperCarsConstants.Speed) intent.getSerializableExtra(EXTRA_SPEED),
(SuperCarsConstants.Movement) intent.getSerializableExtra(EXTRA_MOVEMENT), (SuperCarsConstants.Movement) intent.getSerializableExtra(EXTRA_MOVEMENT),
(SuperCarsConstants.Light) intent.getSerializableExtra(EXTRA_LIGHT), (SuperCarsConstants.Light) intent.getSerializableExtra(EXTRA_LIGHT),
@ -112,7 +112,8 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport {
if (decoded_data.length == 16) { if (decoded_data.length == 16) {
GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo();
batteryEvent.state = BatteryState.BATTERY_NORMAL; batteryEvent.state = BatteryState.BATTERY_NORMAL;
batteryEvent.level = decoded_data[4]; int level = decoded_data[4];
batteryEvent.level = level;
evaluateGBDeviceEvent(batteryEvent); evaluateGBDeviceEvent(batteryEvent);
} }
} }
@ -294,10 +295,10 @@ public class SuperCarsSupport extends AbstractBTLEDeviceSupport {
return false; return false;
} }
private void send_data(SuperCarsConstants.Speed speed, private void queueDataToBLE(SuperCarsConstants.Speed speed,
SuperCarsConstants.Movement movement, SuperCarsConstants.Movement movement,
SuperCarsConstants.Light light, SuperCarsConstants.Light light,
SuperCarsConstants.Direction direction) { SuperCarsConstants.Direction direction) {
byte[] command = craft_packet(speed, direction, movement, light); byte[] command = craft_packet(speed, direction, movement, light);
TransactionBuilder builder = new TransactionBuilder("send data"); TransactionBuilder builder = new TransactionBuilder("send data");

View File

@ -10,6 +10,17 @@
android:orientation="vertical" android:orientation="vertical"
tools:ignore="MissingConstraints"> tools:ignore="MissingConstraints">
<TextView
android:id="@+id/battery_percentage_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:foregroundGravity="center_horizontal"
android:gravity="center_horizontal"
android:text=""
android:textSize="48sp" />
<nodomain.freeyourgadget.gadgetbridge.devices.supercars.JoystickView <nodomain.freeyourgadget.gadgetbridge.devices.supercars.JoystickView
android:id="@+id/joystickLeft" android:id="@+id/joystickLeft"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -26,14 +37,27 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:text="@string/supercars_turbo_speed_label" /> android:height="48dp"
android:text="@string/supercars_turbo_speed_label"
tools:ignore="TouchTargetSizeCheck" />
<CheckBox <CheckBox
android:id="@+id/lightsOn" android:id="@+id/lightsOn"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:text="@string/supercars_lights_label" /> android:height="48dp"
android:text="@string/supercars_lights_label"
tools:ignore="TouchTargetSizeCheck" />
<CheckBox
android:id="@+id/lightsBlinking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="48dp"
android:text="@string/supercars_lights_blinking_label"
tools:ignore="TouchTargetSizeCheck" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -1850,4 +1850,5 @@
<string name="devicetype_super_cars">Shell Racing</string> <string name="devicetype_super_cars">Shell Racing</string>
<string name="supercars_turbo_speed_label">Turbo Speed</string> <string name="supercars_turbo_speed_label">Turbo Speed</string>
<string name="supercars_lights_label">Lights</string> <string name="supercars_lights_label">Lights</string>
<string name="supercars_lights_blinking_label">Blinking</string>
</resources> </resources>