diff --git a/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java b/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java index 86bfc23..7b0adb0 100644 --- a/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java +++ b/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java @@ -1,17 +1,12 @@ package com.example.aped.ui.plots; import android.graphics.Color; -import android.graphics.ColorSpace; -import android.net.sip.SipSession; -import android.content.DialogInterface; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; -import android.widget.Toast; import com.example.aped.MainActivity; import com.example.aped.R; import com.example.aped.communication.Communicator; @@ -21,7 +16,6 @@ import com.jjoe64.graphview.LegendRenderer; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries; -import com.google.android.material.dialog.MaterialAlertDialogBuilder; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; @@ -34,17 +28,17 @@ public class PlotFragment extends Fragment { private MainActivity mainActivity; /** List of all the devices with buffers. **/ private List bufferedDevices; - /** Line Chart View that is displayed **/ + /** Line Chart View that is displayed. **/ private GraphView graphView; /** maximum value on X axis. **/ private int maxX = 0; /** - * paul - * @param inflater - * @param container + * Override method for the on create view event. + * @param inflater the default {@link LayoutInflater} + * @param container the {@link ViewGroup} the fragment is in * @param savedInstanceState - * @return + * @return the created view */ @Override public View onCreateView(final LayoutInflater inflater, @@ -64,6 +58,11 @@ public class PlotFragment extends Fragment { return view; } + /** + * Method to create a list of all the devices with buffers. + * @param xml the {@link IXML} to pull the information about the devices + * @return a list of all the device names + */ private List getBufferedDevices(final IXML xml) { List returnList = new ArrayList<>(); for (String deviceName : xml.getDeviceNames()) { @@ -74,11 +73,18 @@ public class PlotFragment extends Fragment { return returnList; } - private void getBufferedValues(final Communicator communicator, final IXML xml) { + /** + * Method to request and display all the buffered data. + * @param communicator the {@link Communicator} to request the data + * @param xml the {@link IXML} to get information about the data + */ + private void getBufferedValues(final Communicator communicator, + final IXML xml) { Random random = new Random(); for (String deviceName : this.bufferedDevices) { int factor; - if(xml.getPort(deviceName).get("protocol").equals("PWM")) { + //normalize PWM and digital data + if (xml.getPort(deviceName).get("protocol").equals("PWM")) { factor = 1; } else { factor = 100; @@ -86,10 +92,13 @@ public class PlotFragment extends Fragment { communicator.readBuffer(deviceName, response -> { try { JSONObject buffer = response.getJSONObject("buffer"); - for (String pin : (List) (xml.getPort(deviceName)).get("pins")) { + for (String pin : (List) (xml.getPort(deviceName)) + .get("pins")) { String valueString = buffer.getString(pin); - valueString = valueString.substring(2, valueString.length() - 2); - String[] valueStringArray = valueString.split("', '"); + valueString = valueString + .substring(2, valueString.length() - 2); + String[] valueStringArray = valueString + .split("', '"); int[] values = new int[valueStringArray.length]; for (int i = 0; i < valueStringArray.length; i++) { values[i] = Integer.parseInt(valueStringArray[i]); @@ -97,12 +106,17 @@ public class PlotFragment extends Fragment { //display data DataPoint[] dataPoints = new DataPoint[values.length]; - for (int i = 0; i < values.length; i++){ - dataPoints[i] = new DataPoint(i, values[i] * factor); + for (int i = 0; i < values.length; i++) { + dataPoints[i] = new DataPoint(i, + values[i] * factor); } - LineGraphSeries lineGraphSeries = new LineGraphSeries<>(dataPoints); - lineGraphSeries.setTitle(deviceName +":"+ pin); - lineGraphSeries.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + LineGraphSeries lineGraphSeries = + new LineGraphSeries<>(dataPoints); + lineGraphSeries.setTitle(deviceName + ":" + pin); + lineGraphSeries.setColor(Color.rgb( + random.nextInt(256), + random.nextInt(256), + random.nextInt(256))); graphView.addSeries(lineGraphSeries); if (maxX < values.length) { maxX = values.length; @@ -113,10 +127,14 @@ public class PlotFragment extends Fragment { graphView.getViewport().setXAxisBoundsManual(true); graphView.getViewport().setYAxisBoundsManual(true); graphView.getLegendRenderer().setVisible(true); - graphView.getLegendRenderer().setBackgroundColor(Color.rgb(200,200,200)); - graphView.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP); + graphView.getLegendRenderer().setBackgroundColor(Color.rgb( + 200, 200, 200)); + graphView.getLegendRenderer() + .setAlign(LegendRenderer.LegendAlign.TOP); } catch (JSONException e) { - Log.e("PlotFragment", "error while parsing JSON on READ BUFFER: " + e.getMessage()); + Log.e("PlotFragment", + "error while parsing JSON on READ BUFFER: " + + e.getMessage()); } }); }