mirror of
https://github.com/jwetzell/artnet-go.git
synced 2026-09-10 07:49:28 +00:00
some byte slice cleanup
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type ArtDmx struct {
|
||||
ID []uint8
|
||||
ID [8]uint8
|
||||
OpCode uint16
|
||||
ProtVerHi uint8
|
||||
ProtVerLo uint8
|
||||
@@ -15,7 +15,6 @@ type ArtDmx struct {
|
||||
Physical uint8
|
||||
SubUni uint8
|
||||
Net uint8
|
||||
Length uint16
|
||||
Data []uint8
|
||||
}
|
||||
|
||||
@@ -27,18 +26,22 @@ func (ad *ArtDmx) GetProtVer() uint16 {
|
||||
return uint16(ad.ProtVerHi)<<8 + uint16(ad.ProtVerLo)
|
||||
}
|
||||
|
||||
func (ad *ArtDmx) GetID() []uint8 {
|
||||
func (ad *ArtDmx) GetID() [8]uint8 {
|
||||
return ad.ID
|
||||
}
|
||||
|
||||
func (ad *ArtDmx) Length() uint16 {
|
||||
return uint16(len(ad.Data))
|
||||
}
|
||||
|
||||
func (ad *ArtDmx) UnmarshalBinary(data []byte) error {
|
||||
if len(data) < 18 {
|
||||
return errors.New("ArtDmx packet must be at least 18 bytes long")
|
||||
}
|
||||
|
||||
ad.ID = data[0:8]
|
||||
copy(ad.ID[:], data[0:8])
|
||||
|
||||
if !slices.Equal(ArtNetID, ad.ID) {
|
||||
if !slices.Equal(ArtNetID[:], ad.ID[:]) {
|
||||
return errors.New("ID does not match Art-Net ID")
|
||||
}
|
||||
|
||||
@@ -53,23 +56,30 @@ func (ad *ArtDmx) UnmarshalBinary(data []byte) error {
|
||||
ad.SubUni = data[offset+2]
|
||||
ad.Net = data[offset+3]
|
||||
|
||||
ad.Length = uint16(data[offset+4])<<8 + uint16(data[offset+5])
|
||||
length := int(data[offset+4])<<8 + int(data[offset+5])
|
||||
|
||||
dmxDataOffset := offset + 6
|
||||
|
||||
if len(data[dmxDataOffset:]) < int(ad.Length) {
|
||||
if len(data[dmxDataOffset:]) < length {
|
||||
return errors.New("ArtDmx packet length mismatch")
|
||||
}
|
||||
|
||||
ad.Data = make([]uint8, ad.Length)
|
||||
ad.Data = data[dmxDataOffset : dmxDataOffset+length]
|
||||
|
||||
copy(ad.Data, data[dmxDataOffset:dmxDataOffset+int(ad.Length)])
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ad *ArtDmx) MarshalBinary() ([]byte, error) {
|
||||
data := []byte(ArtNetID)
|
||||
data = append(data, byte(ad.OpCode), byte(ad.OpCode>>8), ad.ProtVerHi, ad.ProtVerLo, ad.Sequence, ad.Physical, ad.SubUni, ad.Net, byte(ad.Length>>8), byte(ad.Length))
|
||||
data = append(data, ad.Data...)
|
||||
data := make([]byte, 8+10+ad.Length())
|
||||
copy(data[0:8], ad.ID[:])
|
||||
binary.LittleEndian.PutUint16(data[8:10], ad.OpCode)
|
||||
data[10] = ad.ProtVerHi
|
||||
data[11] = ad.ProtVerLo
|
||||
data[12] = ad.Sequence
|
||||
data[13] = ad.Physical
|
||||
data[14] = ad.SubUni
|
||||
data[15] = ad.Net
|
||||
binary.BigEndian.PutUint16(data[16:18], ad.Length())
|
||||
copy(data[18:], ad.Data)
|
||||
return data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user