2022-06-20 14:29:02 -07:00
|
|
|
package com.rusefi.can.reader.dbc;
|
|
|
|
|
2022-06-20 18:49:31 -07:00
|
|
|
import com.rusefi.can.CANPacket;
|
|
|
|
|
2022-06-20 14:29:02 -07:00
|
|
|
public class DbcField {
|
|
|
|
private final String name;
|
|
|
|
private final int startOffset;
|
|
|
|
private final int length;
|
|
|
|
private final double mult;
|
|
|
|
|
|
|
|
public DbcField(String name, int startOffset, int length, double mult) {
|
|
|
|
this.name = name;
|
|
|
|
this.startOffset = startOffset;
|
|
|
|
this.length = length;
|
|
|
|
this.mult = mult;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getStartOffset() {
|
|
|
|
return startOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLength() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getMult() {
|
|
|
|
return mult;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "DbcField{" +
|
|
|
|
"name='" + name + '\'' +
|
|
|
|
", startOffset=" + startOffset +
|
|
|
|
", length=" + length +
|
|
|
|
", mult=" + mult +
|
|
|
|
'}';
|
|
|
|
}
|
2022-06-20 18:29:27 -07:00
|
|
|
|
|
|
|
public static int getBitIndex(byte[] data, int bitIndex, int bitWidth) {
|
2022-06-26 09:54:11 -07:00
|
|
|
if (bitIndex < 0)
|
|
|
|
throw new IllegalArgumentException("Huh? " + bitIndex + " " + bitWidth);
|
2022-06-20 18:29:27 -07:00
|
|
|
int byteIndex = bitIndex >> 3;
|
|
|
|
int shift = bitIndex - byteIndex * 8;
|
2022-06-26 09:54:11 -07:00
|
|
|
if (byteIndex >= data.length)
|
|
|
|
return 0;
|
2022-06-20 18:49:31 -07:00
|
|
|
int value = data[byteIndex] & 0xff;
|
2022-06-20 18:29:27 -07:00
|
|
|
if (shift + bitWidth > 8) {
|
2022-06-26 09:54:11 -07:00
|
|
|
if (byteIndex + 1 >= data.length)
|
|
|
|
return 0;
|
2022-06-20 18:29:27 -07:00
|
|
|
value = value + data[1 + byteIndex] * 256;
|
|
|
|
}
|
|
|
|
int mask = (1 << bitWidth) - 1;
|
|
|
|
return (value >> shift) & mask;
|
|
|
|
}
|
2022-06-20 18:49:31 -07:00
|
|
|
|
|
|
|
public double getValue(CANPacket packet) {
|
|
|
|
return getBitIndex(packet.getData(), startOffset, length) * mult;
|
|
|
|
}
|
2022-06-20 14:29:02 -07:00
|
|
|
}
|