Skip to content

Commit d3250d8

Browse files
committed
update
1 parent 5060697 commit d3250d8

File tree

4 files changed

+69
-44
lines changed

4 files changed

+69
-44
lines changed

src/main/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function setSignal(data: { signal: string; value: number | number[] | str
5050
throw new Error(`Signal ${signalName} not found`)
5151
}
5252
if (typeof data.value === 'string') {
53-
updateSignalPhys(ss)
53+
updateSignalPhys(ss, db)
5454
} else {
5555
if (Array.isArray(data.value)) {
5656
throw new Error('Can not set array value')

src/renderer/src/database/dbc/calc.ts

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -102,46 +102,60 @@ export function updateSignalRaw(signal: Signal) {
102102
}
103103
}
104104
}
105-
export function updateSignalPhys(row: Signal) {
105+
export function updateSignalPhys(row: Signal, db: DBC) {
106106
if (row.physValue === undefined) return
107107

108108
if (row.values || row.valueTable) {
109-
// For enum values, directly set the phys value as raw value
110-
row.value = typeof row.physValue === 'number' ? row.physValue : 0
111-
} else {
112-
const physValue = typeof row.physValue === 'number' ? row.physValue : 0
113-
// Handle float value types
114-
if (row.valueType === 1) {
115-
// IEEE Float (single precision)
116-
const buffer = new ArrayBuffer(4)
117-
const view = new DataView(buffer)
118-
view.setFloat32(0, physValue, row.isLittleEndian) // true for little-endian
119-
row.value = view.getUint32(0, row.isLittleEndian)
120-
} else if (row.valueType === 2) {
121-
// IEEE Double (double precision)
122-
const buffer = new ArrayBuffer(8)
123-
const view = new DataView(buffer)
124-
view.setFloat64(0, physValue, row.isLittleEndian)
125-
// For simplicity, we're only using the lower 32 bits
126-
// This is a limitation as JavaScript numbers can't fully represent 64-bit integers
127-
row.value = view.getUint32(0, row.isLittleEndian)
128-
} else {
129-
// Clamp physical value to min/max if defined
130-
let clampedPhysValue = physValue
131-
if (row.minimum !== undefined && physValue < row.minimum) {
132-
clampedPhysValue = row.minimum
133-
} else if (row.maximum !== undefined && physValue > row.maximum) {
134-
clampedPhysValue = row.maximum
109+
// For enum values, comapre the phys value with the values or valueTable and set the raw value
110+
if (row.values) {
111+
const v = row.values.find((v) => v.value === row.physValue)
112+
if (v) {
113+
row.value = v.value
114+
return
135115
}
136-
137-
// Update physical value if it was clamped
138-
if (clampedPhysValue !== physValue) {
139-
row.physValue = clampedPhysValue
116+
} else if (row.valueTable) {
117+
const vt = Object.values(db.valueTables).find((vt) => vt.name === row.valueTable)
118+
if (vt) {
119+
const v = vt.values.find((v) => v.value === row.physValue)
120+
if (v) {
121+
row.value = v.value
122+
return
123+
}
140124
}
125+
}
126+
}
127+
const physValue = typeof row.physValue === 'number' ? row.physValue : 0
128+
// Handle float value types
129+
if (row.valueType === 1) {
130+
// IEEE Float (single precision)
131+
const buffer = new ArrayBuffer(4)
132+
const view = new DataView(buffer)
133+
view.setFloat32(0, physValue, row.isLittleEndian) // true for little-endian
134+
row.value = view.getUint32(0, row.isLittleEndian)
135+
} else if (row.valueType === 2) {
136+
// IEEE Double (double precision)
137+
const buffer = new ArrayBuffer(8)
138+
const view = new DataView(buffer)
139+
view.setFloat64(0, physValue, row.isLittleEndian)
140+
// For simplicity, we're only using the lower 32 bits
141+
// This is a limitation as JavaScript numbers can't fully represent 64-bit integers
142+
row.value = view.getUint32(0, row.isLittleEndian)
143+
} else {
144+
// Clamp physical value to min/max if defined
145+
let clampedPhysValue = physValue
146+
if (row.minimum !== undefined && physValue < row.minimum) {
147+
clampedPhysValue = row.minimum
148+
} else if (row.maximum !== undefined && physValue > row.maximum) {
149+
clampedPhysValue = row.maximum
150+
}
141151

142-
// Calculate and set raw value
143-
row.value = physToRaw(clampedPhysValue, row)
152+
// Update physical value if it was clamped
153+
if (clampedPhysValue !== physValue) {
154+
row.physValue = clampedPhysValue
144155
}
156+
157+
// Calculate and set raw value
158+
row.value = physToRaw(clampedPhysValue, row)
145159
}
146160
}
147161

src/renderer/src/views/uds/canisignale.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
v-model="row.physValue"
3333
size="small"
3434
style="width: 100%"
35-
@change="handlePhysValueChange(row)"
35+
@change="handlePhysValueChange(row, dataStore.database.can[props.database])"
3636
>
3737
<el-option
3838
v-for="item in row.values"
@@ -47,7 +47,7 @@
4747
v-model="row.physValue"
4848
size="small"
4949
style="width: 100%"
50-
@change="handlePhysValueChange(row)"
50+
@change="handlePhysValueChange(row, dataStore.database.can[props.database])"
5151
>
5252
<el-option
5353
v-for="item in getValues(row.valueTable)"
@@ -65,7 +65,7 @@
6565
style="width: 100%"
6666
size="small"
6767
controls-position="right"
68-
@change="handlePhysValueChange(row)"
68+
@change="handlePhysValueChange(row, dataStore.database.can[props.database])"
6969
/>
7070
</template>
7171
</template>
@@ -104,7 +104,7 @@ import { ref, computed, onMounted, nextTick } from 'vue'
104104
import { VxeGridProps } from 'vxe-table'
105105
import { VxeGrid } from 'vxe-table'
106106
import { Icon } from '@iconify/vue'
107-
import { Message, Signal } from '@r/database/dbc/dbcVisitor'
107+
import { DBC, Message, Signal } from '@r/database/dbc/dbcVisitor'
108108
import { useDataStore } from '@r/stores/data'
109109
import {
110110
getMessageData,
@@ -282,8 +282,8 @@ function handleRawValueChange(row: Signal) {
282282
}
283283
284284
// Physical value change handler
285-
function handlePhysValueChange(row: Signal) {
286-
updateSignalPhys(row)
285+
function handlePhysValueChange(row: Signal, db: DBC) {
286+
updateSignalPhys(row, db)
287287
if (message.value) {
288288
if (globalStart.value) {
289289
window.electron.ipcRenderer.send(

test/dbc/dbc.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ describe('DBC Parser Tests', () => {
6565

6666
s.physValue = 1
6767
s1.physValue = 1
68-
updateSignalPhys(s)
69-
updateSignalPhys(s1)
68+
updateSignalPhys(s, result)
69+
updateSignalPhys(s1, result)
7070

7171
const buf = getMessageData(result.messages[0x200])
7272
expect(buf).toEqual(Buffer.from([0x81, 0, 0, 0, 0, 0, 0, 0]))
@@ -76,8 +76,8 @@ describe('DBC Parser Tests', () => {
7676

7777
ns.physValue = 1
7878
ns1.physValue = -116
79-
updateSignalPhys(ns)
80-
updateSignalPhys(ns1)
79+
updateSignalPhys(ns, result)
80+
updateSignalPhys(ns1, result)
8181

8282
const buf1 = getMessageData(result.messages[12])
8383
expect(buf1).toEqual(Buffer.from([0x2, 0, 0, 0xc, 0, 0, 0, 0]))
@@ -283,5 +283,16 @@ describe('DBC Parser Tests', () => {
283283
const s = msg.signals['MCU_F_CrtSpd']
284284
expect(s.value).toBe(0x8002)
285285
expect(s.physValue).toBe(2)
286+
287+
const buf = getMessageData(msg)
288+
// The original buffer has 0xA0 (10100000) at the end, but the DBC defines no signal for bit 63 (MSB).
289+
// getMessageData reconstructs the buffer from signals, so the unmapped bit 63 becomes 0.
290+
// Result is 0x20 (00100000).
291+
expect(buf).toEqual(Buffer.from([0xb7, 0x1b, 0x80, 0x02, 0x80, 0, 0xb5, 0x20]))
292+
293+
s.physValue = 3
294+
updateSignalPhys(s, result)
295+
const buf1 = getMessageData(msg)
296+
expect(buf1).toEqual(Buffer.from([0xb7, 0x1b, 0x80, 0x03, 0x80, 0, 0xb5, 0x20]))
286297
})
287298
})

0 commit comments

Comments
 (0)