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.IntentFilter;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@ -22,6 +21,7 @@ import java.util.Map;
public class ManualDriveActivity extends AppCompatActivity {
private GlobalState state;
private Handler coilHandler;
private Handler manualModeCheckHandler;
private HashMap<Integer, Boolean> mapCoilCheckStatus = new HashMap <> ();
private HashMap<ToggleButton, Integer> mapButtonCoil = new HashMap<>();
@ -47,20 +47,26 @@ public class ManualDriveActivity extends AppCompatActivity {
setContentView(R.layout.activity_manual_drive);
ToggleButton h1ControlButton = (ToggleButton) findViewById(R.id.h1_control_button);
ToggleButton h2ControlButton = (ToggleButton) findViewById(R.id.h2_control_button);
Button pu1ControlButton = (Button) findViewById(R.id.pu1_control_button);
Button pu2ControlButton = (Button) findViewById(R.id.pu2_control_button);
Button sp1ControlButton = (Button) findViewById(R.id.sp1_control_button);
Button b1ControlButton = (Button) findViewById(R.id.b1_control_button);
ToggleButton m1ControlButton = (ToggleButton) findViewById(R.id.m1_control_button);
ToggleButton m2ControlButton = (ToggleButton) findViewById(R.id.m2_control_button);
ToggleButton h1ControlButton =findViewById(R.id.h1_control_button);
ToggleButton h2ControlButton = findViewById(R.id.h2_control_button);
ToggleButton pu1ControlButton = findViewById(R.id.pu1_control_button);
ToggleButton pu2ControlButton = findViewById(R.id.pu2_control_button);
ToggleButton sp1ControlButton = findViewById(R.id.sp1_control_button);
ToggleButton b1ControlButton = findViewById(R.id.b1_control_button);
ToggleButton m1ControlButton = findViewById(R.id.m1_control_button);
ToggleButton m2ControlButton = findViewById(R.id.m2_control_button);
mapButtonCoil.put(h1ControlButton, 64);
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(m2ControlButton, 67);
LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("readVmCoil2"));
// LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue"));
LocalBroadcastManager.getInstance(this).registerReceiver(coilMessageReceiver, new IntentFilter("readOnlyCoil"));
@ -103,31 +109,31 @@ public class ManualDriveActivity extends AppCompatActivity {
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && manualMode) {
// writing the value true
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.bit", true);
serviceIntent.putExtra("extra.intent.name", "writeBit65");
serviceIntent.putExtra("extra.is.basic.coil", true);
getApplicationContext().startService(serviceIntent);
mapCoilCheckStatus.put(coilToWrite, true);
} else {
// writing the value false
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.bit", false);
serviceIntent.putExtra("extra.intent.name", "writeBit65");
serviceIntent.putExtra("extra.is.basic.coil", true);
getApplicationContext().startService(serviceIntent);
mapCoilCheckStatus.put(coilToWrite, false);
}
if (isChecked && manualMode) {
// writing the value true
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.bit", true);
serviceIntent.putExtra("extra.intent.name", "writeBit");
serviceIntent.putExtra("extra.is.basic.coil", true);
getApplicationContext().startService(serviceIntent);
mapCoilCheckStatus.put(coilToWrite, true);
} else {
// writing the value false
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.bit", false);
serviceIntent.putExtra("extra.intent.name", "writeBit");
serviceIntent.putExtra("extra.is.basic.coil", true);
getApplicationContext().startService(serviceIntent);
mapCoilCheckStatus.put(coilToWrite, false);
}
}
});
}
@ -138,31 +144,97 @@ public class ManualDriveActivity extends AppCompatActivity {
super.onStart();
coilHandler = new Handler();
coilHandler.post(refreshCoil);
manualModeCheckHandler = new Handler();
manualModeCheckHandler.post(checkManualMockStatus) ;
}
private BroadcastReceiver coilWriterMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
boolean supposedToBeValue = intent.getBooleanExtra("coil", false);
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) {
boolean isManualModeActivated = intent.getBooleanExtra("coil", false);
if (isManualModeActivated) {
manualMode = true;
lightOn.setVisibility(View.VISIBLE);
lightOff.setVisibility(View.INVISIBLE);
} else {
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
public void run() {
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
@ -304,35 +376,6 @@ public class ManualDriveActivity extends AppCompatActivity {
b1StatusLightOn.setVisibility(View.INVISIBLE);
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) {
Intent intent = new Intent(intentReceive);
System.out.println("here "+newValue);
intent.putExtra("coil", newValue);
sendLocationBroadcast(intent);
}

View File

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