Skip to content

Commit bc71432

Browse files
committed
Create Available PIDs Command
1 parent c86e07a commit bc71432

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

command/src/main/kotlin/com/github/eltonvs/obd/command/Response.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ data class ObdRawResponse(
4040
)
4141
}
4242

43-
private val processedValue by lazy { value.pipe(*valueProcessorPipeline) }
43+
val processedValue by lazy { value.pipe(*valueProcessorPipeline) }
4444

4545
val bufferedValue by lazy { processedValue.chunked(2) { it.toString().toInt(radix = 16) }.toIntArray() }
4646
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.eltonvs.obd.command.control
2+
3+
import com.github.eltonvs.obd.command.ObdCommand
4+
import com.github.eltonvs.obd.command.ObdRawResponse
5+
6+
7+
class AvailablePIDsCommand(val range: AvailablePIDsRanges) : ObdCommand() {
8+
override val tag = "AVAILABLE_COMMANDS_${range.name}"
9+
override val name = "Available Commands - ${range.displayName}"
10+
override val mode = "01"
11+
override val pid = range.pid
12+
13+
override val defaultUnit = ""
14+
override val handler = { it: ObdRawResponse ->
15+
parsePIDs(it.processedValue).joinToString(",") { "%02X".format(it) }
16+
}
17+
18+
private fun parsePIDs(rawValue: String): IntArray {
19+
val value = rawValue.toLong(radix = 16)
20+
val initialPID = range.pid.toInt(radix = 16)
21+
return (1..33).fold(listOf<Int>()) { acc, i ->
22+
if (getBit(value, i) == 1) acc + listOf(i + initialPID)
23+
else acc
24+
}.toIntArray()
25+
}
26+
27+
private fun getBit(number: Long, position: Int) = (number shr (32 - position) and 1).toInt()
28+
29+
enum class AvailablePIDsRanges(val displayName: String, internal val pid: String) {
30+
PIDS_01_TO_20("PIDs from 01 to 20", "00"),
31+
PIDS_21_TO_40("PIDs from 21 to 40", "20"),
32+
PIDS_41_TO_60("PIDs from 41 to 60", "40"),
33+
PIDS_61_TO_80("PIDs from 61 to 80", "60"),
34+
PIDS_81_TO_100("PIDs from 81 to 100", "80")
35+
}
36+
}

0 commit comments

Comments
 (0)