Skip to content

Commit

Permalink
canid builder backbone
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed May 10, 2024
1 parent 94312d6 commit 7e77498
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
94 changes: 94 additions & 0 deletions canid_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package acmelib

type CANID uint32

type CANIDBuilderOpKind int

const (
CANIDBuilderOpMessagePriority CANIDBuilderOpKind = iota
CANIDBuilderOpMessageID
CANIDBuilderOpNodeID
)

type CANIDBuilderOp struct {
kind CANIDBuilderOpKind
from int
len int
}

func (bo *CANIDBuilderOp) Kind() CANIDBuilderOpKind {
return bo.kind
}

func (bo *CANIDBuilderOp) From() int {
return bo.from
}

func (bo *CANIDBuilderOp) Len() int {
return bo.len
}

type CANIDBuilder struct {
operations []*CANIDBuilderOp
}

func NewCANIDBuilder() *CANIDBuilder {
return &CANIDBuilder{
operations: []*CANIDBuilderOp{},
}
}

func (b *CANIDBuilder) Operations() []*CANIDBuilderOp {
return b.operations
}

func (b *CANIDBuilder) Calculate(messagePriority MessagePriority, messageID int, nodeID NodeID) CANID {
canID := uint32(0)

for _, op := range b.operations {
tmpVal := uint32(0)
switch op.kind {
case CANIDBuilderOpMessagePriority:
tmpVal = uint32(messagePriority)
case CANIDBuilderOpMessageID:
tmpVal = uint32(messageID)
case CANIDBuilderOpNodeID:
tmpVal = uint32(nodeID)
}

mask := uint32(0xFFFFFFFF) >> uint32(32-op.len)
tmpVal &= mask

tmpVal = tmpVal << uint32(op.from)
canID |= tmpVal
}

return CANID(canID)
}

func (b *CANIDBuilder) UseMessagePriority(from int) *CANIDBuilder {
b.operations = append(b.operations, &CANIDBuilderOp{
kind: CANIDBuilderOpMessagePriority,
from: from,
len: 2,
})
return b
}

func (b *CANIDBuilder) UseMessageID(from, len int) *CANIDBuilder {
b.operations = append(b.operations, &CANIDBuilderOp{
kind: CANIDBuilderOpMessageID,
from: from,
len: len,
})
return b
}

func (b *CANIDBuilder) UseNodeID(from, len int) *CANIDBuilder {
b.operations = append(b.operations, &CANIDBuilderOp{
kind: CANIDBuilderOpNodeID,
from: from,
len: len,
})
return b
}
26 changes: 26 additions & 0 deletions canid_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package acmelib

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_CANIDBuilder(t *testing.T) {
assert := assert.New(t)

b := NewCANIDBuilder()
b.UseMessagePriority(30).UseMessageID(4, 10).UseNodeID(0, 4)

msgPriority := MessagePriorityLow
msgID := 0b1111111111
nodeID := NodeID(0b11)

expected := uint32(msgPriority << 30)
expected |= uint32(msgID << 4)
expected |= uint32(nodeID)

res := b.Calculate(msgPriority, msgID, nodeID)

assert.Equal(expected, uint32(res))
}
2 changes: 2 additions & 0 deletions examples/markdown/SC24.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
> [!IMPORTANT]
> This markdown document is generated by [acmelib](https://github.com/squadracorsepolito/acmelib)
# SC24
## Main CAN Bus
### BMS_LV
Expand Down
2 changes: 2 additions & 0 deletions md_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (e *mdExporter) getLink(text string) string {
}

func (e *mdExporter) exportNetwork(net *Network) {
e.w.Importantf("This markdown document is generated by %s", md.Link("acmelib", "https://github.com/squadracorsepolito/acmelib"))

e.w.H1(net.name)

if len(net.desc) > 0 {
Expand Down

0 comments on commit 7e77498

Please sign in to comment.