Browse Source

App: Telegramm with wheel datas/Turn directions

master
Baran Yasar 1 year ago
parent
commit
53c37e03c1

+ 40
- 0
MobileApp/app/src/main/java/com/example/lfrmobileapp/Communication.java View File

@@ -1,10 +1,14 @@
package com.example.lfrmobileapp;

import android.accounts.NetworkErrorException;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;

public class Communication {
public boolean sendData(String data, String ip, int port) {
@@ -20,4 +24,40 @@ public class Communication {
return true;
}

public String telegram(int[] wheels, int strenght){
String tel = "";
tel += "0;"; //Manuel mode

for(int i = 0; i < wheels.length; i++){
if(wheels[i] == 1){
tel += Float.toString(((float)strenght)/100) + ";";
}else if(wheels[i] == -1)
tel += Float.toString(((float)-strenght)/100) + ";";
else{
tel += "0.00;";
}
}
Log.v("TAG", tel + "\n");
return tel;
}

public String telegram(boolean startStop, ArrayList<String> list){
String tel = "";
tel += "1;"; //Autonomous mode
if(startStop){
tel += "1;";
}else{
tel += "0;";
}

for (String i : list) {
if(i.equals("Links")){
tel += "0;";
}else if (i.equals("Rechts")){
tel += "1;";
}
}
Log.v("TAG", tel + "\n");
return tel;
}
}

+ 43
- 14
MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/dashboard/DashboardFragment.java View File

@@ -9,6 +9,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

@@ -16,6 +17,7 @@ import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.lfrmobileapp.Communication;
import com.example.lfrmobileapp.R;
import com.example.lfrmobileapp.databinding.FragmentAutomatikBinding;

@@ -27,8 +29,16 @@ import kotlin.collections.UArraySortingKt;

public class DashboardFragment extends Fragment {

String ipKey = "ipkey";
String portKey = "portkey";

String ipAddress = "192.168.0.1";
int port = 8000;

Communication com = new Communication();
private FragmentAutomatikBinding binding;

//Keys for Shared Preferences
String listKey = "listkey";

public View onCreateView(@NonNull LayoutInflater inflater,
@@ -42,35 +52,51 @@ public class DashboardFragment extends Fragment {
final TextView textView = binding.textDashboard;
dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

//Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();

//Get Arraylist from Shared Preferences String
ArrayList<String> autoList = stringToArraylist(sharedPref.getString(listKey, ""));

ipAddress = sharedPref.getString(ipKey, "");
port = sharedPref.getInt(portKey, -1);

binding.startAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
com.sendData(com.telegram(true, autoList), ipAddress, port);
}else{
com.sendData(com.telegram(false, autoList), ipAddress, port);
}
}
});

ArrayList<String> autoList = stringToArraylist(sharedPref.getString(listKey, ""));

// on the below line we are initializing the adapter for our list view.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(root.getContext(), android.R.layout.simple_list_item_1, autoList){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view =super.getView(position, convertView, parent);

TextView textView=(TextView) view.findViewById(android.R.id.text1);

/*YOUR CHOICE OF COLOR*/
//Set Color to black
textView.setTextColor(Color.BLACK);

return view;
}

@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
//Save Arraylist in Shared Preferences as String
editor.putString(listKey, listToString(autoList));
editor.apply();
}
};



// on below line we are setting adapter for our list view.
binding.automaticList.setAdapter(adapter);

@@ -80,9 +106,9 @@ public class DashboardFragment extends Fragment {
public void onClick(View v) {
// on below line we are getting text from edit text
String item = "Links";
Boolean automaticOn = binding.startAutomatic.isChecked();
// on below line we are checking if item is not empty
if (!item.isEmpty()) {
if (!item.isEmpty() && !automaticOn) {
// on below line we are adding item to our list.
autoList.add(item);
// on below line we are notifying adapter
@@ -90,7 +116,6 @@ public class DashboardFragment extends Fragment {
// update our list view.
adapter.notifyDataSetChanged();
}

}
});

@@ -101,8 +126,9 @@ public class DashboardFragment extends Fragment {
// on below line we are getting text from edit text
String item = "Rechts";

Boolean automaticOn = binding.startAutomatic.isChecked();
// on below line we are checking if item is not empty
if (!item.isEmpty()) {
if (!item.isEmpty() && !automaticOn) {
// on below line we are adding item to our list.
autoList.add(item);
// on below line we are notifying adapter
@@ -113,14 +139,17 @@ public class DashboardFragment extends Fragment {
}
});

//Deleting element in ListView
binding.automaticList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
autoList.remove(position);
adapter.notifyDataSetChanged();
Boolean automaticOn = binding.startAutomatic.isChecked();
if(!automaticOn) {
autoList.remove(position);
adapter.notifyDataSetChanged();
}
}
});

return root;
}

@@ -132,11 +161,11 @@ public class DashboardFragment extends Fragment {

public String listToString(ArrayList<String> list){
String result = "";
//if(!list.size().isEmptry()) {
if(!list.isEmpty()) {
for (String s : list) {
result += s + ";";
}
//}
}
return result;
}


+ 31
- 5
MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/home/HomeFragment.java View File

@@ -23,12 +23,16 @@ import io.github.controlwear.virtual.joystick.android.JoystickView;

public class HomeFragment extends Fragment {

//Keys for Shared Preferences
String ipKey = "ipkey";
String portKey = "portkey";

//Default Ip-Address and Port
String ipAddress = "192.168.0.1";
int port = 8000;

Communication com = new Communication();

private FragmentManuellBinding binding;

public View onCreateView(@NonNull LayoutInflater inflater,
@@ -42,40 +46,63 @@ public class HomeFragment extends Fragment {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();

//Get saved Ip-Adress and Port from Shared Preferences
ipAddress = sharedPref.getString(ipKey, "");
port = sharedPref.getInt(portKey, 0);

//Set text from Textview
final TextView textView = binding.textHome;
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

binding.rotateLeft.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
com.sendData(com.telegram(new int[]{-1, 1, -1, 1}, 100), ipAddress, port);
};
});

binding.rotateRight.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
com.sendData(com.telegram(new int[]{1, -1, 1, -1}, 100), ipAddress, port);
};
});

JoystickView joystick = (JoystickView) binding.joystick;
Communication com = new Communication();
joystick.setOnMoveListener(new JoystickView.OnMoveListener() {
@Override
public void onMove(int angle, int strength) {
homeViewModel.setText(Integer.toString(angle), Integer.toString(strength));
//8 segments of the joystick
if((angle >= 338 || angle < 22) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right);
com.sendData(com.telegram(new int[]{1, -1, -1, 1}, strength), ipAddress, port);
}else if((angle >= 22 && angle < 67) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right_forward);
com.sendData(com.telegram(new int[]{1, 0, 0, 1}, strength), ipAddress, port);
}else if((angle >=67 && angle < 112) && strength != 0){
joystick.setBackgroundResource(R.mipmap.forward);
com.sendData(com.telegram(new int[]{1, 1, 1, 1}, strength), ipAddress, port);
}else if((angle >=112 && angle < 157) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left_forward);
com.sendData(com.telegram(new int[]{0, 1, 1, 0}, strength), ipAddress, port);
}else if((angle >=157 && angle < 202) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left);
com.sendData(com.telegram(new int[]{-1, 1, 1, -1}, strength), ipAddress, port);
}else if((angle >=202 && angle < 247) && strength != 0){
joystick.setBackgroundResource(R.mipmap.left_back);
com.sendData(com.telegram(new int[]{-1, 0, 0, -1}, strength), ipAddress, port);
}else if((angle >=247 && angle < 292) && strength != 0){
joystick.setBackgroundResource(R.mipmap.back);
com.sendData(com.telegram(new int[]{-1, -1, -1, -1}, strength), ipAddress, port);
}else if((angle >=292 && angle < 337) && strength != 0){
joystick.setBackgroundResource(R.mipmap.right_back);
com.sendData(com.telegram(new int[]{0, -1, -1, 0}, strength), ipAddress, port);
}
else{
joystick.setBackgroundResource(R.mipmap.blank);
com.sendData(com.telegram(new int[]{0, 0, 0, 0}, strength), ipAddress, port);
}

com.sendData("Vanessa stinkt, Baran nicht", ipAddress, port);
}
});

@@ -87,6 +114,5 @@ public class HomeFragment extends Fragment {
super.onDestroyView();
binding = null;
}
}


}

Loading…
Cancel
Save