Skip to content

Commit

Permalink
Added Riding Mode override from App
Browse files Browse the repository at this point in the history
  • Loading branch information
mspider65 committed Sep 30, 2020
1 parent 36a0016 commit 21aa71e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 57 deletions.
10 changes: 5 additions & 5 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 11
versionName "2.1.4"
versionCode 12
versionName "2.1.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -39,11 +39,11 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "com.google.android.material:material:1.1.0"
implementation "com.google.android.material:material:1.2.1"
implementation group: 'org.nanohttpd', name: 'nanohttpd', version: '2.3.1'
implementation 'com.github.hedzr:android-file-chooser:v1.2.0-final'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
Expand Down
124 changes: 75 additions & 49 deletions app/src/main/java/spider65/ebike/tsdz2_esp32/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ public boolean onSwipe(Direction direction) {
mTitle = toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(R.string.status);

modeLevelTV = findViewById(R.id.modeLevelTV);
statusTV = findViewById(R.id.statusTV);
statusTV.setOnClickListener(v -> {
int val;
Expand Down Expand Up @@ -197,6 +196,8 @@ public boolean onSwipe(Direction direction) {
}
});
brakeIV = findViewById(R.id.brakeIV);
modeLevelTV = findViewById(R.id.modeLevelTV);
registerForContextMenu(modeLevelTV);
streetModeIV = findViewById(R.id.streetModeIV);
registerForContextMenu(streetModeIV);

Expand Down Expand Up @@ -233,54 +234,6 @@ 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) {
final BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
final BluetoothAdapter btAdapter = btManager.getAdapter();
BluetoothDevice selectedDevice = btAdapter.getRemoteDevice(mac);
return selectedDevice.getBondState() == BluetoothDevice.BOND_BONDED;
}
return false;
}

@Override
protected void onResume() {
super.onResume();
Expand Down Expand Up @@ -367,6 +320,68 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

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

MenuInflater inflater;
switch (v.getId()) {
case R.id.streetModeIV:
// create context menu for Street Mode Icon long press
inflater = getMenuInflater();
inflater.inflate(R.menu.menu_street_mode, menu);
menu.setHeaderTitle(getResources().getString(R.string.street_mode));
break;
case R.id.modeLevelTV:
// create context menu for Assist Mode Icon long press
inflater = getMenuInflater();
inflater.inflate(R.menu.menu_assist_mode, menu);
menu.setHeaderTitle(getResources().getString(R.string.assist_mode));
break;
}
}

@Override
public boolean onContextItemSelected(MenuItem item){
TSDZBTService service = TSDZBTService.getBluetoothService();
if (service == null || service.getConnectionStatus() != TSDZBTService.ConnectionState.CONNECTED)
return false;

switch (item.getItemId()) {
// manage Street Mode context menu selection
case R.id.street_lcd_master:
service.writeCommand(new byte[] {TSDZConst.CMD_STREET_MODE, TSDZConst.STREET_MODE_LCD_MASTER});
break;
case R.id.street_force_off:
service.writeCommand(new byte[] {TSDZConst.CMD_STREET_MODE, TSDZConst.STREET_MODE_FORCE_OFF});
break;
case R.id.street_force_on:
service.writeCommand(new byte[] {TSDZConst.CMD_STREET_MODE, TSDZConst.STREET_MODE_FORCE_ON});
break;
// manage Assist Mode context menu selection
case R.id.assist_lcd_master:
service.writeCommand(new byte[] {TSDZConst.CMD_ASSIST_MODE, TSDZConst.ASSIST_MODE_LCD_MASTER});
break;
case R.id.assist_power:
service.writeCommand(new byte[] {TSDZConst.CMD_ASSIST_MODE, TSDZConst.ASSIST_MODE_FORCE_POWER});
break;
case R.id.assist_emtb:
service.writeCommand(new byte[] {TSDZConst.CMD_ASSIST_MODE, TSDZConst.ASSIST_MODE_FORCE_EMTB});
break;
case R.id.assist_torque:
service.writeCommand(new byte[] {TSDZConst.CMD_ASSIST_MODE, TSDZConst.ASSIST_MODE_FORCE_TORQUE});
break;
case R.id.assist_cadence:
service.writeCommand(new byte[] {TSDZConst.CMD_ASSIST_MODE, TSDZConst.ASSIST_MODE_FORCE_CADENCE});
break;
default:
return false;
}
return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
Expand Down Expand Up @@ -397,6 +412,17 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}

private boolean checkDevice() {
String mac = MyApp.getPreferences().getString(KEY_DEVICE_MAC, null);
if (mac != null) {
final BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
final BluetoothAdapter btAdapter = btManager.getAdapter();
BluetoothDevice selectedDevice = btAdapter.getRemoteDevice(mac);
return selectedDevice.getBondState() == BluetoothDevice.BOND_BONDED;
}
return false;
}

private void refreshView() {
if (status.brake)
brakeIV.setVisibility(View.VISIBLE);
Expand Down
7 changes: 7 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 @@ -24,11 +24,18 @@ public interface TSDZConst {
byte CMD_STM8_OTA_STATUS = 0x06;
byte CMD_ESP32_CONFIG = 0x08;
byte CMD_STREET_MODE = 0x09;
byte CMD_ASSIST_MODE = 0x0A;

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

byte ASSIST_MODE_LCD_MASTER = 0;
byte ASSIST_MODE_FORCE_POWER = 1;
byte ASSIST_MODE_FORCE_EMTB = 2;
byte ASSIST_MODE_FORCE_TORQUE = 3;
byte ASSIST_MODE_FORCE_CADENCE = 4;

// sub commands of CMD_ESP32_CONFIG
byte CONFIG_GET = 0;
byte CONFIG_SET = 1;
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/menu/menu_assist_mode.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/assist_lcd_master"
android:title="@string/lcd_master"/>
<item android:id="@+id/assist_power"
android:title="@string/power_mode" />
<item android:id="@+id/assist_emtb"
android:title="@string/eMTB_mode"/>
<item android:id="@+id/assist_torque"
android:title="@string/torque_mode"/>
<item android:id="@+id/assist_cadence"
android:title="@string/cadence_mode"/>
</menu>
6 changes: 3 additions & 3 deletions app/src/main/res/menu/menu_street_mode.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/lcd_master"
<item android:id="@+id/street_lcd_master"
android:title="@string/lcd_master"/>
<item android:id="@+id/force_off"
<item android:id="@+id/street_force_off"
android:title="@string/off" />
<item android:id="@+id/force_on"
<item android:id="@+id/street_force_on"
android:title="@string/on"/>
</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
<string name="lcd_master">LCD Master</string>
<string name="max_speed">Max Speed</string>

<string name="assist_mode">Assist Mode</string>


<string name="hal_1">HAL 1</string>
<string name="hal_2">HAL 2</string>
<string name="hal_3">HAL 3</string>
Expand Down

0 comments on commit 21aa71e

Please sign in to comment.