Skip to content

Commit

Permalink
Added Decode and DecodePayload signal methods for decoding without sh…
Browse files Browse the repository at this point in the history
…ifting ranges
  • Loading branch information
Jason Shiverick committed Mar 22, 2021
1 parent 9986b66 commit b87d69e
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/descriptor/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@ func (s *Signal) UnmarshalPhysical(d can.Data) float64 {
}
}

// Decode returns the physical value of the signal in the provided CAN frame.
func (s *Signal) Decode(d can.Data) float64 {
switch {
case uint8(s.Length) == 1:
if d.Bit(uint8(s.Start)) {
return 1
}
return 0
case s.IsSigned:
var value int64
if s.IsBigEndian {
value = d.SignedBitsBigEndian(uint8(s.Start), uint8(s.Length))
} else {
value = d.SignedBitsLittleEndian(uint8(s.Start), uint8(s.Length))
}
return s.Offset + float64(value)*s.Scale
default:
var value uint64
if s.IsBigEndian {
value = d.UnsignedBitsBigEndian(uint8(s.Start), uint8(s.Length))
} else {
value = d.UnsignedBitsLittleEndian(uint8(s.Start), uint8(s.Length))
}
return s.Offset + float64(value)*s.Scale
}
}

// UnmarshalPhysicalPayload returns the physical value of the signal in the provided CAN frame.
func (s *Signal) UnmarshalPhysicalPayload(p *can.Payload) float64 {
switch {
Expand Down Expand Up @@ -136,6 +163,33 @@ func (s *Signal) UnmarshalPhysicalPayload(p *can.Payload) float64 {
}
}

// DecodePayload returns the physical value of the signal in the provided CAN frame.
func (s *Signal) DecodePayload(p *can.Payload) float64 {
switch {
case uint8(s.Length) == 1:
if p.Bit(s.Start) {
return 1
}
return 0
case s.IsSigned:
var value int64
if s.IsBigEndian {
value = p.SignedBitsBigEndian(s.Start, s.Length)
} else {
value = p.SignedBitsLittleEndian(s.Start, s.Length)
}
return s.Offset + float64(value)*s.Scale
default:
var value uint64
if s.IsBigEndian {
value = p.UnsignedBitsBigEndian(s.Start, s.Length)
} else {
value = p.UnsignedBitsLittleEndian(s.Start, s.Length)
}
return s.Offset + float64(value)*s.Scale
}
}

// UnmarshalUnsigned returns the unsigned value of the signal in the provided CAN frame.
func (s *Signal) UnmarshalUnsigned(d can.Data) uint64 {
if s.IsBigEndian {
Expand Down

0 comments on commit b87d69e

Please sign in to comment.