|
| 1 | +package io.pslab.fragment; |
| 2 | + |
| 3 | +import android.graphics.Color; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.os.Handler; |
| 6 | +import android.support.annotation.NonNull; |
| 7 | +import android.support.annotation.Nullable; |
| 8 | +import android.support.v4.app.Fragment; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import android.widget.TextView; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import com.github.anastr.speedviewlib.PointerSpeedometer; |
| 16 | +import com.github.mikephil.charting.charts.LineChart; |
| 17 | +import com.github.mikephil.charting.components.Legend; |
| 18 | +import com.github.mikephil.charting.components.XAxis; |
| 19 | +import com.github.mikephil.charting.components.YAxis; |
| 20 | +import com.github.mikephil.charting.data.Entry; |
| 21 | +import com.github.mikephil.charting.data.LineData; |
| 22 | +import com.github.mikephil.charting.data.LineDataSet; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Timer; |
| 26 | +import java.util.TimerTask; |
| 27 | + |
| 28 | +import butterknife.BindView; |
| 29 | +import butterknife.ButterKnife; |
| 30 | +import butterknife.Unbinder; |
| 31 | +import io.pslab.R; |
| 32 | +import io.pslab.activity.GasSensorActivity; |
| 33 | +import io.pslab.communication.ScienceLab; |
| 34 | +import io.pslab.others.ScienceLabCommon; |
| 35 | + |
| 36 | +public class GasSensorDataFragment extends Fragment { |
| 37 | + |
| 38 | + @BindView(R.id.gas_sensor_value) |
| 39 | + TextView gasValue; |
| 40 | + @BindView(R.id.label_gas_sensor) |
| 41 | + TextView sensorLabel; |
| 42 | + @BindView(R.id.chart_gas_sensor) |
| 43 | + LineChart mChart; |
| 44 | + @BindView(R.id.gas_sensor) |
| 45 | + PointerSpeedometer gasSensorMeter; |
| 46 | + private GasSensorActivity gasSensorActivity; |
| 47 | + private View rootView; |
| 48 | + private Unbinder unbinder; |
| 49 | + private ScienceLab scienceLab; |
| 50 | + private YAxis y; |
| 51 | + private Timer graphTimer; |
| 52 | + private ArrayList<Entry> entries; |
| 53 | + private long updatePeriod = 1000; |
| 54 | + private long startTime; |
| 55 | + private long timeElapsed; |
| 56 | + private long previousTimeElapsed = (System.currentTimeMillis() - startTime) / updatePeriod; |
| 57 | + |
| 58 | + public static GasSensorDataFragment newInstance() { |
| 59 | + return new GasSensorDataFragment(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onCreate(@Nullable Bundle savedInstanceState) { |
| 64 | + super.onCreate(savedInstanceState); |
| 65 | + startTime = System.currentTimeMillis(); |
| 66 | + gasSensorActivity = (GasSensorActivity) getActivity(); |
| 67 | + graphTimer = new Timer(); |
| 68 | + } |
| 69 | + |
| 70 | + @Nullable |
| 71 | + @Override |
| 72 | + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| 73 | + rootView = inflater.inflate(R.layout.fragment_gas_sensor, container, false); |
| 74 | + unbinder = ButterKnife.bind(this, rootView); |
| 75 | + scienceLab = ScienceLabCommon.scienceLab; |
| 76 | + entries = new ArrayList<>(); |
| 77 | + setupInstruments(); |
| 78 | + return rootView; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onResume() { |
| 83 | + super.onResume(); |
| 84 | + mChart.clear(); |
| 85 | + entries.clear(); |
| 86 | + mChart.invalidate(); |
| 87 | + updateGraphs(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onDestroyView() { |
| 92 | + super.onDestroyView(); |
| 93 | + if (graphTimer != null) { |
| 94 | + graphTimer.cancel(); |
| 95 | + } |
| 96 | + unbinder.unbind(); |
| 97 | + } |
| 98 | + |
| 99 | + private void updateGraphs() { |
| 100 | + final Handler handler = new Handler(); |
| 101 | + if (graphTimer != null) { |
| 102 | + graphTimer.cancel(); |
| 103 | + } |
| 104 | + graphTimer = new Timer(); |
| 105 | + graphTimer.schedule(new TimerTask() { |
| 106 | + @Override |
| 107 | + public void run() { |
| 108 | + handler.post(new Runnable() { |
| 109 | + @Override |
| 110 | + public void run() { |
| 111 | + try { |
| 112 | + visualizeData(); |
| 113 | + } catch (NullPointerException e) { |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + }, 0, 1000); |
| 119 | + } |
| 120 | + |
| 121 | + private void visualizeData() { |
| 122 | + if (scienceLab.isConnected()) { |
| 123 | + double volt = scienceLab.getVoltage("CH1", 1); |
| 124 | + double ppmValue = (volt / 3.3) * 1024.0; |
| 125 | + gasValue.setText(String.valueOf(String.format("%.2f", ppmValue))); |
| 126 | + gasSensorMeter.setWithTremble(false); |
| 127 | + gasSensorMeter.setSpeedAt((float) ppmValue); |
| 128 | + timeElapsed = ((System.currentTimeMillis() - startTime) / updatePeriod); |
| 129 | + if (timeElapsed != previousTimeElapsed) { |
| 130 | + previousTimeElapsed = timeElapsed; |
| 131 | + Entry entry = new Entry((float) timeElapsed, (float) ppmValue); |
| 132 | + entries.add(entry); |
| 133 | + |
| 134 | + LineDataSet dataSet = new LineDataSet(entries, getString(R.string.gas_sensor_unit)); |
| 135 | + dataSet.setDrawCircles(false); |
| 136 | + dataSet.setDrawValues(false); |
| 137 | + dataSet.setLineWidth(2); |
| 138 | + LineData data = new LineData(dataSet); |
| 139 | + |
| 140 | + mChart.setData(data); |
| 141 | + mChart.notifyDataSetChanged(); |
| 142 | + mChart.setVisibleXRangeMaximum(80); |
| 143 | + mChart.moveViewToX(data.getEntryCount()); |
| 144 | + mChart.invalidate(); |
| 145 | + } |
| 146 | + } else { |
| 147 | + Toast.makeText(getContext(), R.string.not_connected, Toast.LENGTH_SHORT).show(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void setupInstruments() { |
| 152 | + gasSensorMeter.setMaxSpeed(1024); |
| 153 | + XAxis x = mChart.getXAxis(); |
| 154 | + this.y = mChart.getAxisLeft(); |
| 155 | + YAxis y2 = mChart.getAxisRight(); |
| 156 | + |
| 157 | + mChart.setTouchEnabled(true); |
| 158 | + mChart.setHighlightPerDragEnabled(true); |
| 159 | + mChart.setDragEnabled(true); |
| 160 | + mChart.setScaleEnabled(true); |
| 161 | + mChart.setDrawGridBackground(false); |
| 162 | + mChart.setPinchZoom(true); |
| 163 | + mChart.setScaleYEnabled(true); |
| 164 | + mChart.setBackgroundColor(Color.BLACK); |
| 165 | + mChart.getDescription().setEnabled(false); |
| 166 | + |
| 167 | + LineData data = new LineData(); |
| 168 | + mChart.setData(data); |
| 169 | + |
| 170 | + Legend l = mChart.getLegend(); |
| 171 | + l.setForm(Legend.LegendForm.LINE); |
| 172 | + l.setTextColor(Color.WHITE); |
| 173 | + |
| 174 | + x.setTextColor(Color.WHITE); |
| 175 | + x.setDrawGridLines(true); |
| 176 | + x.setAvoidFirstLastClipping(true); |
| 177 | + |
| 178 | + y.setTextColor(Color.WHITE); |
| 179 | + y.setAxisMaximum(1024); |
| 180 | + y.setAxisMinimum(0); |
| 181 | + y.setDrawGridLines(true); |
| 182 | + y.setLabelCount(10); |
| 183 | + |
| 184 | + y2.setDrawGridLines(false); |
| 185 | + y2.setMaxWidth(0); |
| 186 | + } |
| 187 | +} |
0 commit comments