Skip to content

Commit

Permalink
v2.1.3
Browse files Browse the repository at this point in the history
- Override Street Mode from app
- New Power assist level values (2% resolition)
- Added Max speed parameters
- Fix Bluetooth setup when device is already paired
  • Loading branch information
mspider65 committed Sep 15, 2020
1 parent 8326d45 commit 875e27f
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 70 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "spider65.ebike.tsdz2_esp32"
minSdkVersion 23
targetSdkVersion 28
versionCode 9
versionName "2.1.2"
versionCode 10
versionName "2.1.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
44 changes: 42 additions & 2 deletions app/src/main/java/spider65/ebike/tsdz2_esp32/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import spider65.ebike.tsdz2_esp32.utils.OnSwipeListener;

import android.util.Log;
import android.view.ContextMenu;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -196,6 +198,7 @@ public boolean onSwipe(Direction direction) {
});
brakeIV = findViewById(R.id.brakeIV);
streetModeIV = findViewById(R.id.streetModeIV);
registerForContextMenu(streetModeIV);

fabButton = findViewById(R.id.fab);
fabButton.setOnClickListener((View) -> {
Expand Down Expand Up @@ -230,6 +233,43 @@ public boolean onSwipe(Direction direction) {
checkBT();
}

@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() != R.id.streetModeIV)
return;
TSDZBTService service = TSDZBTService.getBluetoothService();
if (service == null || service.getConnectionStatus() != TSDZBTService.ConnectionState.CONNECTED)
return;

// create context menu for Street Mode Icon long press
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_street_mode, menu);
menu.setHeaderTitle(getResources().getString(R.string.street_mode));
}

@Override
public boolean onContextItemSelected(MenuItem item){
// manage Street Mode icon context menu selection
TSDZBTService service = TSDZBTService.getBluetoothService();
if (service == null || service.getConnectionStatus() != TSDZBTService.ConnectionState.CONNECTED)
return false;

switch (item.getItemId()) {
case R.id.lcd_master:
service.writeCommand(new byte[] {TSDZConst.CMD_STREET_MODE, TSDZConst.STREET_MODE_LCD_MASTER});
break;
case R.id.force_off:
service.writeCommand(new byte[] {TSDZConst.CMD_STREET_MODE, TSDZConst.STREET_MODE_FORCE_OFF});
break;
case R.id.force_on:
service.writeCommand(new byte[] {TSDZConst.CMD_STREET_MODE, TSDZConst.STREET_MODE_FORCE_ON});
break;
default:
return false;
}
return true;
}

private boolean checkDevice() {
String mac = MyApp.getPreferences().getString(KEY_DEVICE_MAC, null);
if (mac != null) {
Expand Down Expand Up @@ -370,9 +410,9 @@ private void refreshView() {
statusTV.setVisibility(View.INVISIBLE);

if (status.streetMode)
streetModeIV.setVisibility(View.VISIBLE);
streetModeIV.setImageResource(R.mipmap.street_icon_on);
else
streetModeIV.setVisibility(View.INVISIBLE);
streetModeIV.setImageResource(R.mipmap.street_icon_off);

switch (status.ridingMode) {
case OFF_MODE:
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/spider65/ebike/tsdz2_esp32/TSDZConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public interface TSDZConst {
byte CMD_ESP_OTA_STATUS = 0x05;
byte CMD_STM8_OTA_STATUS = 0x06;
byte CMD_ESP32_CONFIG = 0x08;
byte CMD_STREET_MODE = 0x09;

byte STREET_MODE_LCD_MASTER = 0;
byte STREET_MODE_FORCE_OFF = 1;
byte STREET_MODE_FORCE_ON = 2;

// sub commands of CMD_ESP32_CONFIG
byte CONFIG_GET = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ public void onClick(View v) {
okButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopScanning();
if (selectedDevice != null && (selectedDevice.getBondState() != BluetoothDevice.BOND_BONDED)) {
if (!selectedDevice.createBond())
showDialog(getString(R.string.error), getString(R.string.pairing_error),true);
if (selectedDevice != null) {
if (selectedDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
if (!selectedDevice.createBond())
showDialog(getString(R.string.error), getString(R.string.pairing_error), true);
} else {
// device is already bonded
SharedPreferences.Editor editor = MyApp.getPreferences().edit();
editor.putString(KEY_DEVICE_NAME, selectedDeviceString);
editor.putString(KEY_DEVICE_MAC, selectedDevice.getAddress());
editor.apply();
BluetoothSetupActivity.this.showDialog(null, getString(R.string.pairing_done),true);
}
} else
finish();
}
Expand All @@ -105,7 +114,6 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
selectedDeviceString = deviceList.get(position);
selectedDevice = btDeviceList.get(position);
updateDeviceTV();
stopScanning();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,27 @@ private void saveCfg() {
return;
}

if ((val1 = checkRange(binding.powerAssist1ET, 1, 100)) == null) {
showDialog(getString(R.string.power_assist_level_1), getString(R.string.range_error, 1, 100));
if ((val1 = checkRange(binding.powerAssist1ET, 10, 500)) == null) {
showDialog(getString(R.string.power_assist_level_1), getString(R.string.range_error, 10, 500));
return;
}
if ((val2 = checkRange(binding.powerAssist2ET, 1, 100)) == null) {
showDialog(getString(R.string.power_assist_level_2), getString(R.string.range_error, 1, 100));
if ((val2 = checkRange(binding.powerAssist2ET, 10, 500)) == null) {
showDialog(getString(R.string.power_assist_level_2), getString(R.string.range_error, 10, 500));
return;
}
if ((val3 = checkRange(binding.powerAssist3ET, 1, 100)) == null) {
showDialog(getString(R.string.power_assist_level_3), getString(R.string.range_error, 1, 100));
if ((val3 = checkRange(binding.powerAssist3ET, 10, 500)) == null) {
showDialog(getString(R.string.power_assist_level_3), getString(R.string.range_error, 10, 500));
return;
}
if ((val4 = checkRange(binding.powerAssist4ET, 1, 100)) == null) {
showDialog(getString(R.string.power_assist_level_4), getString(R.string.range_error, 1, 100));
if ((val4 = checkRange(binding.powerAssist4ET, 10, 500)) == null) {
showDialog(getString(R.string.power_assist_level_4), getString(R.string.range_error, 10, 500));
return;
}
if (val2>val1 && val3>val2 && val4>val3) {
cfg.ui8_power_assist_level[0] = val1;
cfg.ui8_power_assist_level[1] = val2;
cfg.ui8_power_assist_level[2] = val3;
cfg.ui8_power_assist_level[3] = val4;
cfg.ui8_power_assist_level[0] = val1/2;
cfg.ui8_power_assist_level[1] = val2/2;
cfg.ui8_power_assist_level[2] = val3/2;
cfg.ui8_power_assist_level[3] = val4/2;
} else {
showDialog(getString(R.string.power_mode), getString(R.string.level_error));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,21 @@ private void saveCfg() {
}
cfg.ui16_wheel_perimeter = val;

if ((val = checkRange(binding.maxSpeedET, 10, 60)) == null) {
showDialog(getString(R.string.max_speed), getString(R.string.range_error, 10, 60));
return;
}
cfg.ui8_max_speed = val;

checked = binding.cruiseModeCB.isChecked();
cfg.ui8_cruise_enabled = checked;

if ((val = checkRange(binding.maxStreetSpeedET, 10, 45)) == null) {
showDialog(getString(R.string.max_speed), getString(R.string.range_error, 10, 45));
return;
}
cfg.ui8_street_max_speed = val;

checked = binding.streetPowerCB.isChecked();
if (checked) {
if ((val = checkRange(binding.streetPowerET, 50, 1000)) == null) {
Expand Down
92 changes: 47 additions & 45 deletions app/src/main/java/spider65/ebike/tsdz2_esp32/data/TSDZ_Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public int getValue() {
public int ui8_motor_temperature_max_value_to_limit;
public int ui8_motor_acceleration;
public int ui8_dummy;
public int ui16_dummy;
public int ui8_max_speed;
public int ui8_street_max_speed;
public int ui8_pedal_torque_per_10_bit_ADC_step_x100;
public TempControl temperature_control;
public boolean throttleEnabled;
Expand All @@ -59,7 +60,7 @@ public int getValue() {
public int ui8_li_io_cell_full_bars_x100;
public int ui8_li_io_cell_one_bar_x100;
public int ui8_li_io_cell_empty_x100;
public boolean ui8_street_mode_enabled;
public boolean ui8_dummy2;
public boolean ui8_street_mode_power_limit_enabled;
public boolean ui8_street_mode_throttle_enabled;
public int ui8_street_mode_power_limit_div25;
Expand All @@ -74,43 +75,43 @@ public int getValue() {

/*
#pragma pack(1)
typedef struct _tsdz_cfg
{
volatile uint8_t ui8_motor_type;
volatile uint8_t ui8_motor_temperature_min_value_to_limit;
volatile uint8_t ui8_motor_temperature_max_value_to_limit;
volatile uint8_t ui8_motor_acceleration;
volatile uint8_t ui8_cadence_sensor_mode;
volatile uint16_t ui16_cadence_sensor_pulse_high_percentage_x10;
volatile uint8_t ui8_pedal_torque_per_10_bit_ADC_step_x100;
volatile uint8_t ui8_optional_ADC_function;
volatile uint8_t ui8_assist_without_pedal_rotation_threshold;
volatile uint8_t ui8_lights_configuration;
volatile uint16_t ui16_wheel_perimeter;
volatile uint8_t ui8_cruise_enabled;
volatile uint16_t ui16_battery_voltage_reset_wh_counter_x10;
volatile uint8_t ui8_battery_max_current;
volatile uint8_t ui8_target_max_battery_power_div25;
volatile uint8_t ui8_battery_cells_number;
volatile uint16_t ui16_battery_pack_resistance_x1000;
volatile uint16_t ui16_battery_low_voltage_cut_off_x10;
volatile uint8_t ui8_li_io_cell_overvolt_x100;
volatile uint8_t ui8_li_io_cell_full_bars_x100;
volatile uint8_t ui8_li_io_cell_one_bar_x100;
volatile uint8_t ui8_li_io_cell_empty_x100;
volatile uint8_t ui8_street_mode_enabled;
volatile uint8_t ui8_street_mode_power_limit_enabled;
volatile uint8_t ui8_street_mode_throttle_enabled;
volatile uint8_t ui8_street_mode_power_limit_div25;
volatile uint8_t ui8_street_mode_speed_limit;
volatile uint8_t ui8_esp32_temp_control;
volatile uint8_t ui8_cadence_assist_level[4];
volatile uint8_t ui8_power_assist_level[4];
volatile uint8_t ui8_torque_assist_level[4];
volatile uint8_t ui8_eMTB_assist_level[4];
volatile uint8_t ui8_walk_assist_level[4];
volatile uint8_t ui8_torque_offset_fix;
volatile uint16_t ui16_torque_offset_value;
typedef struct _tsdz_cfg {
volatile uint8_t ui8_motor_inductance_x1048576;
volatile uint8_t ui8_motor_temperature_min_value_to_limit;
volatile uint8_t ui8_motor_temperature_max_value_to_limit;
volatile uint8_t ui8_motor_acceleration;
volatile uint8_t ui8_dummy;
volatile uint8_t ui8_max_speed;
volatile uint8_t ui8_street_max_speed;
volatile uint8_t ui8_pedal_torque_per_10_bit_ADC_step_x100;
volatile uint8_t ui8_optional_ADC_function;
volatile uint8_t ui8_assist_without_pedal_rotation_threshold;
volatile uint8_t ui8_lights_configuration;
volatile uint16_t ui16_wheel_perimeter;
volatile uint8_t ui8_cruise_mode_enabled;
volatile uint16_t ui16_battery_voltage_reset_wh_counter_x10;
volatile uint8_t ui8_battery_max_current;
volatile uint8_t ui8_target_max_battery_power_div25;
volatile uint8_t ui8_battery_cells_number;
volatile uint16_t ui16_battery_pack_resistance_x1000;
volatile uint16_t ui16_battery_low_voltage_cut_off_x10;
volatile uint8_t ui8_li_io_cell_overvolt_x100;
volatile uint8_t ui8_li_io_cell_full_bars_x100;
volatile uint8_t ui8_li_io_cell_one_bar_x100;
volatile uint8_t ui8_li_io_cell_empty_x100;
volatile uint8_t ui8_dummy2;
volatile uint8_t ui8_street_mode_power_limit_enabled;
volatile uint8_t ui8_street_mode_throttle_enabled;
volatile uint8_t ui8_street_mode_power_limit_div25;
volatile uint8_t ui8_street_mode_speed_limit;
volatile uint8_t ui8_esp32_temp_control;
volatile uint8_t ui8_cadence_assist_level[4];
volatile uint8_t ui8_power_assist_level[4];
volatile uint8_t ui8_torque_assist_level[4];
volatile uint8_t ui8_eMTB_assist_sensitivity[4];
volatile uint8_t ui8_walk_assist_level[4];
volatile uint8_t ui8_torque_offset_fix;
volatile uint16_t ui16_torque_offset_value;
} struct_tsdz_cfg;
*/

Expand All @@ -124,7 +125,8 @@ public boolean setData(byte[] data) {
ui8_motor_temperature_max_value_to_limit = (data[2] & 255);
ui8_motor_acceleration = (data[3] & 255);
ui8_dummy = (data[4] & 255 ); // ui8_cadence_sensor_mode
ui16_dummy = (data[5] & 255) + ((data[6] & 255) << 8);
ui8_max_speed = (data[5] & 255);
ui8_street_max_speed = (data[6] & 255);
ui8_pedal_torque_per_10_bit_ADC_step_x100 = (data[7] & 255);
throttleEnabled = (data[8] & 255) == 2;
ui8_assist_without_pedal_rotation_threshold = (data[9] & 255);
Expand All @@ -142,7 +144,7 @@ public boolean setData(byte[] data) {
ui8_li_io_cell_full_bars_x100 = (data[24] & 255) + 200;
ui8_li_io_cell_one_bar_x100 = (data[25] & 255) + 200;
ui8_li_io_cell_empty_x100 = (data[26] & 255) + 200;
ui8_street_mode_enabled = (data[27] & 255) != 0;
ui8_dummy2 = (data[27] & 255) != 0;
ui8_street_mode_power_limit_enabled = (data[28] & 255) != 0;
ui8_street_mode_throttle_enabled = (data[29] & 255) != 0;
ui8_street_mode_power_limit_div25 = (data[30] & 255) * 25;
Expand Down Expand Up @@ -174,9 +176,9 @@ public byte[] toByteArray() {
data[1] = (byte)ui8_motor_temperature_min_value_to_limit;
data[2] = (byte)ui8_motor_temperature_max_value_to_limit;
data[3] = (byte)ui8_motor_acceleration;
data[4] = (byte)ui8_dummy; // ui8_cadence_sensor_mode
data[5] = (byte)ui16_dummy;
data[6] = (byte)(ui16_dummy >>> 8);
data[4] = (byte)ui8_dummy;
data[5] = (byte)ui8_max_speed;
data[6] = (byte)(ui8_street_max_speed);
data[7] = (byte)ui8_pedal_torque_per_10_bit_ADC_step_x100;
data[8] = (byte)(throttleEnabled ? 2:0); // ui8_optional_ADC_function
data[8] = temperature_control == TempControl.tempADC ? (byte)1:data[8]; // ui8_optional_ADC_function
Expand All @@ -198,7 +200,7 @@ public byte[] toByteArray() {
data[24] = (byte)(ui8_li_io_cell_full_bars_x100 - 200);
data[25] = (byte)(ui8_li_io_cell_one_bar_x100 - 200);
data[26] = (byte)(ui8_li_io_cell_empty_x100 - 200);
data[27] = (byte)(ui8_street_mode_enabled? 1:0);
data[27] = (byte)(ui8_dummy2 ? 1:0);
data[28] = (byte)(ui8_street_mode_power_limit_enabled? 1:0);
data[29] = (byte)(ui8_street_mode_throttle_enabled? 1:0);
data[30] = (byte)(ui8_street_mode_power_limit_div25/25);
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/layout/activity_levels_setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:ems="4"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[0])}"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[0]*2)}"
android:inputType="number" />
</LinearLayout>

Expand All @@ -190,7 +190,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:ems="4"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[1])}"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[1]*2)}"
android:inputType="number" />
</LinearLayout>

Expand All @@ -211,7 +211,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:ems="4"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[2])}"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[2]*2)}"
android:inputType="number" />
</LinearLayout>

Expand All @@ -232,7 +232,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:ems="4"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[3])}"
android:text="@{String.valueOf(cfg.ui8_power_assist_level[3]*2)}"
android:inputType="number" />
</LinearLayout>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
android:id="@+id/streetModeIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/street_icon" />
android:src="@mipmap/street_icon_on" />
</LinearLayout>

<LinearLayout
Expand Down
Loading

0 comments on commit 875e27f

Please sign in to comment.