Skip to content

Commit

Permalink
added order to signal types
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Apr 15, 2024
1 parent 95b57ff commit 657d8be
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
10 changes: 9 additions & 1 deletion adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ func handleDBCFile(net *Network, file *dbc.File) error {
case dbc.SignalUnsigned:
signed = false
}
tmpSigType, err := NewCustomSignalType(fmt.Sprintf("%s_type", dbcSig.Name), int(dbcSig.Size), signed, dbcSig.Min, dbcSig.Max)
order := SignalTypeOrderLittleEndian
switch dbcSig.ByteOrder {
case dbc.SignalLittleEndian:
order = SignalTypeOrderLittleEndian
case dbc.SignalBigEndian:
order = SignalTypeOrderBigEndian
}

tmpSigType, err := NewCustomSignalType(fmt.Sprintf("%s_type", dbcSig.Name), int(dbcSig.Size), signed, order, dbcSig.Min, dbcSig.Max)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func Test_Message_AppendSignal(t *testing.T) {

msg := NewMessage("msg_0", 8)

sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127)
sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, -128, 127)
sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127)
sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, SignalTypeOrderLittleEndian, -128, 127)

sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"}

Expand All @@ -41,7 +41,7 @@ func Test_Message_AppendSignal(t *testing.T) {

assert.NoError(msg.AppendSignal(sig4))

sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, -128, 127)
sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, SignalTypeOrderLittleEndian, -128, 127)
massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive)
assert.Error(msg.AppendSignal(massiveSig))

Expand All @@ -60,8 +60,8 @@ func Test_Message_InsertSignal(t *testing.T) {

msg := NewMessage("msg_0", 8)

sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127)
sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, -128, 127)
sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127)
sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, SignalTypeOrderLittleEndian, -128, 127)

sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"}

Expand All @@ -87,7 +87,7 @@ func Test_Message_InsertSignal(t *testing.T) {
assert.NoError(msg.InsertSignal(sig3, 8))
assert.NoError(msg.InsertSignal(sig4, 32))

sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, -128, 127)
sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, SignalTypeOrderLittleEndian, -128, 127)
massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive)
assert.Error(msg.InsertSignal(massiveSig, 0))

Expand All @@ -110,8 +110,8 @@ func Test_Message_RemoveSignal(t *testing.T) {

msg := NewMessage("msg_0", 8)

sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127)
sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, -128, 127)
sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127)
sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, SignalTypeOrderLittleEndian, -128, 127)

sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"}

Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_Message_CompactSignals(t *testing.T) {

msg := NewMessage("msg_0", 8)

sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127)
sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127)

sig0, _ := NewStandardSignal("sig_0", sigTypInt8)
sig1, _ := NewStandardSignal("sig_1", sigTypInt8)
Expand All @@ -170,7 +170,7 @@ func Test_Message_ShiftSignalLeft(t *testing.T) {

msg := NewMessage("message", 2)

sigTypeInt4, err := newSignalType("int4", SignalTypeKindInteger, 4, true, -8, 7)
sigTypeInt4, err := newSignalType("int4", SignalTypeKindInteger, 4, true, SignalTypeOrderLittleEndian, -8, 7)
assert.NoError(err)

sig0, err := NewStandardSignal("signal_0", sigTypeInt4)
Expand Down Expand Up @@ -206,7 +206,7 @@ func Test_Message_ShiftSignalRight(t *testing.T) {

msg := NewMessage("message", 2)

sigTypeInt4, _ := newSignalType("int4", SignalTypeKindInteger, 4, true, -8, 7)
sigTypeInt4, _ := newSignalType("int4", SignalTypeKindInteger, 4, true, SignalTypeOrderLittleEndian, -8, 7)

sig0, err := NewStandardSignal("signal_0", sigTypeInt4)
assert.NoError(err)
Expand Down
37 changes: 27 additions & 10 deletions signal_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@ const (
SignalTypeKindFloat SignalTypeKind = "signal_type-float"
)

// SignalTypeOrder represents the bit order of a [SignalType].
type SignalTypeOrder string

const (
// SignalTypeOrderLittleEndian defines a little endian signal type.
SignalTypeOrderLittleEndian SignalTypeOrder = "signal_type_order-little_endian"
// SignalTypeOrderBigEndian defines a big endian signal type.
SignalTypeOrderBigEndian SignalTypeOrder = "signal_type_order-big_endian"
)

// SignalType is the representation of a signal type.
type SignalType struct {
*entity

kind SignalTypeKind
size int
signed bool
order SignalTypeOrder
min float64
max float64
}

func newSignalType(name string, kind SignalTypeKind, size int, signed bool, min, max float64) (*SignalType, error) {
func newSignalType(name string, kind SignalTypeKind, size int, signed bool, oredr SignalTypeOrder, min, max float64) (*SignalType, error) {
if size <= 0 {
return nil, fmt.Errorf("signal type size cannot be negative")
}
Expand All @@ -41,30 +52,31 @@ func newSignalType(name string, kind SignalTypeKind, size int, signed bool, min,
kind: kind,
size: size,
signed: signed,
order: oredr,
min: min,
max: max,
}, nil
}

// NewCustomSignalType creates a new [SignalType] of kind [SignalTypeKindCustom]
// with the given name, size, signed, and min/max values.
// with the given name, size, signed, order, and min/max values.
// It may return an error if the size is negative.
func NewCustomSignalType(name string, size int, signed bool, min, max float64) (*SignalType, error) {
return newSignalType(name, SignalTypeKindCustom, size, signed, min, max)
func NewCustomSignalType(name string, size int, signed bool, order SignalTypeOrder, min, max float64) (*SignalType, error) {
return newSignalType(name, SignalTypeKindCustom, size, signed, order, min, max)
}

// NewFlagSignalType creates a new [SignalType] of kind [SignalTypeKindFlag]
// with the given name.
// with the given name. The order is set to little endian.
func NewFlagSignalType(name string) *SignalType {
sig, err := newSignalType(name, SignalTypeKindFlag, 1, false, 0, 1)
sig, err := newSignalType(name, SignalTypeKindFlag, 1, false, SignalTypeOrderLittleEndian, 0, 1)
if err != nil {
panic(err)
}
return sig
}

// NewIntegerSignalType creates a new [SignalType] of kind [SignalTypeKindInteger]
// with the given name, size, and signed.
// with the given name, size, and signed. The order is set to little endian.
// It may return an error if the size is negative.
func NewIntegerSignalType(name string, size int, signed bool) (*SignalType, error) {
var min float64
Expand All @@ -81,16 +93,16 @@ func NewIntegerSignalType(name string, size int, signed bool) (*SignalType, erro
max = float64(tmp)
}

return newSignalType(name, SignalTypeKindInteger, size, signed, min, max)
return newSignalType(name, SignalTypeKindInteger, size, signed, SignalTypeOrderLittleEndian, min, max)
}

// NewFloatSignalType creates a new [SignalType] of kind [SignalTypeKindFloat]
// with the given name and size.
// with the given name and size. The order is set to little endian.
// It may return an error if the size is negative.
func NewFloatSignalType(name string, size int) (*SignalType, error) {
min := (1<<size - 1) - 1
max := -(1<<size - 1)
return newSignalType(name, SignalTypeKindFloat, size, true, float64(min), float64(max))
return newSignalType(name, SignalTypeKindFloat, size, true, SignalTypeOrderLittleEndian, float64(min), float64(max))
}

func (st *SignalType) stringify(b *strings.Builder, tabs int) {
Expand Down Expand Up @@ -123,6 +135,11 @@ func (st *SignalType) Signed() bool {
return st.signed
}

// Order returns the [SignalTypeOrder] of the [SignalType].
func (st *SignalType) Order() SignalTypeOrder {
return st.order
}

// Min returns the minimum value of the [SignalType].
func (st *SignalType) Min() float64 {
return st.min
Expand Down

0 comments on commit 657d8be

Please sign in to comment.