From 899f283a1b3674d38ac3a975ad974ad0a155227a Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 16 Dec 2024 02:00:37 +0100 Subject: [PATCH] Marstek B2500: support battery pass-though, cleanups --- .../DeviceSettingsPreferenceConst.java | 1 + .../DeviceSpecificSettingsFragment.java | 1 + .../MarstekB2500DeviceCoordinator.java | 1 + .../marstek/MarstekB2500DeviceSupport.java | 42 ++++++++++++------- app/src/main/res/values/strings.xml | 24 ++++++----- ...vicesettings_battery_allow_pass_though.xml | 9 ++++ .../devicesettings_battery_discharge_5.xml | 2 +- .../devicesettings_battery_minimum_charge.xml | 1 - 8 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_battery_allow_pass_though.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java index 4668026bd..79596e55c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java @@ -598,4 +598,5 @@ public class DeviceSettingsPreferenceConst { public static final String PREF_BATTERY_DISCHARGE_INTERVALS_SET = "battery_discharge_intervals_set"; public static final String PREF_BATTERY_DISCHARGE_MANAUAL = "battery_discharge_manual"; public static final String PREF_BATTERY_MINIMUM_CHARGE = "battery_minimum_charge"; + public static final String PREF_BATTERY_ALLOW_PASS_THOUGH = "battery_allow_pass_though"; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java index 7d11b87d5..591d1e5ba 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java @@ -884,6 +884,7 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i addPreferenceHandlerFor("lock"); addPreferenceHandlerFor(PREF_BATTERY_MINIMUM_CHARGE); + addPreferenceHandlerFor(PREF_BATTERY_ALLOW_PASS_THOUGH); final Preference dischargeIntervalsSet = findPreference(PREF_BATTERY_DISCHARGE_INTERVALS_SET); if (dischargeIntervalsSet != null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/marstek/MarstekB2500DeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/marstek/MarstekB2500DeviceCoordinator.java index 7d48370a8..1b206de39 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/marstek/MarstekB2500DeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/marstek/MarstekB2500DeviceCoordinator.java @@ -61,6 +61,7 @@ public class MarstekB2500DeviceCoordinator extends AbstractDeviceCoordinator { @Override public int[] getSupportedDeviceSpecificSettings(GBDevice device) { return new int[]{ + R.xml.devicesettings_battery_allow_pass_though, R.xml.devicesettings_battery_minimum_charge, R.xml.devicesettings_battery_discharge_5 }; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/marstek/MarstekB2500DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/marstek/MarstekB2500DeviceSupport.java index 05a369018..36b5c940c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/marstek/MarstekB2500DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/marstek/MarstekB2500DeviceSupport.java @@ -1,5 +1,6 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.marstek; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_BATTERY_ALLOW_PASS_THOUGH; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_BATTERY_DISCHARGE_INTERVALS_SET; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_BATTERY_DISCHARGE_MANAUAL; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_BATTERY_MINIMUM_CHARGE; @@ -45,6 +46,9 @@ public class MarstekB2500DeviceSupport extends AbstractBTLEDeviceSupport { private static final byte[] COMMAND_REBOOT = new byte[]{COMMAND_PREFIX, 0x06, COMMAND, OPCODE_REBOOT, 0x01, 0x72}; private static final byte[] COMMAND_SET_AUTO_DISCHARGE = new byte[]{COMMAND_PREFIX, 0x06, COMMAND, 0x11, 0x00, 0x47}; private static final byte[] COMMAND_SET_POWERMETER_CHANNEL1 = new byte[]{COMMAND_PREFIX, 0x06, COMMAND, 0x2a, 0x00, 0x7c}; + private static final byte[] COMMAND_SET_BATTERY_ALLOW_PASS_THOUGH = new byte[]{COMMAND_PREFIX, 0x06, COMMAND, 0x0d, 0x00, 0x5b}; + private static final byte[] COMMAND_SET_BATTERY_DISALLOW_PASS_THOUGH = new byte[]{COMMAND_PREFIX, 0x06, COMMAND, 0x0d, 0x01, 0x5a}; + private static final Logger LOG = LoggerFactory.getLogger(MarstekB2500DeviceSupport.class); private int firmwareVersion; @@ -97,7 +101,7 @@ public class MarstekB2500DeviceSupport extends AbstractBTLEDeviceSupport { getDevice().setFirmwareVersion2("N/A"); builder.requestMtu(512); builder.notify(getCharacteristic(UUID_CHARACTERISTIC_MAIN), true); - builder.wait(3000); + builder.wait(3500); builder.write(getCharacteristic(UUID_CHARACTERISTIC_MAIN), COMMAND_GET_INFOS1); builder.wait(750); builder.write(getCharacteristic(UUID_CHARACTERISTIC_MAIN), COMMAND_GET_INFOS2); @@ -130,18 +134,26 @@ public class MarstekB2500DeviceSupport extends AbstractBTLEDeviceSupport { @Override public void onSendConfiguration(final String config) { - if (config.equals(PREF_BATTERY_DISCHARGE_INTERVALS_SET)) { - Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress())); - if (devicePrefs.getBoolean(PREF_BATTERY_DISCHARGE_MANAUAL, true)) { - sendCommand("set discharge intervals", encodeDischargeIntervalsFromPreferences()); - } else { - sendCommand("set dynamic discharge", COMMAND_SET_AUTO_DISCHARGE); - sendCommand("set channel auto", COMMAND_SET_POWERMETER_CHANNEL1); - } - return; - } else if (config.equals(PREF_BATTERY_MINIMUM_CHARGE)) { - sendCommand("set minimum charge", encodeMinimumChargeFromPreferences()); - return; + Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress())); + switch (config) { + case PREF_BATTERY_DISCHARGE_INTERVALS_SET: + if (devicePrefs.getBoolean(PREF_BATTERY_DISCHARGE_MANAUAL, true)) { + sendCommand("set discharge intervals", encodeDischargeIntervalsFromPreferences()); + } else { + sendCommand("set dynamic discharge", COMMAND_SET_AUTO_DISCHARGE); + sendCommand("set channel auto", COMMAND_SET_POWERMETER_CHANNEL1); + } + return; + case PREF_BATTERY_MINIMUM_CHARGE: + sendCommand("set minimum charge", encodeMinimumChargeFromPreferences()); + return; + case PREF_BATTERY_ALLOW_PASS_THOUGH: + if (devicePrefs.getBoolean(PREF_BATTERY_ALLOW_PASS_THOUGH, true)) { + sendCommand("set allow pass-though", COMMAND_SET_BATTERY_ALLOW_PASS_THOUGH); + } else { + sendCommand("set disallow pass-though", COMMAND_SET_BATTERY_DISALLOW_PASS_THOUGH); + } + return; } LOG.warn("Unknown config changed: {}", config); @@ -154,7 +166,7 @@ public class MarstekB2500DeviceSupport extends AbstractBTLEDeviceSupport { buf.order(ByteOrder.LITTLE_ENDIAN); buf.position(12); // skip header and unknown firmwareVersion = buf.get(); - boolean charge_before_discharge = buf.get() == 0x01; + boolean battery_allow_passthough = buf.get() != 0x01; boolean manual_discharge_intervals = buf.get() != 0x01; buf.position(buf.position() + 3); // skip unknown byte battery_max_use = buf.get(); @@ -169,7 +181,7 @@ public class MarstekB2500DeviceSupport extends AbstractBTLEDeviceSupport { devicePrefsEdit.putString(PREF_BATTERY_MINIMUM_CHARGE, String.valueOf(battery_minimum_charge)); devicePrefsEdit.putBoolean(PREF_BATTERY_DISCHARGE_MANAUAL, manual_discharge_intervals); - + devicePrefsEdit.putBoolean(PREF_BATTERY_ALLOW_PASS_THOUGH, battery_allow_passthough); devicePrefsEdit.apply(); devicePrefsEdit.commit(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6b2dffa70..e21dfe0e8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2646,8 +2646,8 @@ ANC / Transparency / Off Double Connection Allow the earbuds to connect to two devices at the same time - - + + Adaptive Sound Adjusts the sound according to the ear shape and the environment Standard @@ -2741,8 +2741,8 @@ Adaptive volume control Adaptive ANC Set the strength of the ANC automatically depending on the ambient sound level - - + + Speak-to-chat Turn off noise cancelling automatically when you start talking. Voice Detection Sensitivity @@ -3559,14 +3559,16 @@ Build details copied to clipboard Marstek B2500 Battery Discharge Intervals - Discharge Interval 1 - Discharge Interval 2 - Discharge Interval 3 - Discharge Interval 4 - Discharge Interval 5 + Discharge interval 1 + Discharge interval 2 + Discharge interval 3 + Discharge interval 4 + Discharge interval 5 Power in W when disabled, this assumes intelligent discharge controlled by an external power meter (not supported by Gadgetbridge) - Manual Discharge Intervals + Manual discharge intervals Send configuration below to device - Minimum allowed charge in % + Minimum battery charge in % + When enabled, the battery can be charged while discharging + Allow battery pass-though diff --git a/app/src/main/res/xml/devicesettings_battery_allow_pass_though.xml b/app/src/main/res/xml/devicesettings_battery_allow_pass_though.xml new file mode 100644 index 000000000..791d3f885 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_battery_allow_pass_though.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/devicesettings_battery_discharge_5.xml b/app/src/main/res/xml/devicesettings_battery_discharge_5.xml index 031cf96a7..92a43f108 100644 --- a/app/src/main/res/xml/devicesettings_battery_discharge_5.xml +++ b/app/src/main/res/xml/devicesettings_battery_discharge_5.xml @@ -2,7 +2,7 @@ diff --git a/app/src/main/res/xml/devicesettings_battery_minimum_charge.xml b/app/src/main/res/xml/devicesettings_battery_minimum_charge.xml index a750a1086..8fad45e71 100644 --- a/app/src/main/res/xml/devicesettings_battery_minimum_charge.xml +++ b/app/src/main/res/xml/devicesettings_battery_minimum_charge.xml @@ -2,7 +2,6 @@