Created
May 17, 2018 12:40
-
-
Save pavelaizen/98dacc83ca80e36c4983c886870ff5b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.earlysense.sdk.rs232 | |
class SignalDataCollector() { | |
private var index = 0 | |
private var signalData = ByteArray(DATA_VAL_MAX_PACKET_SIZE) | |
var callback: ((signalData: ByteArray) -> Unit)? = null | |
/** | |
* Building chunks from the buffer, every chunk must be [DATA_VAL_MAX_PACKET_SIZE] | |
* @param buffer the buffer into which the data was read | |
* @param amount the total number of bytes read into the buffer | |
*/ | |
fun process(buffer: ByteArray, amount: Int) { | |
var read = 0 | |
while (read < amount) { | |
val copyLength = Math.min(DATA_VAL_MAX_PACKET_SIZE - index, amount - read) | |
System.arraycopy(buffer, read, signalData, index, copyLength) | |
index += copyLength | |
read += copyLength | |
if (index == DATA_VAL_MAX_PACKET_SIZE) { | |
callback?.let { it(signalData) } | |
recreate() | |
} | |
} | |
} | |
fun reset() = recreate() | |
private fun recreate() { | |
signalData = ByteArray(DATA_VAL_MAX_PACKET_SIZE) | |
index = 0 | |
} | |
companion object { | |
private const val DATA_VAL_MAX_PACKET_SIZE = 102 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment