Skip to content

Commit

Permalink
finalized message for incoming bytes, added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BK1031 committed Feb 7, 2025
1 parent 01d2fc8 commit 5192e6d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
19 changes: 15 additions & 4 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (m Message) Size() int {
}

// FillFromBytes fills the Fields of a Message with the provided byte array.
// It decodes the bytes into integer values and stores them in the Value of each Field.
// It returns an error if the data length does not match the size of the Message.
func (m Message) FillFromBytes(data []byte) error {
if len(data) != m.Size() {
Expand All @@ -29,19 +30,24 @@ func (m Message) FillFromBytes(data []byte) error {
for i, field := range m {
field.Bytes = data[counter : counter+field.Size]
counter += field.Size
m[i] = field
m[i] = field.Decode()
}
return nil
}

// FillFromInts fills the Fields of a Message with the provided integers.
// It encodes the integers into bytes and stores them in the Bytes of each Field.
// It returns an error if the number of integers does not match the number of Fields in the Message.
func (m Message) FillFromInts(ints []int) error {
if len(ints) != m.Length() {
return fmt.Errorf("invalid ints length, expected %d, got %d", m.Length(), len(ints))
}
for i, field := range m {
field.Value = ints[i]
field, err := field.Encode()
if err != nil {
return err
}
m[i] = field
}
return nil
Expand Down Expand Up @@ -122,9 +128,14 @@ func (f Field) Encode() (Field, error) {
return f, err
}

// CheckBit takes a Field object and a bit position, and returns true if the bit at the given position is set to 1, otherwise false.
func (f Field) CheckBit(bit int) bool {
return (f.Bytes[0] & (1 << bit)) != 0
// CheckBit takes a Field object and a bit position, and returns the integer value of the bit at the given position (0 or 1).
func (f Field) CheckBit(bit int) int {
byteIndex := bit / 8
bitPosition := bit % 8
if byteIndex >= len(f.Bytes) {
return 0
}
return int(f.Bytes[byteIndex] & (1 << bitPosition))
}

// ExportSignals takes a Field object and exports it as an array of signals.
Expand Down
91 changes: 91 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,94 @@ func TestTimestamp(t *testing.T) {
fmt.Println(now)
fmt.Printf("Size of timestamp: %d bytes\n", unsafe.Sizeof(now))
}

func TestMessage(t *testing.T) {
ecuStatusMessage := Message{
NewField("ecu_state", 1, Unsigned, BigEndian, nil),
NewField("ecu_status_flags", 3, Unsigned, BigEndian, func(f Field) []Signal {
signals := []Signal{}
bitMap := []string{
"ecu_status_acu",
"ecu_status_inv_one",
"ecu_status_inv_two",
"ecu_status_inv_three",
"ecu_status_inv_four",
"ecu_status_fan_one",
"ecu_status_fan_two",
"ecu_status_fan_three",
"ecu_status_fan_four",
"ecu_status_fan_five",
"ecu_status_fan_six",
"ecu_status_fan_seven",
"ecu_status_fan_eight",
"ecu_status_dash",
"ecu_status_steering",
}
for i := 0; i < len(bitMap); i++ {
signals = append(signals, Signal{
Name: bitMap[i],
Value: float64(f.CheckBit(i)),
RawValue: f.CheckBit(i),
})
}
return signals
}),
NewField("ecu_maps", 1, Unsigned, BigEndian, func(f Field) []Signal {
signals := []Signal{}
signals = append(signals, Signal{
Name: "ecu_power_level",
Value: float64((f.Value >> 4) & 0x0F),
RawValue: (f.Value >> 4) & 0x0F,
})
signals = append(signals, Signal{
Name: "ecu_torque_map",
Value: float64(f.Value & 0x0F),
RawValue: f.Value & 0x0F,
})
return signals
}),
NewField("ecu_max_cell_temp", 1, Unsigned, BigEndian, func(f Field) []Signal {
signals := []Signal{}
signals = append(signals, Signal{
Name: "ecu_max_cell_temp",
Value: float64(f.Value) * 0.25,
RawValue: f.Value,
})
return signals
}),
NewField("ecu_acu_state_of_charge", 1, Unsigned, BigEndian, func(f Field) []Signal {
signals := []Signal{}
signals = append(signals, Signal{
Name: "ecu_acu_state_of_charge",
Value: float64(f.Value) * 20 / 51,
RawValue: f.Value,
})
return signals
}),
NewField("ecu_glv_state_of_charge", 1, Unsigned, BigEndian, func(f Field) []Signal {
signals := []Signal{}
signals = append(signals, Signal{
Name: "ecu_glv_state_of_charge",
Value: float64(f.Value) * 20 / 51,
RawValue: f.Value,
})
return signals
}),
}
t.Run("Invalid byte length", func(t *testing.T) {
err := ecuStatusMessage.FillFromBytes([]byte{0, 0})
if err == nil {
t.Errorf("Expected error, got nil")
}
})
t.Run("Valid byte length", func(t *testing.T) {
err := ecuStatusMessage.FillFromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
signals := ecuStatusMessage.ExportSignals()
for _, signal := range signals {
fmt.Printf("%s: %f\n", signal.Name, signal.Value)
}
})
}

0 comments on commit 5192e6d

Please sign in to comment.