Made FlickerCodeStepsCalculator state free

This commit is contained in:
dankito 2020-08-13 14:51:04 +02:00
parent 8b6b12a755
commit 469e9c9dab
3 changed files with 59 additions and 43 deletions

View File

@ -0,0 +1,12 @@
package net.dankito.utils.multiplatform
open class ObjectHolder<T>(
var value: T
) {
override fun toString(): String {
return value?.toString() ?: "Value is null"
}
}

View File

@ -1,22 +1,15 @@
package net.dankito.banking.fints.tan package net.dankito.banking.fints.tan
import net.dankito.utils.multiplatform.ObjectHolder
open class FlickerCodeStepsCalculator(var code: String) {
var halfbyteid = 0
var clock = Bit.High
var bitarray = mutableListOf<MutableList<Bit>>()
val steps: List<Array<Bit>>
fun reset() { open class FlickerCodeStepsCalculator {
halfbyteid = 0
clock = Bit.High companion object {
}
val bits = mutableMapOf<Char, List<Bit>>()
init { init {
val bits = mutableMapOf<Char, List<Bit>>()
/* bitfield: clock, bits 2^1, 2^2, 2^3, 2^4 */ /* bitfield: clock, bits 2^1, 2^2, 2^3, 2^4 */
bits['0'] = listOf(Bit.Low, Bit.Low, Bit.Low, Bit.Low, Bit.Low) bits['0'] = listOf(Bit.Low, Bit.Low, Bit.Low, Bit.Low, Bit.Low)
bits['1'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.Low, Bit.Low) bits['1'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.Low, Bit.Low)
@ -34,9 +27,20 @@ open class FlickerCodeStepsCalculator(var code: String) {
bits['D'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.High, Bit.High) bits['D'] = listOf(Bit.Low, Bit.High, Bit.Low, Bit.High, Bit.High)
bits['E'] = listOf(Bit.Low, Bit.Low, Bit.High, Bit.High, Bit.High) bits['E'] = listOf(Bit.Low, Bit.Low, Bit.High, Bit.High, Bit.High)
bits['F'] = listOf(Bit.Low, Bit.High, Bit.High, Bit.High, Bit.High) bits['F'] = listOf(Bit.Low, Bit.High, Bit.High, Bit.High, Bit.High)
}
}
open fun calculateSteps(flickerCode: String): List<Array<Bit>> {
val halfbyteid = ObjectHolder(0)
val clock = ObjectHolder(Bit.High)
val bitarray = mutableListOf<MutableList<Bit>>()
/* prepend synchronization identifier */ /* prepend synchronization identifier */
code = "0FFF" + code var code = "0FFF" + flickerCode
if (code.length % 2 != 0) { if (code.length % 2 != 0) {
code += "F" code += "F"
} }
@ -49,24 +53,24 @@ open class FlickerCodeStepsCalculator(var code: String) {
val steps = mutableListOf<Array<Bit>>() val steps = mutableListOf<Array<Bit>>()
do { do {
steps.add(step()) steps.add(calculateStep(halfbyteid, clock, bitarray))
} while (halfbyteid > 0 || clock == Bit.Low) } while (halfbyteid.value > 0 || clock.value == Bit.Low)
this.steps = steps return steps
} }
fun step(): Array<Bit> { protected open fun calculateStep(halfbyteid: ObjectHolder<Int>, clock: ObjectHolder<Bit>, bitarray: MutableList<MutableList<Bit>>): Array<Bit> {
bitarray[halfbyteid][0] = clock bitarray[halfbyteid.value][0] = clock.value
val stepBits = Array(5) { index -> bitarray[halfbyteid][index] } val stepBits = Array(5) { index -> bitarray[halfbyteid.value][index] }
clock = clock.invert() clock.value = clock.value.invert()
if (clock == Bit.High) { if (clock.value == Bit.High) {
halfbyteid++ halfbyteid.value++
if (halfbyteid >= bitarray.size) { if (halfbyteid.value >= bitarray.size) {
halfbyteid = 0 halfbyteid.value = 0
} }
} }

View File

@ -39,7 +39,7 @@ open class FlickerCodeAnimator {
currentFrequency = frequency currentFrequency = frequency
animationJob = GlobalScope.launch(Dispatchers.Default) { animationJob = GlobalScope.launch(Dispatchers.Default) {
val steps = FlickerCodeStepsCalculator(flickerCode.parsedDataSet).steps val steps = FlickerCodeStepsCalculator().calculateSteps(flickerCode.parsedDataSet)
calculateAnimation(steps, showStep) calculateAnimation(steps, showStep)
} }