Skip to content

Commit 6935884

Browse files
authored
Merge pull request fossasia#1828 from neel1998/oscilloscope_xy_plot
Fix fossasia#1823 xy plot working fine in oscilloscope
2 parents fb78421 + 575add4 commit 6935884

File tree

3 files changed

+59
-88
lines changed

3 files changed

+59
-88
lines changed

app/src/main/java/io/pslab/activity/OscilloscopeActivity.java

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public class OscilloscopeActivity extends AppCompatActivity implements View.OnCl
117117
public boolean isXYPlotSelected;
118118
public boolean sineFit;
119119
public boolean squareFit;
120-
public boolean viewIsClicked;
121120
public boolean isCH1FrequencyRequired;
122121
public boolean isCH2FrequencyRequired;
123122
public String triggerChannel;
@@ -204,6 +203,8 @@ public class OscilloscopeActivity extends AppCompatActivity implements View.OnCl
204203
private HashMap<String, Integer> channelIndexMap;
205204
private Integer[] channelColors = {Color.CYAN, Color.GREEN, Color.WHITE, Color.MAGENTA};
206205
private String[] loggingYdata = new String[4];
206+
public String xyPlotAxis1 = "CH1";
207+
public String xyPlotAxis2 = "CH2";
207208

208209
private enum CHANNEL {CH1, CH2, CH3, MIC}
209210

@@ -257,7 +258,6 @@ public void onClick(View v) {
257258
curveFittingChannel2 = "None";
258259
xyPlotXAxisChannel = CHANNEL.CH1.toString();
259260
xyPlotYAxisChannel = CHANNEL.CH2.toString();
260-
viewIsClicked = false;
261261
analyticsClass = new AnalyticsClass();
262262
isCH1FrequencyRequired = false;
263263
isCH2FrequencyRequired = false;
@@ -456,12 +456,9 @@ public void run() {
456456
}
457457
}
458458

459-
if (scienceLab.isConnected() && viewIsClicked && isXYPlotSelected) {
459+
if (scienceLab.isConnected() && isXYPlotSelected) {
460460
xyPlotTask = new XYPlotTask();
461-
if (xyPlotXAxisChannel.equals(CHANNEL.CH2.toString()))
462-
xyPlotTask.execute(xyPlotYAxisChannel);
463-
else
464-
xyPlotTask.execute(xyPlotXAxisChannel);
461+
xyPlotTask.execute(xyPlotAxis1, xyPlotAxis2);
465462
synchronized (lock) {
466463
try {
467464
lock.wait();
@@ -1106,69 +1103,65 @@ protected void onPostExecute(Void aVoid) {
11061103
}
11071104

11081105
public class XYPlotTask extends AsyncTask<String, Void, Void> {
1109-
String analogInput;
1110-
float[] xFloatData = new float[1000];
1111-
float[] yFloatData = new float[1000];
1106+
private String analogInput1;
1107+
private String analogInput2;
1108+
private float[] xFloatData;
1109+
private float[] yFloatData;
11121110

11131111
@Override
11141112
protected Void doInBackground(String... params) {
1113+
analogInput1 = params[0];
1114+
analogInput2 = params[1];
11151115
HashMap<String, double[]> data;
1116-
if ("CH2".equals(xyPlotXAxisChannel) || "CH2".equals(xyPlotYAxisChannel)) {
1117-
analogInput = params[0];
1118-
data = scienceLab.captureTwo(1000, 10, analogInput, false);
1119-
double y1Data[] = data.get("y1");
1120-
double y2Data[] = data.get("y2");
1121-
if ("CH2".equals(xyPlotYAxisChannel)) {
1122-
for (int i = 0; i < y1Data.length; i++) {
1123-
xFloatData[i] = (float) y1Data[i];
1124-
yFloatData[i] = (float) y2Data[i];
1116+
if (analogInput1.equals(analogInput2)) {
1117+
scienceLab.captureTraces(1, samples, timeGap, analogInput1, isTriggerSelected, null);
1118+
data = scienceLab.fetchTrace(1);
1119+
double[] yData = data.get("y");
1120+
int n = yData.length;
1121+
xFloatData = new float[n];
1122+
yFloatData = new float[n];
1123+
for (int i = 0; i < n; i++) {
1124+
xFloatData[i] = (float) yData[i];
1125+
yFloatData[i] = (float) yData[i];
1126+
}
1127+
} else {
1128+
int noChannels = 1;
1129+
if ((analogInput1.equals(CHANNEL.CH1.toString()) && analogInput2.equals(CHANNEL.CH2.toString())) || (analogInput1.equals(CHANNEL.CH2.toString()) && analogInput2.equals(CHANNEL.CH1.toString()))) {
1130+
noChannels = 2;
1131+
scienceLab.captureTraces(noChannels, 175, timeGap, "CH1", isTriggerSelected, null);
1132+
data = scienceLab.fetchTrace(1);
1133+
double[] yData1 = data.get("y");
1134+
data = scienceLab.fetchTrace(2);
1135+
double[] yData2 = data.get("y");
1136+
int n = Math.min(yData1.length, yData2.length);
1137+
xFloatData = new float[n];
1138+
yFloatData = new float[n];
1139+
for (int i = 0; i < n; i++) {
1140+
xFloatData[i] = (float) yData1[i];
1141+
yFloatData[i] = (float) yData2[i];
11251142
}
1143+
11261144
} else {
1127-
for (int i = 0; i < y1Data.length; i++) {
1128-
xFloatData[i] = (float) y2Data[i];
1129-
yFloatData[i] = (float) y1Data[i];
1145+
noChannels = 4;
1146+
scienceLab.captureTraces(noChannels, 175, timeGap, "CH1", isTriggerSelected, null);
1147+
data = scienceLab.fetchTrace(channelIndexMap.get(analogInput1) + 1);
1148+
double[] yData1 = data.get("y");
1149+
data = scienceLab.fetchTrace(channelIndexMap.get(analogInput2) + 1);
1150+
double[] yData2 = data.get("y");
1151+
int n = Math.min(yData1.length, yData2.length);
1152+
xFloatData = new float[n];
1153+
yFloatData = new float[n];
1154+
for (int i = 0; i < n; i++) {
1155+
xFloatData[i] = (float) yData1[i];
1156+
yFloatData[i] = (float) yData2[i];
11301157
}
11311158
}
1132-
} else {
1133-
data = scienceLab.captureFour(1000, 10, analogInput, false);
1134-
double[] y1Data = data.get("y");
1135-
double[] y3Data = data.get("y3");
1136-
double[] y4Data = data.get("y4");
1137-
switch (xyPlotXAxisChannel) {
1138-
case "CH1":
1139-
for (int i = 0; i < y1Data.length; i++) {
1140-
xFloatData[i] = (float) y1Data[i];
1141-
}
1142-
break;
1143-
case "CH3":
1144-
for (int i = 0; i < y3Data.length; i++) {
1145-
xFloatData[i] = (float) y3Data[i];
1146-
}
1147-
break;
1148-
case "MIC":
1149-
for (int i = 0; i < y4Data.length; i++) {
1150-
xFloatData[i] = (float) y4Data[i];
1151-
}
1152-
break;
1153-
}
11541159

1155-
switch (xyPlotYAxisChannel) {
1156-
case "CH1":
1157-
for (int i = 0; i < y1Data.length; i++) {
1158-
yFloatData[i] = (float) y1Data[i];
1159-
}
1160-
break;
1161-
case "CH3":
1162-
for (int i = 0; i < y3Data.length; i++) {
1163-
yFloatData[i] = (float) y3Data[i];
1164-
}
1165-
break;
1166-
case "MIC":
1167-
for (int i = 0; i < y4Data.length; i++) {
1168-
yFloatData[i] = (float) y4Data[i];
1169-
}
1170-
break;
1171-
}
1160+
}
1161+
try {
1162+
Thread.sleep(200);
1163+
} catch (InterruptedException e) {
1164+
e.printStackTrace();
11721165
}
11731166
return null;
11741167
}
@@ -1177,6 +1170,9 @@ protected Void doInBackground(String... params) {
11771170
protected void onPostExecute(Void aVoid) {
11781171
super.onPostExecute(aVoid);
11791172
graph.plotData(xFloatData, yFloatData, 1);
1173+
synchronized (lock) {
1174+
lock.notify();
1175+
}
11801176
}
11811177
}
11821178

app/src/main/java/io/pslab/fragment/XYPlotFragment.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class XYPlotFragment extends Fragment {
2222
private Spinner spinnerChannelSelect1;
2323
private Spinner spinnerChannelSelect2;
2424
private CheckBox checkBoxXYPlot;
25-
private Button viewButton;
2625

2726
public static XYPlotFragment newInstance() {
2827
return new XYPlotFragment();
@@ -36,10 +35,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
3635
spinnerChannelSelect1 = v.findViewById(R.id.spinner_channel_select_xy1);
3736
spinnerChannelSelect2 = v.findViewById(R.id.spinner_channel_select_xy2);
3837
checkBoxXYPlot = v.findViewById(R.id.checkBox_enable_xy_xy);
39-
viewButton = v.findViewById(R.id.button_view_xy);
4038
spinnerChannelSelect1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
4139
@Override
4240
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
41+
((OscilloscopeActivity) getActivity()).xyPlotAxis1 = channels[position];
4342
if (((OscilloscopeActivity) getActivity()).isXYPlotSelected) {
4443
((OscilloscopeActivity) getActivity()).setXAxisLabel(channels[position]);
4544
((OscilloscopeActivity) getActivity()).xAxisLabelUnit.setText("(V)");
@@ -56,6 +55,7 @@ public void onNothingSelected(AdapterView<?> adapterView) {
5655

5756
@Override
5857
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
58+
((OscilloscopeActivity) getActivity()).xyPlotAxis2 = channels[position];
5959
if (((OscilloscopeActivity) getActivity()).isXYPlotSelected) {
6060
((OscilloscopeActivity) getActivity()).setLeftYAxisLabel(channels[position]);
6161
}
@@ -110,13 +110,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
110110
}
111111
});
112112

113-
viewButton.setOnClickListener(new View.OnClickListener() {
114-
@Override
115-
public void onClick(View v) {
116-
((OscilloscopeActivity) getActivity()).viewIsClicked = true;
117-
}
118-
});
119-
120113
return v;
121114
}
122115
}

app/src/main/res/layout/fragment_xyplot.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,4 @@
4848
app:layout_constraintBottom_toBottomOf="parent"
4949
app:layout_constraintLeft_toLeftOf="parent" />
5050

51-
<Button
52-
android:id="@+id/button_view_xy"
53-
android:layout_width="wrap_content"
54-
android:layout_height="wrap_content"
55-
android:layout_alignBottom="@+id/spinner_channel_select_xy2"
56-
android:layout_marginBottom="8dp"
57-
android:layout_marginEnd="8dp"
58-
android:layout_marginRight="8dp"
59-
android:layout_toEndOf="@+id/spinner_channel_select_xy2"
60-
android:layout_toRightOf="@+id/spinner_channel_select_xy2"
61-
android:layout_weight="1"
62-
android:background="@drawable/selector_button"
63-
android:text="@string/view"
64-
android:textColor="@color/white"
65-
android:textSize="@dimen/control_textsize_small"
66-
app:layout_constraintBottom_toBottomOf="parent"
67-
app:layout_constraintRight_toRightOf="parent" />
68-
6951
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)