Skip to content

Commit 93c932a

Browse files
authored
Merge pull request #1824 from neel1998/all_channel_logging
Fix #1822 logging for all channels added
2 parents 413d03c + 4d130d6 commit 93c932a

File tree

1 file changed

+40
-130
lines changed

1 file changed

+40
-130
lines changed

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

Lines changed: 40 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ public class OscilloscopeActivity extends AppCompatActivity implements View.OnCl
194194
private long recordPeriod = 100;
195195
private String oscilloscopeCSVHeader = "Timestamp,DateTime,Channel,xData,yData,Timebase,lat,lon";
196196
private String loggingXdata = "";
197-
private String loggingYdata1 = "";
198-
private String loggingYdata2 = "";
199197
private final String KEY_LOG = "has_log";
200198
private final String DATA_BLOCK = "data_block";
201199
private int currentPosition = 0;
@@ -206,6 +204,7 @@ public class OscilloscopeActivity extends AppCompatActivity implements View.OnCl
206204
public boolean isPlaybackFourierChecked = false;
207205
private HashMap<String, Integer> channelIndexMap;
208206
private Integer[] channelColors = {Color.CYAN, Color.GREEN, Color.WHITE, Color.MAGENTA};
207+
private String[] loggingYdata = new String[4];
209208

210209
private enum CHANNEL {CH1, CH2, CH3, MIC}
211210

@@ -652,122 +651,57 @@ public void run() {
652651
try {
653652
if (currentPosition < recordedOscilloscopeData.size()) {
654653
OscilloscopeData data = recordedOscilloscopeData.get(currentPosition);
655-
if (data.getMode() == 1) {
656-
currentPosition += 1;
657-
ArrayList<Entry> entries = new ArrayList<>();
654+
int mode = data.getMode();
655+
List<ILineDataSet> dataSets = new ArrayList<>();
656+
ArrayList<ArrayList<Entry>> entries = new ArrayList<>();
657+
for (int i = 0; i < mode; i ++) {
658+
data = recordedOscilloscopeData.get(currentPosition);
659+
entries.add(new ArrayList<>());
658660
String[] xData = data.getDataX().split(" ");
659661
String[] yData = data.getDataY().split(" ");
660-
661662
if (!isPlaybackFourierChecked) {
662663
int n = Math.min(xData.length, yData.length);
663-
for (int i = 0; i < n; i++) {
664-
if (xData[i].length() > 0 && yData[i].length() > 0) {
665-
entries.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData[i])));
664+
for (int j = 0; j < n; j++) {
665+
if (xData[j].length() > 0 && yData[j].length() > 0) {
666+
entries.get(i).add(new Entry(Float.valueOf(xData[j]), Float.valueOf(yData[j])));
666667
}
667668
}
668669
setLeftYAxisScale(16f, -16f);
669670
setRightYAxisScale(16f, -16f);
670671
setXAxisScale(data.getTimebase());
671672
} else {
672673
Complex[] yComplex = new Complex[yData.length];
673-
for (int i = 0; i < yData.length; i++) {
674-
yComplex[i] = Complex.valueOf(Double.valueOf(yData[i]));
674+
for (int j = 0; j < yData.length; j++) {
675+
yComplex[j] = Complex.valueOf(Double.valueOf(yData[j]));
675676
}
676677
Complex[] fftOut = fft(yComplex);
677678
int n = fftOut.length;
678679
double mA = 0;
679680
double factor = samples * timeGap * 1e-3;
680681
double mF = (n / 2 - 1) / factor;
681-
for (int i = 0; i < n / 2; i++) {
682-
float y = (float) fftOut[i].abs() / samples;
682+
for (int j = 0; j < n / 2; j++) {
683+
float y = (float) fftOut[j].abs() / samples;
683684
if (y > mA) {
684685
mA = y;
685686
}
686-
entries.add(new Entry((float) (i / factor), y));
687+
entries.get(i).add(new Entry((float) (j / factor), y));
687688
}
688689
setLeftYAxisScale(mA, 0);
689690
setRightYAxisScale(mA, 0);
690691
setXAxisScale(mF);
691692
}
692-
LineDataSet dataSet = new LineDataSet(entries, data.getChannel());
693-
LineData lineData = new LineData(dataSet);
693+
currentPosition ++;
694+
LineDataSet dataSet;
695+
dataSet = new LineDataSet(entries.get(i), data.getChannel());
694696
dataSet.setDrawCircles(false);
695-
mChart.setData(lineData);
696-
mChart.notifyDataSetChanged();
697-
mChart.invalidate();
698-
699-
((OscilloscopePlaybackFragment) playbackFragment).setTimeBase(String.valueOf(data.getTimebase()));
700-
} else if (data.getMode() == 2) {
701-
OscilloscopeData data2 = recordedOscilloscopeData.get(currentPosition + 1);
702-
currentPosition += 2;
703-
ArrayList<Entry> entries1 = new ArrayList<>();
704-
ArrayList<Entry> entries2 = new ArrayList<>();
705-
String[] xData = data.getDataX().split(" ");
706-
String[] yData1 = data.getDataY().split(" ");
707-
String[] yData2 = data2.getDataY().split(" ");
708-
709-
if (!isPlaybackFourierChecked) {
710-
int n = Math.min(xData.length, Math.min(yData1.length, yData2.length));
711-
for (int i = 0; i < n; i++) {
712-
if (xData[i].length() > 0 && yData1[i].length() > 0 && yData2[i].length() > 0) {
713-
entries1.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData1[i])));
714-
entries2.add(new Entry(Float.valueOf(xData[i]), Float.valueOf(yData2[i])));
715-
}
716-
}
717-
718-
setLeftYAxisScale(16f, -16f);
719-
setRightYAxisScale(16f, -16f);
720-
setXAxisScale(data.getTimebase());
721-
} else {
722-
Complex[] yComplex1 = new Complex[yData1.length];
723-
Complex[] yComplex2 = new Complex[yData2.length];
724-
for (int i = 0; i < Math.min(yData1.length, yData2.length); i++) {
725-
yComplex1[i] = Complex.valueOf(Double.valueOf(yData1[i]));
726-
yComplex2[i] = Complex.valueOf(Double.valueOf(yData2[i]));
727-
}
728-
Complex[] fftOut1 = fft(yComplex1);
729-
Complex[] fftOut2 = fft(yComplex2);
730-
int n = Math.min(fftOut1.length, fftOut2.length);
731-
double mA = 0;
732-
733-
float maxAmp1 = 0;
734-
float maxAmp2 = 0;
735-
double factor = samples * timeGap * 1e-3;
736-
double mF = (n / 2 - 1) / factor;
737-
for (int i = 0; i < n / 2; i++) {
738-
float y1 = (float) fftOut1[i].abs() / samples;
739-
if (y1 > maxAmp1) {
740-
maxAmp1 = y1;
741-
}
742-
entries1.add(new Entry((float) (i / factor), y1));
743-
float y2 = (float) fftOut2[i].abs() / samples;
744-
if (y2 > maxAmp2) {
745-
maxAmp2 = y2;
746-
}
747-
entries2.add(new Entry((float) (i / factor), y2));
748-
}
749-
mA = Math.max(maxAmp1, maxAmp2);
750-
setXAxisScale(mF);
751-
setLeftYAxisScale(mA, 0);
752-
setRightYAxisScale(mA, 0);
753-
}
754-
LineDataSet dataSet1 = new LineDataSet(entries1, data.getChannel());
755-
LineDataSet dataSet2 = new LineDataSet(entries2, data2.getChannel());
756-
dataSet1.setDrawCircles(false);
757-
dataSet2.setDrawCircles(false);
758-
dataSet2.setColor(Color.GREEN);
759-
dataSet2.setDrawCircles(false);
760-
List<ILineDataSet> dataSets = new ArrayList<>();
761-
dataSets.add(dataSet1);
762-
dataSets.add(dataSet2);
763-
764-
LineData lineData = new LineData(dataSets);
765-
mChart.setData(lineData);
766-
mChart.notifyDataSetChanged();
767-
mChart.invalidate();
768-
697+
dataSet.setColor(channelColors[i]);
698+
dataSets.add(dataSet);
769699
((OscilloscopePlaybackFragment) playbackFragment).setTimeBase(String.valueOf(data.getTimebase()));
770700
}
701+
LineData lineData = new LineData(dataSets);
702+
mChart.setData(lineData);
703+
mChart.notifyDataSetChanged();
704+
mChart.invalidate();
771705
} else {
772706
playbackTimer.cancel();
773707
playbackTimer = null;
@@ -792,31 +726,17 @@ public void pauseData() {
792726
playbackTimer = null;
793727
}
794728

795-
private void logSingleChannelData(String channel) {
796-
long timestamp = System.currentTimeMillis();
797-
if (loggingXdata.length() > 0 && loggingYdata1.length() > 0) {
798-
recordSensorData(new OscilloscopeData(timestamp, block, 1, channel, loggingXdata, loggingYdata1, xAxisScale, lat, lon));
799-
String timeData = timestamp + "," + CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp));
800-
String locationData = lat + "," + lon;
801-
String data = timeData + "," + channel + "," + loggingXdata + "," + loggingYdata1 + "," + xAxisScale + "," + locationData;
802-
csvLogger.writeCSVFile(data);
803-
}
804-
}
805-
806-
private void logTwoChannelData(String channel1, String channel2) {
729+
private void logChannelData(String[] channels) {
807730
long timestamp = System.currentTimeMillis();
808-
if (loggingXdata.length() > 0 && loggingYdata1.length() > 0 && loggingYdata2.length() > 0) {
809-
recordSensorData(new OscilloscopeData(timestamp, block, 2, channel1, loggingXdata, loggingYdata1, xAxisScale, lat, lon));
810-
recordSensorData(new OscilloscopeData(timestamp + 1, block, 2, channel2, loggingXdata, loggingYdata2, xAxisScale, lat, lon));
811-
String timeData = timestamp + "," + CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp));
812-
String locationData = lat + "," + lon;
813-
String data = timeData + "," + channel1 + "," + loggingXdata + "," + loggingYdata1 + "," + xAxisScale + "," + locationData;
814-
csvLogger.writeCSVFile(data);
815-
data = timeData + "," + channel2 + "," + loggingXdata + "," + loggingYdata2 + "," + xAxisScale + "," + locationData;
731+
int noOfChannels = channels.length;
732+
String timeData = timestamp + "," + CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp));
733+
String locationData = lat + "," + lon;
734+
for (int i = 0; i < noOfChannels; i ++) {
735+
recordSensorData(new OscilloscopeData(timestamp + i, block, noOfChannels, channels[i], loggingXdata, loggingYdata[i], xAxisScale, lat, lon));
736+
String data = timeData + "," + channels[i] + "," + loggingXdata + "," + loggingYdata[i] + "," + xAxisScale + "," + locationData;
816737
csvLogger.writeCSVFile(data);
817738
}
818739
}
819-
820740
@Override
821741
protected void onResume() {
822742
super.onResume();
@@ -1121,26 +1041,16 @@ protected Void doInBackground(String... channels) {
11211041
}
11221042

11231043
if (isRecording) {
1124-
if (noOfChannels == 1) {
1125-
loggingXdata = String.join(" ", xDataString);
1126-
loggingYdata1 = String.join(" ", yDataString.get(0));
1127-
runOnUiThread(new Runnable() {
1128-
@Override
1129-
public void run() {
1130-
logSingleChannelData(paramsChannels[0]);
1131-
}
1132-
});
1133-
} else if (noOfChannels == 2) {
1134-
loggingXdata = String.join(" ", xDataString);
1135-
loggingYdata1 = String.join(" ", yDataString.get(0));
1136-
loggingYdata2 = String.join(" ", yDataString.get(1));
1137-
runOnUiThread(new Runnable() {
1138-
@Override
1139-
public void run() {
1140-
logTwoChannelData(paramsChannels[0], paramsChannels[1]);
1141-
}
1142-
});
1044+
loggingXdata = String.join(" ", xDataString);
1045+
for (int i = 0; i < noOfChannels; i ++) {
1046+
loggingYdata[i] = String.join(" ", yDataString.get(i));
11431047
}
1048+
runOnUiThread(new Runnable() {
1049+
@Override
1050+
public void run() {
1051+
logChannelData(paramsChannels);
1052+
}
1053+
});
11441054
}
11451055

11461056
} catch (NullPointerException e) {

0 commit comments

Comments
 (0)