Skip to content

Commit 047b82f

Browse files
jshivNaveen Venkatesan
and
Naveen Venkatesan
authored
Assign byte array directly to big.Int for big and little endian signals (#2)
Co-authored-by: Naveen Venkatesan <nvenkatesan@rivian.com>
1 parent 4e93a88 commit 047b82f

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

payload.go

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package can
22

33
import (
44
"encoding/hex"
5-
"fmt"
65
"math/big"
76
)
87

@@ -265,51 +264,22 @@ func (p *Payload) SetBit(i uint16, value bool) {
265264
// return packed
266265
// }
267266

267+
// PackLittleEndian packs the byte array into a continuous little endian big.Int
268268
func (p *Payload) PackLittleEndian() *big.Int {
269269
if p.PackedLittleEndian == nil {
270-
// Initialize a big.Int called packed
271-
//var packed, _ = new(big.Int).SetString(strings.Repeat("00000000", int(p.Length)), 2)
272-
packed, _ := new(big.Int).SetString(CanBitsLittleEndian(p.Data), 2)
273-
274-
// for i := 0; i < int(p.Length); i++ {
275-
// //packed |= uint8(packed >> (i * 8))
276-
// fmt.Println(fmt.Sprintf("%08b", p.Data[i]))
277-
// }
278-
//packed.SetBytes(p.Data)
270+
packed := new(big.Int).SetBytes(reverse(p.Data))
279271
p.PackedLittleEndian = packed
280272
}
281273
return new(big.Int).Set(p.PackedLittleEndian)
282274
}
283275

284-
// CanBitsLittleEndian creates the zig zag pattern of bits feeding into a can.Data frame
285-
func CanBitsLittleEndian(bytes []byte) string {
286-
var bits string
287-
for _, n := range bytes {
288-
bn := fmt.Sprintf("%08b", n)
289-
290-
// Need to reverse the binary strings because of the definition of bit order
291-
bits += reverse(bn)
292-
}
293-
return reverse(bits)
294-
}
295-
296-
// CanBitsBigEndian creates the zig zag pattern of bits feeding into a can.Data frame
297-
func CanBitsBigEndian(bytes []byte) string {
298-
var bits string
299-
for _, n := range bytes {
300-
bn := fmt.Sprintf("%08b", n)
301-
bits += bn
302-
}
303-
return bits
304-
}
305-
306-
// function to reverse strings
307-
func reverse(s string) string {
308-
runes := []rune(s)
309-
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
310-
runes[i], runes[j] = runes[j], runes[i]
276+
// reverse byte array for little endian signals
277+
func reverse(data []byte) []byte {
278+
reversedArray := make([]byte, len(data))
279+
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
280+
reversedArray[i], reversedArray[j] = data[j], data[i]
311281
}
312-
return string(runes)
282+
return reversedArray
313283
}
314284

315285
// // PackBigEndian packs the data into a contiguous uint64 value for big-endian signals.
@@ -326,9 +296,10 @@ func reverse(s string) string {
326296
// return packed
327297
// }
328298

299+
// PackBigEndian packs the byte array into a continuous big endian big.Int
329300
func (p *Payload) PackBigEndian() *big.Int {
330301
if p.PackedBigEndian == nil {
331-
var packed, _ = new(big.Int).SetString(CanBitsBigEndian(p.Data), 2)
302+
var packed = new(big.Int).SetBytes(p.Data)
332303
p.PackedBigEndian = packed
333304
}
334305
return new(big.Int).Set(p.PackedBigEndian)

tests/decode_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,16 @@ func TestDecodeEACVariables(t *testing.T) {
203203

204204
for _, signal := range message.Signals {
205205
value := signal.UnmarshalPhysicalPayload(&payload)
206+
valueDesc, _ := signal.UnmarshalValueDescriptionPayload(&payload)
206207
name := signal.Name
207208

208209
if value != expectedMap[name].value {
209210
t.Errorf("signal[%s] value = %f ; expected: %f", name, value, expectedMap[name].value)
210211
}
212+
213+
if valueDesc != expectedMap[name].description {
214+
t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, expectedMap[name].description)
215+
}
211216
}
212217
}
213218

0 commit comments

Comments
 (0)