Skip to content

Commit

Permalink
proto model + small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed May 22, 2024
1 parent fa8ab30 commit 1dea029
Show file tree
Hide file tree
Showing 38 changed files with 4,094 additions and 436 deletions.
30 changes: 30 additions & 0 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,33 @@ func (ea *EnumAttribute) ToFloat() (*FloatAttribute, error) {
func (ea *EnumAttribute) ToEnum() (*EnumAttribute, error) {
return ea, nil
}

type referenceableEntity interface {
EntityID() EntityID
}

type withRefs[R referenceableEntity] struct {
refs *set[EntityID, R]
}

func newWithRefs[R referenceableEntity]() *withRefs[R] {
return &withRefs[R]{
refs: newSet[EntityID, R](),
}
}

func (t *withRefs[R]) addRef(ref R) {
t.refs.add(ref.EntityID(), ref)
}

func (t *withRefs[R]) removeRef(refID EntityID) {
t.refs.remove(refID)
}

func (t *withRefs[R]) ReferenceCount() int {
return t.refs.size()
}

func (t *withRefs[R]) References() []R {
return t.refs.getValues()
}
36 changes: 18 additions & 18 deletions canid_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ type CANID uint32
type CANIDBuilderOpKind int

const (
// CANIDBuilderOpMessagePriority represents an operation
// CANIDBuilderOpKindMessagePriority represents an operation
// that involves the message priority.
CANIDBuilderOpMessagePriority CANIDBuilderOpKind = iota
// CANIDBuilderOpMessageID represents an operation
CANIDBuilderOpKindMessagePriority CANIDBuilderOpKind = iota
// CANIDBuilderOpKindMessageID represents an operation
// that involves the message id.
CANIDBuilderOpMessageID
// CANIDBuilderOpNodeID represents an operation
CANIDBuilderOpKindMessageID
// CANIDBuilderOpKindNodeID represents an operation
// that involves the node id.
CANIDBuilderOpNodeID
CANIDBuilderOpKindNodeID
)

func (bok CANIDBuilderOpKind) String() string {
switch bok {
case CANIDBuilderOpMessagePriority:
case CANIDBuilderOpKindMessagePriority:
return "message-priority"
case CANIDBuilderOpMessageID:
case CANIDBuilderOpKindMessageID:
return "message-id"
case CANIDBuilderOpNodeID:
case CANIDBuilderOpKindNodeID:
return "node-id"
default:
return "unknown"
Expand Down Expand Up @@ -73,16 +73,16 @@ func (bo *CANIDBuilderOp) Len() int {
// the CAN-ID of the messages within a [Bus].
type CANIDBuilder struct {
*entity
*withTemplateRefs[*Bus]
*withRefs[*Bus]

operations []*CANIDBuilderOp
}

// NewCANIDBuilder creates a new [CANIDBuilder] with the given name.
func NewCANIDBuilder(name string) *CANIDBuilder {
return &CANIDBuilder{
entity: newEntity(name),
withTemplateRefs: newWithTemplateRefs[*Bus](),
entity: newEntity(name),
withRefs: newWithRefs[*Bus](),

operations: []*CANIDBuilderOp{},
}
Expand Down Expand Up @@ -117,11 +117,11 @@ func (b *CANIDBuilder) Calculate(messagePriority MessagePriority, messageID Mess
for _, op := range b.operations {
tmpVal := uint32(0)
switch op.kind {
case CANIDBuilderOpMessagePriority:
case CANIDBuilderOpKindMessagePriority:
tmpVal = uint32(messagePriority)
case CANIDBuilderOpMessageID:
case CANIDBuilderOpKindMessageID:
tmpVal = uint32(messageID)
case CANIDBuilderOpNodeID:
case CANIDBuilderOpKindNodeID:
tmpVal = uint32(nodeID)
}

Expand All @@ -139,7 +139,7 @@ func (b *CANIDBuilder) Calculate(messagePriority MessagePriority, messageID Mess
// The length of the operation is fixed (2 bits).
func (b *CANIDBuilder) UseMessagePriority(from int) *CANIDBuilder {
b.operations = append(b.operations, &CANIDBuilderOp{
kind: CANIDBuilderOpMessagePriority,
kind: CANIDBuilderOpKindMessagePriority,
from: from,
len: 2,
})
Expand All @@ -149,7 +149,7 @@ func (b *CANIDBuilder) UseMessagePriority(from int) *CANIDBuilder {
// UseMessageID adds an operation that involves the message id from the given index and length.
func (b *CANIDBuilder) UseMessageID(from, len int) *CANIDBuilder {
b.operations = append(b.operations, &CANIDBuilderOp{
kind: CANIDBuilderOpMessageID,
kind: CANIDBuilderOpKindMessageID,
from: from,
len: len,
})
Expand All @@ -159,7 +159,7 @@ func (b *CANIDBuilder) UseMessageID(from, len int) *CANIDBuilder {
// UseNodeID adds an operation that involves the node id from the given index and length.
func (b *CANIDBuilder) UseNodeID(from, len int) *CANIDBuilder {
b.operations = append(b.operations, &CANIDBuilderOp{
kind: CANIDBuilderOpNodeID,
kind: CANIDBuilderOpKindNodeID,
from: from,
len: len,
})
Expand Down
8 changes: 4 additions & 4 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ func (e *exporter) exportStandardSignal(stdSig *StandardSignal, dbcSig *dbc.Sign
dbcSig.ValueType = dbc.SignalUnsigned
}

dbcSig.Min = stdSig.min
dbcSig.Max = stdSig.max
dbcSig.Offset = stdSig.offset
dbcSig.Factor = stdSig.scale
dbcSig.Min = stdSig.typ.min
dbcSig.Max = stdSig.typ.max
dbcSig.Offset = stdSig.typ.offset
dbcSig.Factor = stdSig.typ.scale

unit := stdSig.unit
if unit != nil {
Expand Down
10 changes: 6 additions & 4 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ func (i *importer) importSignal(dbcSig *dbc.Signal, dbcMsgID uint32) (Signal, er
if err != nil {
return nil, i.errorf(dbcSig, err)
}

tmpSigType.SetMin(dbcSig.Min)
tmpSigType.SetMax(dbcSig.Max)
tmpSigType.SetScale(dbcSig.Factor)
tmpSigType.SetOffset(dbcSig.Offset)

sigType = tmpSigType
}

Expand All @@ -623,10 +629,6 @@ func (i *importer) importSignal(dbcSig *dbc.Signal, dbcMsgID uint32) (Signal, er
return nil, i.errorf(dbcSig, err)
}

if err := stdSig.SetPhysicalValues(dbcSig.Min, dbcSig.Max, dbcSig.Offset, dbcSig.Factor); err != nil {
return nil, i.errorf(dbcSig, err)
}

if dbcSig.Unit != "" {
stdSig.SetUnit(NewSignalUnit(fmt.Sprintf("%s_Unit", sigName), SignalUnitKindCustom, dbcSig.Unit))
}
Expand Down
8 changes: 4 additions & 4 deletions md_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ func (e *mdExporter) exportSignal(sig Signal) {
}

func (e *mdExporter) exportStandardSignal(stdSig *StandardSignal) {
e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.min))
e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.typ.min))

e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.max))
e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.typ.max))

e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.offset))
e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.typ.offset))

e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.scale))
e.sigTableRow = append(e.sigTableRow, fmt.Sprintf("%g", stdSig.typ.scale))

unitSymbol := "-"
if stdSig.unit != nil {
Expand Down
11 changes: 3 additions & 8 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type MessageID uint32
// MessagePriority rappresents the priority of a [Message].
// The priorities are very high, high, medium, and low.
// The higher priority has the value 0 and the lower has 3.
type MessagePriority uint
type MessagePriority uint32

const (
// MessagePriorityVeryHigh defines a very high priority.
Expand Down Expand Up @@ -74,7 +74,6 @@ const (
type Message struct {
*attributeEntity

// senderNode *Node
senderNodeInt *NodeInterface

signals *set[EntityID, Signal]
Expand All @@ -87,9 +86,7 @@ type Message struct {
id MessageID
staticCANID CANID
hasStaticCANID bool
// id MessageCANID
// isStaticID bool
// idGenFn MessageCANIDGeneratorFn

priority MessagePriority
byteOrder MessageByteOrder

Expand All @@ -107,7 +104,6 @@ func NewMessage(name string, id MessageID, sizeByte int) *Message {
return &Message{
attributeEntity: newAttributeEntity(name, AttributeRefKindMessage),

// senderNode: nil,
senderNodeInt: nil,

signals: newSet[EntityID, Signal](),
Expand All @@ -120,8 +116,7 @@ func NewMessage(name string, id MessageID, sizeByte int) *Message {
id: id,
staticCANID: 0,
hasStaticCANID: false,
// isStaticID: false,
// idGenFn: defMsgIDGenFn,

priority: MessagePriorityVeryHigh,
byteOrder: MessageByteOrderLittleEndian,

Expand Down
Loading

0 comments on commit 1dea029

Please sign in to comment.