diff --git a/app/src/main/java/io/pslab/communication/ScienceLab.java b/app/src/main/java/io/pslab/communication/ScienceLab.java index 07e94466b..4250a1785 100644 --- a/app/src/main/java/io/pslab/communication/ScienceLab.java +++ b/app/src/main/java/io/pslab/communication/ScienceLab.java @@ -446,52 +446,58 @@ public int[] oscilloscopeProgress() { } private boolean fetchData(int channelNumber) { - int samples = this.aChannels.get(channelNumber - 1).length; - if (channelNumber > this.channelsInBuffer) { - Log.v(TAG, "Channel Unavailable"); - return false; - } - Log.v("Samples", "" + samples); - Log.v("Data Splitting", "" + this.dataSplitting); - ArrayList listData = new ArrayList<>(); - try { - for (int i = 0; i < samples / this.dataSplitting; i++) { - mPacketHandler.sendByte(mCommandsProto.COMMON); - mPacketHandler.sendByte(mCommandsProto.RETRIEVE_BUFFER); - mPacketHandler.sendInt(this.aChannels.get(channelNumber - 1).bufferIndex + (i * this.dataSplitting)); - mPacketHandler.sendInt(this.dataSplitting); - byte[] data = new byte[this.dataSplitting * 2 + 1]; - mPacketHandler.read(data, this.dataSplitting * 2 + 1); - for (int j = 0; j < data.length - 1; j++) - listData.add((int) data[j] & 0xff); + int samples = this.aChannels.get(channelNumber - 1).length; + if (channelNumber > this.channelsInBuffer) { + Log.v(TAG, "Channel Unavailable"); + return false; + } + Log.v("Samples", "" + samples); + Log.v("Data Splitting", "" + this.dataSplitting); + ArrayList listData = new ArrayList<>(); + try { + for (int i = 0; i < samples / this.dataSplitting; i++) { + mPacketHandler.sendByte(mCommandsProto.COMMON); + mPacketHandler.sendByte(mCommandsProto.RETRIEVE_BUFFER); + mPacketHandler.sendInt(this.aChannels.get(channelNumber - 1).bufferIndex + (i * this.dataSplitting)); + mPacketHandler.sendInt(this.dataSplitting); + byte[] data = new byte[this.dataSplitting * 2 + 1]; + mPacketHandler.read(data, this.dataSplitting * 2 + 1); + for (int j = 0; j < data.length - 1; j++) { + listData.add((int) data[j] & 0xff); } + } - if ((samples % this.dataSplitting) != 0) { - mPacketHandler.sendByte(mCommandsProto.COMMON); - mPacketHandler.sendByte(mCommandsProto.RETRIEVE_BUFFER); - mPacketHandler.sendInt(this.aChannels.get(channelNumber - 1).bufferIndex + samples - samples % this.dataSplitting); - mPacketHandler.sendInt(samples % this.dataSplitting); - byte[] data = new byte[2 * (samples % this.dataSplitting) + 1]; - mPacketHandler.read(data, 2 * (samples % this.dataSplitting) + 1); - for (int j = 0; j < data.length - 1; j++) - listData.add((int) data[j] & 0xff); + if ((samples % this.dataSplitting) != 0) { + mPacketHandler.sendByte(mCommandsProto.COMMON); + mPacketHandler.sendByte(mCommandsProto.RETRIEVE_BUFFER); + mPacketHandler.sendInt(this.aChannels.get(channelNumber - 1).bufferIndex + samples - samples % this.dataSplitting); + mPacketHandler.sendInt(samples % this.dataSplitting); + byte[] data = new byte[2 * (samples % this.dataSplitting) + 1]; + mPacketHandler.read(data, 2 * (samples % this.dataSplitting) + 1); + for (int j = 0; j < data.length - 1; j++) { + listData.add((int) data[j] & 0xff); } - - } catch (IOException e) { - e.printStackTrace(); - return false; } - for (int i = 0; i < listData.size() / 2; i++) { + } catch (IOException e) { + e.printStackTrace(); + return false; + } + + for (int i = 0; i < listData.size() / 2; i++) { + if (i * 2 + 1 < listData.size()) { this.buffer[i] = (listData.get(i * 2)) | (listData.get(i * 2 + 1) << 8); while (this.buffer[i] > 1023) this.buffer[i] -= 1023; + } else { + Log.e(TAG, "Index out of bounds: " + (i * 2 + 1)); } + } - Log.v("RAW DATA:", Arrays.toString(Arrays.copyOfRange(buffer, 0, samples))); + Log.v("RAW DATA:", Arrays.toString(Arrays.copyOfRange(buffer, 0, samples))); - this.aChannels.get(channelNumber - 1).yAxis = this.aChannels.get(channelNumber - 1).fixValue(Arrays.copyOfRange(this.buffer, 0, samples)); - return true; - } + this.aChannels.get(channelNumber - 1).yAxis = this.aChannels.get(channelNumber - 1).fixValue(Arrays.copyOfRange(this.buffer, 0, samples)); + return true; +} /** * Configure trigger parameters for 10-bit capture commands @@ -2905,4 +2911,4 @@ public void disconnect() throws IOException { mCommunicationHandler.close(); PacketHandler.version = ""; } -} \ No newline at end of file +}