-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
FerroO2000
committed
May 10, 2024
1 parent
94312d6
commit 7e77498
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters