App: Telegramm with wheel datas/Turn directions
This commit is contained in:
parent
ed730bc438
commit
53c37e03c1
@ -1,10 +1,14 @@
|
|||||||
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) {
|
||||||
@ -20,4 +24,40 @@ public class Communication {
|
|||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -9,6 +9,7 @@ import android.view.View;
|
|||||||
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;
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ import androidx.annotation.NonNull;
|
|||||||
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;
|
||||||
|
|
||||||
@ -27,8 +29,16 @@ import kotlin.collections.UArraySortingKt;
|
|||||||
|
|
||||||
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,
|
||||||
@ -42,35 +52,51 @@ public class DashboardFragment extends Fragment {
|
|||||||
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, ""));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
//Set Color to black
|
||||||
/*YOUR CHOICE OF COLOR*/
|
|
||||||
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);
|
||||||
|
|
||||||
@ -80,9 +106,9 @@ public class DashboardFragment extends Fragment {
|
|||||||
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
|
||||||
@ -90,7 +116,6 @@ public class DashboardFragment extends Fragment {
|
|||||||
// update our list view.
|
// update our list view.
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -101,8 +126,9 @@ public class DashboardFragment extends Fragment {
|
|||||||
// 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
|
||||||
@ -113,14 +139,17 @@ public class DashboardFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//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) {
|
||||||
|
Boolean automaticOn = binding.startAutomatic.isChecked();
|
||||||
|
if(!automaticOn) {
|
||||||
autoList.remove(position);
|
autoList.remove(position);
|
||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,11 +161,11 @@ public class DashboardFragment extends Fragment {
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,12 +23,16 @@ import io.github.controlwear.virtual.joystick.android.JoystickView;
|
|||||||
|
|
||||||
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,
|
||||||
@ -42,40 +46,63 @@ public class HomeFragment extends Fragment {
|
|||||||
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);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -87,6 +114,5 @@ public class HomeFragment extends Fragment {
|
|||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
binding = null;
|
binding = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user