Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Created July 6, 2022 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reactormonk/23b9edf0aa37e491430f692cb580328e to your computer and use it in GitHub Desktop.
Save reactormonk/23b9edf0aa37e491430f692cb580328e to your computer and use it in GitHub Desktop.
enum class PICCPollInterval(val value: Byte) {
I250ms(0b00000000),
I500ms(0b00001000),
I1000ms(0b00010000),
I2500ms(0b00011000),;
companion object {
private val values = values()
fun bitMaskToEnum(bitMask: Byte) =
values.find {
it.value == bitMask and 0b00011000
}!!
}
}
enum class AutomaticPPICPolling(override val position: Byte): Positional {
AutomaticPolling(0),
TurnOffFieldIfNoPICC(1),
TurnOffFieldIfPPICInactive(2),
EnforceISO14443AP4(7),;
companion object {
private val values = AutomaticPPICPolling.values()
fun bitMaskToSet(bitMask: Byte): EnumSet<AutomaticPPICPolling> =
values.filterTo(EnumSet.noneOf(AutomaticPPICPolling::class.java)) {
val toBitMask = it.toBitMask()
bitMask and toBitMask != 0.toByte()
}
}
}
interface Positional {
abstract val position: Byte
fun toBitMask(): Byte = (1 shl position.toInt()).toByte()
}
data class PollingSettings(val polling: EnumSet<AutomaticPPICPolling>, val interval: PICCPollInterval) {
fun toBitMask(): Byte =
polling.toBitMask() or interval.value
companion object {
fun bitMaskToEnum(byte: Byte): PollingSettings {
val interval = PICCPollInterval.bitMaskToEnum(byte)
val polling = AutomaticPPICPolling.bitMaskToSet(byte)
return PollingSettings(polling, interval)
}
}
class BluetoothSupportTest : DescribeSpec({
describe("BluetoothSupport") {
forAll<PollingSettings> { setting ->
PollingSettings.bitMaskToEnum(setting.toBitMask()) == setting
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment