Skip to content

Commit

Permalink
Assign byte array directly to big.Int for big and little endian signa…
Browse files Browse the repository at this point in the history
…ls (#2)

Co-authored-by: Naveen Venkatesan <nvenkatesan@rivian.com>
  • Loading branch information
jshiv and Naveen Venkatesan authored Mar 2, 2021
1 parent 4e93a88 commit 047b82f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
49 changes: 10 additions & 39 deletions payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package can

import (
"encoding/hex"
"fmt"
"math/big"
)

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

// PackLittleEndian packs the byte array into a continuous little endian big.Int
func (p *Payload) PackLittleEndian() *big.Int {
if p.PackedLittleEndian == nil {
// Initialize a big.Int called packed
//var packed, _ = new(big.Int).SetString(strings.Repeat("00000000", int(p.Length)), 2)
packed, _ := new(big.Int).SetString(CanBitsLittleEndian(p.Data), 2)

// for i := 0; i < int(p.Length); i++ {
// //packed |= uint8(packed >> (i * 8))
// fmt.Println(fmt.Sprintf("%08b", p.Data[i]))
// }
//packed.SetBytes(p.Data)
packed := new(big.Int).SetBytes(reverse(p.Data))
p.PackedLittleEndian = packed
}
return new(big.Int).Set(p.PackedLittleEndian)
}

// CanBitsLittleEndian creates the zig zag pattern of bits feeding into a can.Data frame
func CanBitsLittleEndian(bytes []byte) string {
var bits string
for _, n := range bytes {
bn := fmt.Sprintf("%08b", n)

// Need to reverse the binary strings because of the definition of bit order
bits += reverse(bn)
}
return reverse(bits)
}

// CanBitsBigEndian creates the zig zag pattern of bits feeding into a can.Data frame
func CanBitsBigEndian(bytes []byte) string {
var bits string
for _, n := range bytes {
bn := fmt.Sprintf("%08b", n)
bits += bn
}
return bits
}

// function to reverse strings
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
// reverse byte array for little endian signals
func reverse(data []byte) []byte {
reversedArray := make([]byte, len(data))
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
reversedArray[i], reversedArray[j] = data[j], data[i]
}
return string(runes)
return reversedArray
}

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

// PackBigEndian packs the byte array into a continuous big endian big.Int
func (p *Payload) PackBigEndian() *big.Int {
if p.PackedBigEndian == nil {
var packed, _ = new(big.Int).SetString(CanBitsBigEndian(p.Data), 2)
var packed = new(big.Int).SetBytes(p.Data)
p.PackedBigEndian = packed
}
return new(big.Int).Set(p.PackedBigEndian)
Expand Down
5 changes: 5 additions & 0 deletions tests/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ func TestDecodeEACVariables(t *testing.T) {

for _, signal := range message.Signals {
value := signal.UnmarshalPhysicalPayload(&payload)
valueDesc, _ := signal.UnmarshalValueDescriptionPayload(&payload)
name := signal.Name

if value != expectedMap[name].value {
t.Errorf("signal[%s] value = %f ; expected: %f", name, value, expectedMap[name].value)
}

if valueDesc != expectedMap[name].description {
t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, expectedMap[name].description)
}
}
}

Expand Down

0 comments on commit 047b82f

Please sign in to comment.