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

package com.example.lfrmobileapp; package com.example.lfrmobileapp;


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


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


public class Communication { public class Communication {
public boolean sendData(String data, String ip, int port) { public boolean sendData(String data, String ip, int port) {
return true; 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

import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;


import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;


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




public class DashboardFragment extends Fragment { 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; private FragmentAutomatikBinding binding;


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


public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
final TextView textView = binding.textDashboard; final TextView textView = binding.textDashboard;
dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);


//Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit(); 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. // 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){ ArrayAdapter<String> adapter = new ArrayAdapter<String>(root.getContext(), android.R.layout.simple_list_item_1, autoList){
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
View view =super.getView(position, convertView, parent); View view =super.getView(position, convertView, parent);

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

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

return view; return view;
} }


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




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


public void onClick(View v) { public void onClick(View v) {
// on below line we are getting text from edit text // on below line we are getting text from edit text
String item = "Links"; String item = "Links";
Boolean automaticOn = binding.startAutomatic.isChecked();
// on below line we are checking if item is not empty // 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. // on below line we are adding item to our list.
autoList.add(item); autoList.add(item);
// on below line we are notifying adapter // on below line we are notifying adapter
// update our list view. // update our list view.
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} }

} }
}); });


// on below line we are getting text from edit text // on below line we are getting text from edit text
String item = "Rechts"; String item = "Rechts";


Boolean automaticOn = binding.startAutomatic.isChecked();
// on below line we are checking if item is not empty // 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. // on below line we are adding item to our list.
autoList.add(item); autoList.add(item);
// on below line we are notifying adapter // on below line we are notifying adapter
} }
}); });


//Deleting element in ListView
binding.automaticList.setOnItemClickListener(new AdapterView.OnItemClickListener() { binding.automaticList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 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; return root;
} }




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



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



public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {


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


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


Communication com = new Communication();

private FragmentManuellBinding binding; private FragmentManuellBinding binding;


public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit(); SharedPreferences.Editor editor = sharedPref.edit();


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


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


super.onDestroyView(); super.onDestroyView();
binding = null; binding = null;
} }
}



}

Loading…
Cancel
Save