end manual drive

This commit is contained in:
misty 2018-06-21 22:32:19 +02:00
parent 0aecd7e80b
commit 5c96e4b295
4 changed files with 171 additions and 81 deletions

View File

@ -0,0 +1,48 @@
package com.example.user.myapp;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
class CustomTouchView extends View {
public CustomTouchView(Context context) {
super(context);
}
boolean mDownTouch = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
// Listening for the down and up touch events
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownTouch = true;
return true;
case MotionEvent.ACTION_UP:
if (mDownTouch) {
mDownTouch = false;
performClick(); // Call this method to handle the response, and
// thereby enable accessibility services to
// perform this action for a user who cannot
// click the touchscreen.
return true;
}
}
return false; // Return false for other touch events
}
@Override
public boolean performClick() {
// Calls the super implementation, which generates an AccessibilityEvent
// and calls the onClick() listener on the view, if any
super.performClick();
// Handle the action for the custom click here
return true;
}
}

View File

@ -5,7 +5,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
@ -22,6 +21,7 @@ import java.util.Map;
public class ManualDriveActivity extends AppCompatActivity { public class ManualDriveActivity extends AppCompatActivity {
private GlobalState state; private GlobalState state;
private Handler coilHandler; private Handler coilHandler;
private Handler manualModeCheckHandler;
private HashMap<Integer, Boolean> mapCoilCheckStatus = new HashMap <> (); private HashMap<Integer, Boolean> mapCoilCheckStatus = new HashMap <> ();
private HashMap<ToggleButton, Integer> mapButtonCoil = new HashMap<>(); private HashMap<ToggleButton, Integer> mapButtonCoil = new HashMap<>();
@ -47,20 +47,26 @@ public class ManualDriveActivity extends AppCompatActivity {
setContentView(R.layout.activity_manual_drive); setContentView(R.layout.activity_manual_drive);
ToggleButton h1ControlButton = (ToggleButton) findViewById(R.id.h1_control_button); ToggleButton h1ControlButton =findViewById(R.id.h1_control_button);
ToggleButton h2ControlButton = (ToggleButton) findViewById(R.id.h2_control_button); ToggleButton h2ControlButton = findViewById(R.id.h2_control_button);
Button pu1ControlButton = (Button) findViewById(R.id.pu1_control_button); ToggleButton pu1ControlButton = findViewById(R.id.pu1_control_button);
Button pu2ControlButton = (Button) findViewById(R.id.pu2_control_button); ToggleButton pu2ControlButton = findViewById(R.id.pu2_control_button);
Button sp1ControlButton = (Button) findViewById(R.id.sp1_control_button); ToggleButton sp1ControlButton = findViewById(R.id.sp1_control_button);
Button b1ControlButton = (Button) findViewById(R.id.b1_control_button); ToggleButton b1ControlButton = findViewById(R.id.b1_control_button);
ToggleButton m1ControlButton = (ToggleButton) findViewById(R.id.m1_control_button); ToggleButton m1ControlButton = findViewById(R.id.m1_control_button);
ToggleButton m2ControlButton = (ToggleButton) findViewById(R.id.m2_control_button); ToggleButton m2ControlButton = findViewById(R.id.m2_control_button);
mapButtonCoil.put(h1ControlButton, 64); mapButtonCoil.put(h1ControlButton, 64);
mapButtonCoil.put(h2ControlButton, 70); mapButtonCoil.put(h2ControlButton, 70);
mapButtonCoil.put(pu1ControlButton, 65);
mapButtonCoil.put(pu2ControlButton, 71);
mapButtonCoil.put(sp1ControlButton, 68);
mapButtonCoil.put(b1ControlButton, 69);
mapButtonCoil.put(m1ControlButton, 66); mapButtonCoil.put(m1ControlButton, 66);
mapButtonCoil.put(m2ControlButton, 67); mapButtonCoil.put(m2ControlButton, 67);
LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("readVmCoil2")); LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("readVmCoil2"));
// LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue")); // LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue"));
LocalBroadcastManager.getInstance(this).registerReceiver(coilMessageReceiver, new IntentFilter("readOnlyCoil")); LocalBroadcastManager.getInstance(this).registerReceiver(coilMessageReceiver, new IntentFilter("readOnlyCoil"));
@ -111,7 +117,7 @@ public class ManualDriveActivity extends AppCompatActivity {
serviceIntent.putExtra("extra.ip.port", state.getPort()); serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", coilToWrite); serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.bit", true); serviceIntent.putExtra("extra.bit", true);
serviceIntent.putExtra("extra.intent.name", "writeBit65"); serviceIntent.putExtra("extra.intent.name", "writeBit");
serviceIntent.putExtra("extra.is.basic.coil", true); serviceIntent.putExtra("extra.is.basic.coil", true);
getApplicationContext().startService(serviceIntent); getApplicationContext().startService(serviceIntent);
mapCoilCheckStatus.put(coilToWrite, true); mapCoilCheckStatus.put(coilToWrite, true);
@ -123,7 +129,7 @@ public class ManualDriveActivity extends AppCompatActivity {
serviceIntent.putExtra("extra.ip.port", state.getPort()); serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", coilToWrite); serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.bit", false); serviceIntent.putExtra("extra.bit", false);
serviceIntent.putExtra("extra.intent.name", "writeBit65"); serviceIntent.putExtra("extra.intent.name", "writeBit");
serviceIntent.putExtra("extra.is.basic.coil", true); serviceIntent.putExtra("extra.is.basic.coil", true);
getApplicationContext().startService(serviceIntent); getApplicationContext().startService(serviceIntent);
mapCoilCheckStatus.put(coilToWrite, false); mapCoilCheckStatus.put(coilToWrite, false);
@ -138,31 +144,97 @@ public class ManualDriveActivity extends AppCompatActivity {
super.onStart(); super.onStart();
coilHandler = new Handler(); coilHandler = new Handler();
coilHandler.post(refreshCoil); coilHandler.post(refreshCoil);
manualModeCheckHandler = new Handler();
manualModeCheckHandler.post(checkManualMockStatus) ;
} }
private BroadcastReceiver coilWriterMessageReceiver = new BroadcastReceiver() { private BroadcastReceiver coilWriterMessageReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); boolean isManualModeActivated = intent.getBooleanExtra("coil", false);
boolean supposedToBeValue = intent.getBooleanExtra("coil", false); if (isManualModeActivated) {
ImageView lightOn = (ImageView) findViewById(R.id.manual_override_status_light_on);
ImageView lightOff = (ImageView) findViewById(R.id.manual_override_status_light_on);
System.out.print("value "+supposedToBeValue);
if (supposedToBeValue) {
manualMode = true; manualMode = true;
lightOn.setVisibility(View.VISIBLE);
lightOff.setVisibility(View.INVISIBLE);
} else { } else {
manualMode = false; manualMode = false;
lightOn.setVisibility(View.INVISIBLE);
lightOff.setVisibility(View.VISIBLE);
} }
} }
}; };
Runnable refreshCoil = new Runnable() { private Runnable checkManualMockStatus = new Runnable() {
@Override
public void run() {
ToggleButton h1ControlButton = findViewById(R.id.h1_control_button);
ToggleButton h2ControlButton = findViewById(R.id.h2_control_button);
Button pu1ControlButton = findViewById(R.id.pu1_control_button);
Button pu2ControlButton = findViewById(R.id.pu2_control_button);
Button sp1ControlButton = findViewById(R.id.sp1_control_button);
Button b1ControlButton = findViewById(R.id.b1_control_button);
ToggleButton m1ControlButton = findViewById(R.id.m1_control_button);
ToggleButton m2ControlButton = findViewById(R.id.m2_control_button);
// ============= Light ===============
ImageView manualLightOn = findViewById(R.id.manual_override_status_light_on);
ImageView manualLightOff = findViewById(R.id.manual_override_status_light_off);
ImageView h1StatusLightOn = findViewById(R.id.h1_status_light_on);
ImageView h1StatusLightOff = findViewById(R.id.h1_status_light_off);
ImageView h2StatusLightOn = findViewById(R.id.h2_status_light_on);
ImageView h2StatusLightOff = findViewById(R.id.h2_status_light_off);
ImageView pu1StatusLightOn = findViewById(R.id.pu1_status_light_on);
ImageView pu1StatusLightOff = findViewById(R.id.pu1_status_light_off);
ImageView pu2StatusLightOn = findViewById(R.id.pu2_status_light_on);
ImageView pu2StatusLightOff = findViewById(R.id.pu2_status_light_off);
ImageView sp1StatusLightOn = findViewById(R.id.sp1_status_light_on);
ImageView sp1StatusLightOff = findViewById(R.id.sp1_status_light_off);
ImageView b1StatusLightOn = findViewById(R.id.b1_status_light_on);
ImageView b1StatusLightOff = findViewById(R.id.b1_status_light_off);
ImageView m1StatusLightOn = findViewById(R.id.m1_status_light_on);
ImageView m1StatusLightOff = findViewById(R.id.m1_status_light_off);
ImageView m2StatusLightOn = findViewById(R.id.m2_status_light_on);
ImageView m2StatusLightOff = findViewById(R.id.m2_status_light_off);
if (!manualMode) {
manualLightOn.setVisibility(View.INVISIBLE);
manualLightOff.setVisibility(View.VISIBLE);
h1ControlButton.setEnabled(false);
h2ControlButton.setEnabled(false);
m1ControlButton.setEnabled(false);
m2ControlButton.setEnabled(false);
pu1ControlButton.setEnabled(false);
pu2ControlButton.setEnabled(false);
sp1ControlButton.setEnabled(false);
b1ControlButton.setEnabled(false);
manualLightOn.setVisibility(View.INVISIBLE);
manualLightOff.setVisibility(View.VISIBLE);
// switch all off
h1StatusLightOn.setVisibility(View.INVISIBLE);
h1StatusLightOff.setVisibility(View.INVISIBLE);
h2StatusLightOn.setVisibility(View.INVISIBLE);
h2StatusLightOff.setVisibility(View.INVISIBLE);
m1StatusLightOn.setVisibility(View.INVISIBLE);
m1StatusLightOff.setVisibility(View.INVISIBLE);
m2StatusLightOn.setVisibility(View.INVISIBLE);
m2StatusLightOff.setVisibility(View.INVISIBLE);
pu1StatusLightOn.setVisibility(View.INVISIBLE);
pu1StatusLightOff.setVisibility(View.INVISIBLE);
pu2StatusLightOn.setVisibility(View.INVISIBLE);
pu2StatusLightOff.setVisibility(View.INVISIBLE);
sp1StatusLightOn.setVisibility(View.INVISIBLE);
sp1StatusLightOff.setVisibility(View.INVISIBLE);
b1StatusLightOn.setVisibility(View.INVISIBLE);
b1StatusLightOff.setVisibility(View.INVISIBLE);
}
manualModeCheckHandler.postDelayed(this, 1000);
}
};
private Runnable refreshCoil = new Runnable() {
@Override @Override
public void run() { public void run() {
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
@ -304,35 +376,6 @@ public class ManualDriveActivity extends AppCompatActivity {
b1StatusLightOn.setVisibility(View.INVISIBLE); b1StatusLightOn.setVisibility(View.INVISIBLE);
b1StatusLightOff.setVisibility(View.VISIBLE); b1StatusLightOff.setVisibility(View.VISIBLE);
} }
} else {
h1ControlButton.setEnabled(false);
h2ControlButton.setEnabled(false);
m1ControlButton.setEnabled(false);
m2ControlButton.setEnabled(false);
pu1ControlButton.setEnabled(false);
pu2ControlButton.setEnabled(false);
sp1ControlButton.setEnabled(false);
b1ControlButton.setEnabled(false);
manualLightOn.setVisibility(View.INVISIBLE);
manualLightOff.setVisibility(View.VISIBLE);
// switch all off
h1StatusLightOn.setVisibility(View.INVISIBLE);
h1StatusLightOff.setVisibility(View.INVISIBLE);
h2StatusLightOn.setVisibility(View.INVISIBLE);
h2StatusLightOff.setVisibility(View.INVISIBLE);
m1StatusLightOn.setVisibility(View.INVISIBLE);
m1StatusLightOff.setVisibility(View.INVISIBLE);
m2StatusLightOn.setVisibility(View.INVISIBLE);
m2StatusLightOff.setVisibility(View.INVISIBLE);
pu1StatusLightOn.setVisibility(View.INVISIBLE);
pu1StatusLightOff.setVisibility(View.INVISIBLE);
pu2StatusLightOn.setVisibility(View.INVISIBLE);
pu2StatusLightOff.setVisibility(View.INVISIBLE);
sp1StatusLightOn.setVisibility(View.INVISIBLE);
sp1StatusLightOff.setVisibility(View.INVISIBLE);
b1StatusLightOn.setVisibility(View.INVISIBLE);
b1StatusLightOff.setVisibility(View.INVISIBLE);
} }
} }
}; };

View File

@ -156,7 +156,6 @@ public class MyIntentService extends IntentService {
private void sendNewBitValueToActivity (boolean newValue, String intentReceive) { private void sendNewBitValueToActivity (boolean newValue, String intentReceive) {
Intent intent = new Intent(intentReceive); Intent intent = new Intent(intentReceive);
System.out.println("here "+newValue);
intent.putExtra("coil", newValue); intent.putExtra("coil", newValue);
sendLocationBroadcast(intent); sendLocationBroadcast(intent);
} }

View File

@ -180,7 +180,7 @@
app:layout_constraintHorizontal_bias="0.476" app:layout_constraintHorizontal_bias="0.476"
app:layout_constraintStart_toStartOf="@+id/pu2_control_button" /> app:layout_constraintStart_toStartOf="@+id/pu2_control_button" />
<Button <ToggleButton
android:id="@+id/pu1_control_button" android:id="@+id/pu1_control_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -193,7 +193,7 @@
app:layout_constraintTop_toBottomOf="@+id/h1_control_button" /> app:layout_constraintTop_toBottomOf="@+id/h1_control_button" />
<Button <ToggleButton
android:id="@+id/pu2_control_button" android:id="@+id/pu2_control_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -239,7 +239,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:barrierDirection="top" /> app:barrierDirection="top" />
<Button <ToggleButton
android:id="@+id/sp1_control_button" android:id="@+id/sp1_control_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -286,7 +286,7 @@
app:layout_constraintHorizontal_bias="0.476" app:layout_constraintHorizontal_bias="0.476"
app:layout_constraintStart_toStartOf="@+id/sp1_control_button" /> app:layout_constraintStart_toStartOf="@+id/sp1_control_button" />
<Button <ToggleButton
android:id="@+id/b1_control_button" android:id="@+id/b1_control_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"