Skip to content

Commit

Permalink
new mux signal backbone
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Apr 21, 2024
1 parent dff26a4 commit 5350fce
Show file tree
Hide file tree
Showing 17 changed files with 1,915 additions and 1,021 deletions.
50 changes: 25 additions & 25 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,11 @@ func (a *adapter) adaptSignal(sig Signal, receiverNames ...string) {
a.currDBCMsg.Signals = append(a.currDBCMsg.Signals, dbcSig)

case SignalKindMultiplexer:
muxSig, err := sig.ToMultiplexer()
if err != nil {
panic(err)
}
a.adaptMultiplexerSignal(muxSig)
// muxSig, err := sig.ToMultiplexer()
// if err != nil {
// panic(err)
// }
// a.adaptMultiplexerSignal(muxSig)
}

}
Expand Down Expand Up @@ -438,31 +438,31 @@ func (a *adapter) adaptEnumSignal(enumSig *EnumSignal, dbcSig *dbc.Signal) {
dbcSig.Factor = 1
}

func (a *adapter) adaptMultiplexerSignal(muxSig *MultiplexerSignal) {
dbcMuxorSig := new(dbc.Signal)
// func (a *adapter) adaptMultiplexerSignal(muxSig *MultiplexerSignal1) {
// dbcMuxorSig := new(dbc.Signal)

dbcMuxorSig.Name = muxSig.Name()
// dbcMuxorSig.Name = muxSig.Name()

dbcMuxorSig.IsMultiplexor = true
// dbcMuxorSig.IsMultiplexor = true

dbcMuxorSig.Size = uint32(muxSig.GetSize())
dbcMuxorSig.StartBit = uint32(muxSig.GetStartBit())
// dbcMuxorSig.Size = uint32(muxSig.GetSize())
// dbcMuxorSig.StartBit = uint32(muxSig.GetStartBit())

dbcMuxorSig.ByteOrder = dbc.SignalLittleEndian
dbcMuxorSig.ValueType = dbc.SignalUnsigned
// dbcMuxorSig.ByteOrder = dbc.SignalLittleEndian
// dbcMuxorSig.ValueType = dbc.SignalUnsigned

selectValues := 1 << muxSig.SelectSize()
// selectValues := 1 << muxSig.SelectSize()

dbcMuxorSig.Factor = 1
dbcMuxorSig.Offset = 0
dbcMuxorSig.Min = 0
dbcMuxorSig.Max = float64(selectValues)
// dbcMuxorSig.Factor = 1
// dbcMuxorSig.Offset = 0
// dbcMuxorSig.Min = 0
// dbcMuxorSig.Max = float64(selectValues)

a.currDBCMsg.Signals = append(a.currDBCMsg.Signals, dbcMuxorSig)
// a.currDBCMsg.Signals = append(a.currDBCMsg.Signals, dbcMuxorSig)

for i := 0; i < selectValues; i++ {
for _, muxedSig := range muxSig.GetSelectedMuxSignals(i) {
a.adaptSignal(muxedSig)
}
}
}
// for i := 0; i < selectValues; i++ {
// for _, muxedSig := range muxSig.GetSelectedMuxSignals(i) {
// a.adaptSignal(muxedSig)
// }
// }
// }
4 changes: 2 additions & 2 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func newAttribute(name string, kind AttributeKind) *attribute {

kind: kind,

references: newSet[EntityID, *AttributeRef]("reference"),
references: newSet[EntityID, *AttributeRef](),
}
}

Expand Down Expand Up @@ -424,7 +424,7 @@ func NewEnumAttribute(name string, values ...string) (*EnumAttribute, error) {
return nil, fmt.Errorf("at least 1 value is required")
}

valSet := newSet[string, int]("values")
valSet := newSet[string, int]()
currIdx := 0
for _, val := range values {
if valSet.hasKey(val) {
Expand Down
12 changes: 6 additions & 6 deletions bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func NewBus(name string) *Bus {

parentNetwork: nil,

nodes: newSet[EntityID, *Node]("node"),
nodeNames: newSet[string, EntityID]("node name"),
nodeIDs: newSet[NodeID, EntityID]("node id"),
nodes: newSet[EntityID, *Node](),
nodeNames: newSet[string, EntityID](),
nodeIDs: newSet[NodeID, EntityID](),

baudrate: 0,
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (b *Bus) UpdateName(newName string) error {
}

if b.hasParentNetwork() {
if err := b.parentNetwork.busNames.verifyKey(newName); err != nil {
if err := b.parentNetwork.busNames.verifyKeyUnique(newName); err != nil {
return b.errorf(fmt.Errorf(`cannot update name to "%s" : %w`, newName, err))
}

Expand All @@ -115,11 +115,11 @@ func (b *Bus) ParentNetwork() *Network {
// AddNode adds the given [Node] to the [Bus].
// It may return an error if the node name or the node id is already used by the bus.
func (b *Bus) AddNode(node *Node) error {
if err := b.nodeNames.verifyKey(node.name); err != nil {
if err := b.nodeNames.verifyKeyUnique(node.name); err != nil {
return b.errorf(fmt.Errorf(`cannot add node "%s" : %w`, node.name, err))
}

if err := b.nodeIDs.verifyKey(node.id); err != nil {
if err := b.nodeIDs.verifyKeyUnique(node.id); err != nil {
return b.errorf(fmt.Errorf(`cannot add node "%s" : %w`, node.name, err))
}

Expand Down
2 changes: 1 addition & 1 deletion entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func newAttributeEntity(name string, attRefKind AttributeRefKind) *attributeEnti
return &attributeEntity{
entity: newEntity(name),

attributeValues: newSet[EntityID, *AttributeValue]("attribute value"),
attributeValues: newSet[EntityID, *AttributeValue](),
attRefKind: attRefKind,
}
}
Expand Down
95 changes: 95 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package acmelib

import (
"errors"
"fmt"
)

var (
ErrIsDuplicated = errors.New("is duplicated")
ErrNotFound = errors.New("not found")
ErrIsNegative = errors.New("is negative")
ErrOutOfBounds = errors.New("out of bounds")
ErrIsZero = errors.New("is zero")
)

type ArgumentError struct {
Name string
Err error
}

func (e *ArgumentError) Error() string {
return fmt.Sprintf("argument; name:%q : %v", e.Name, e.Err)
}

func (e *ArgumentError) Unwrap() error { return e.Err }

type SignalError struct {
EntityID EntityID
Name string
Err error
}

func (e *SignalError) Error() string {
return fmt.Sprintf("multiplexer signal; entity_id:%q, name:%q : %v", e.EntityID.String(), e.Name, e.Err)
}

func (e *SignalError) Unwrap() error { return e.Err }

type SignalNameError struct {
Name string
Err error
}

func (e *SignalNameError) Error() string {
return fmt.Sprintf("signal name %q : %v", e.Name, e.Err)
}

func (e *SignalNameError) Unwrap() error {
return e.Err
}

type GroupIDError struct {
GroupID int
Err error
}

func (e *GroupIDError) Error() string {
return fmt.Sprintf("group id %d : %v", e.GroupID, e.Err)
}

func (e *GroupIDError) Unwrap() error { return e.Err }

type InsertSignalError struct {
EntityID EntityID
Name string
StartBit int
Err error
}

func (e *InsertSignalError) Error() string {
return fmt.Sprintf("insert signal; entity_id:%q, name:%q, start_bit:%d : %v", e.EntityID.String(), e.Name, e.StartBit, e.Err)
}

func (e *InsertSignalError) Unwrap() error { return e.Err }

type RemoveSignalError struct {
EntityID EntityID
Err error
}

func (e *RemoveSignalError) Error() string {
return fmt.Sprintf("remove signal; entity_id:%q : %v", e.EntityID.String(), e.Err)
}

func (e *RemoveSignalError) Unwrap() error { return e.Err }

type ClearSignalGroupError struct {
Err error
}

func (e *ClearSignalGroupError) Error() string {
return fmt.Sprintf("clear signal group : %v", e.Err)
}

func (e *ClearSignalGroupError) Unwrap() error { return e.Err }
24 changes: 14 additions & 10 deletions examples/sc24/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ func info01DbgV() *acmelib.Message {
msg := acmelib.NewMessage("INFO_01_DbgV", 8)
msg.SetID(288)

muxSig, err := acmelib.NewMultiplexerSignal("BMS_eDbgV", 56, 8)
muxSig, err := acmelib.NewMultiplexerSignal("BMS_eDbgV", 256, 48)
panicErr(err)

selectValue := -1
groupID := -1
for i := 0; i < 255; i++ {
if i%3 == 0 {
selectValue++
tmpIdx := i % 3

if tmpIdx == 0 {
groupID++
}

sig, err := acmelib.NewStandardSignal(fmt.Sprintf("BMS_VDbgV%3d", i), float16Type)
Expand All @@ -103,7 +105,7 @@ func info01DbgV() *acmelib.Message {
sig.SetUnit(voltageUnit)
sig.SetDesc(fmt.Sprintf("Cell %d voltage.", i))

err = muxSig.AppendMuxSignal(selectValue, sig)
err = muxSig.InsertSignal(sig, tmpIdx*16, groupID)
panicErr(err)
}

Expand All @@ -116,13 +118,15 @@ func info02DbgT() *acmelib.Message {
msg := acmelib.NewMessage("INFO_01_DbgV", 8)
msg.SetID(289)

muxSig, err := acmelib.NewMultiplexerSignal("BMS_eDbgT", 55, 7)
muxSig, err := acmelib.NewMultiplexerSignal("BMS_eDbgT", 128, 48)
panicErr(err)

selectValue := -1
groupID := -1
for i := 0; i < 127; i++ {
if i%3 == 0 {
selectValue++
tmpIdx := i % 3

if tmpIdx == 0 {
groupID++
}

sig, err := acmelib.NewStandardSignal(fmt.Sprintf("BMS_TDbgT%3d", i), float16Type)
Expand All @@ -134,7 +138,7 @@ func info02DbgT() *acmelib.Message {

sig.AddAttributeValue(genSigStartVal, 23315)

err = muxSig.AppendMuxSignal(selectValue, sig)
err = muxSig.InsertSignal(sig, tmpIdx*16, groupID)
panicErr(err)
}

Expand Down
26 changes: 12 additions & 14 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func NewMessage(name string, sizeByte int) *Message {

senderNode: nil,

signals: newSet[EntityID, Signal]("signal"),
signalNames: newSet[string, EntityID]("signal name"),
signals: newSet[EntityID, Signal](),
signalNames: newSet[string, EntityID](),

signalPayload: newSignalPayload(sizeByte * 8),

Expand All @@ -114,7 +114,7 @@ func NewMessage(name string, sizeByte int) *Message {
delayTime: 0,
startDelayTime: 0,

receivers: newSet[EntityID, *Node]("receiver"),
receivers: newSet[EntityID, *Node](),
}
}

Expand Down Expand Up @@ -143,13 +143,13 @@ func (m *Message) errorf(err error) error {
}

// GetSignalParentKind always returns [SignalParentKindMessage].
// It can be used to check if the parent of a signal is a [Message] or a [MultiplexerSignal].
// It can be used to check if the parent of a signal is a [Message] or a [MultiplexerSignal1].
func (m *Message) GetSignalParentKind() SignalParentKind {
return SignalParentKindMessage
}

func (m *Message) verifySignalName(_ EntityID, name string) error {
return m.signalNames.verifyKey(name)
return m.signalNames.verifyKeyUnique(name)
}

func (m *Message) modifySignalName(sigID EntityID, newName string) error {
Expand Down Expand Up @@ -257,7 +257,7 @@ func (m *Message) UpdateName(newName string) error {
}

if m.hasSenderNode() {
if err := m.senderNode.messageNames.verifyKey(newName); err != nil {
if err := m.senderNode.messageNames.verifyKeyUnique(newName); err != nil {
return m.errorf(fmt.Errorf(`cannot update name to "%s" : %w`, newName, err))
}
m.senderNode.modifyMessageName(m.entityID, newName)
Expand Down Expand Up @@ -303,7 +303,7 @@ func (m *Message) InsertSignal(signal Signal, startBit int) error {
return m.errorf(fmt.Errorf(`cannot insert signal "%s" : %w`, signal.Name(), err))
}

if err := m.signalPayload.insert(signal, startBit); err != nil {
if err := m.signalPayload.verifyAndInsert(signal, startBit); err != nil {
return m.errorf(err)
}

Expand All @@ -329,11 +329,9 @@ func (m *Message) RemoveSignal(signalEntityID EntityID) error {
panic(err)
}

for _, muxSignals := range muxSig.MuxSignals() {
for _, tmpSig := range muxSignals {
m.signals.remove(tmpSig.EntityID())
m.signalNames.remove(tmpSig.Name())
}
for _, tmpSig := range muxSig.signals.entries() {
m.signals.remove(tmpSig.EntityID())
m.signalNames.remove(tmpSig.Name())
}
}

Expand Down Expand Up @@ -367,7 +365,7 @@ func (m *Message) ShiftSignalLeft(signalEntityID EntityID, amount int) int {
return 0
}

return m.signalPayload.shiftLeft(sig, amount)
return m.signalPayload.shiftLeft(sig.EntityID(), amount)
}

// ShiftSignalRight shifts the signal with the given entity id right by the given amount.
Expand All @@ -378,7 +376,7 @@ func (m *Message) ShiftSignalRight(signalEntityID EntityID, amount int) int {
return 0
}

return m.signalPayload.shiftRight(sig, amount)
return m.signalPayload.shiftRight(sig.EntityID(), amount)
}

// CompactSignals compacts the [Message] payload.
Expand Down
Loading

0 comments on commit 5350fce

Please sign in to comment.