Skip to content

Commit

Permalink
updated StandardSignal factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Apr 13, 2024
1 parent f4a980e commit 8abc838
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 109 deletions.
10 changes: 6 additions & 4 deletions examples/dm1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func main() {

battPackGen := acmelib.NewMessage("BMSLV_BatteryPackGeneral", 6)

currSens, _ := acmelib.NewStandardSignal("Current_Sensor_mV", uint16bit, 0, uint16bit.Max(), 0, 0.076, mVUnit)
totVolt, _ := acmelib.NewStandardSignal("Total_voltage", uint16bit, 0, uint16bit.Max(), 0, 0.076, mVUnit)
currSens, _ := acmelib.NewStandardSignal("Current_Sensor_mV", uint16bit)
currSens.SetUnit(mVUnit)
totVolt, _ := acmelib.NewStandardSignal("Total_voltage", uint16bit)
totVolt.SetUnit(mVUnit)

if err := battPackGen.AppendSignal(currSens); err != nil {
panic(err)
Expand All @@ -33,7 +35,7 @@ func main() {

msgStatus := acmelib.NewMessage("BMSLV_Status", 6)

isRelOpen, _ := acmelib.NewStandardSignal("is_relary_open", flagType, flagType.Min(), flagType.Max(), 0, 1, nil)
isRelOpen, _ := acmelib.NewStandardSignal("is_relary_open", flagType)

if err := msgStatus.AppendSignal(isRelOpen); err != nil {
panic(err)
Expand All @@ -47,7 +49,7 @@ func main() {
dm1Msg := acmelib.NewMessage(dm1MsgName, 8)
sinSig, _ := acmelib.NewEnumSignal("sin", sinEnum)
fmiSig, _ := acmelib.NewEnumSignal("fmi", initFmi())
occSig, _ := acmelib.NewStandardSignal("occ_counter", uint8bit, uint8bit.Min(), uint8bit.Max(), 0, 1, nil)
occSig, _ := acmelib.NewStandardSignal("occ_counter", uint8bit)

if err := dm1Msg.InsertSignal(sinSig, 0); err != nil {
panic(err)
Expand Down
62 changes: 31 additions & 31 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func Test_Message_AppendSignal(t *testing.T) {

sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"}

sig0, _ := NewStandardSignal(sigNames[0], sigTypInt8, 0, 100, 0, 1, nil)
sig1, _ := NewStandardSignal(sigNames[1], sigTypInt8, 0, 100, 0, 1, nil)
sig2, _ := NewStandardSignal(sigNames[2], sigTypInt8, 0, 100, 0, 1, nil)
sig3, _ := NewStandardSignal(sigNames[3], sigTypInt8, 0, 100, 0, 1, nil)
sig4, _ := NewStandardSignal(sigNames[4], sigTypInt32, 0, 100, 0, 1, nil)
sig0, _ := NewStandardSignal(sigNames[0], sigTypInt8)
sig1, _ := NewStandardSignal(sigNames[1], sigTypInt8)
sig2, _ := NewStandardSignal(sigNames[2], sigTypInt8)
sig3, _ := NewStandardSignal(sigNames[3], sigTypInt8)
sig4, _ := NewStandardSignal(sigNames[4], sigTypInt32)

assert.NoError(msg.AppendSignal(sig0))

duplicatedSigName, _ := NewStandardSignal(sigNames[0], sigTypInt8, 0, 100, 0, 1, nil)
duplicatedSigName, _ := NewStandardSignal(sigNames[0], sigTypInt8)
assert.Error(msg.AppendSignal(duplicatedSigName))

assert.NoError(msg.AppendSignal(sig1))
Expand All @@ -42,10 +42,10 @@ func Test_Message_AppendSignal(t *testing.T) {
assert.NoError(msg.AppendSignal(sig4))

sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, -128, 127)
massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive, 0, 100, 0, 1, nil)
massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive)
assert.Error(msg.AppendSignal(massiveSig))

exidingSig, _ := NewStandardSignal("exiding_sig", sigTypInt8, 0, 100, 0, 1, nil)
exidingSig, _ := NewStandardSignal("exiding_sig", sigTypInt8)
assert.Error(msg.AppendSignal(exidingSig))

results := msg.Signals()
Expand All @@ -65,20 +65,20 @@ func Test_Message_InsertSignal(t *testing.T) {

sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"}

sig0, _ := NewStandardSignal(sigNames[0], sigTypInt8, 0, 100, 0, 1, nil)
sig1, _ := NewStandardSignal(sigNames[1], sigTypInt8, 0, 100, 0, 1, nil)
sig2, _ := NewStandardSignal(sigNames[2], sigTypInt8, 0, 100, 0, 1, nil)
sig3, _ := NewStandardSignal(sigNames[3], sigTypInt8, 0, 100, 0, 1, nil)
sig4, _ := NewStandardSignal(sigNames[4], sigTypInt32, 0, 100, 0, 1, nil)
sig0, _ := NewStandardSignal(sigNames[0], sigTypInt8)
sig1, _ := NewStandardSignal(sigNames[1], sigTypInt8)
sig2, _ := NewStandardSignal(sigNames[2], sigTypInt8)
sig3, _ := NewStandardSignal(sigNames[3], sigTypInt8)
sig4, _ := NewStandardSignal(sigNames[4], sigTypInt32)

assert.NoError(msg.InsertSignal(sig0, 0))

assert.NoError(msg.InsertSignal(sig1, 24))

duplicatedSigName, _ := NewStandardSignal(sigNames[0], sigTypInt8, 0, 100, 0, 1, nil)
duplicatedSigName, _ := NewStandardSignal(sigNames[0], sigTypInt8)
assert.Error(msg.InsertSignal(duplicatedSigName, 16))

overlappingSig, _ := NewStandardSignal("overlapping_sig", sigTypInt8, 0, 100, 0, 1, nil)
overlappingSig, _ := NewStandardSignal("overlapping_sig", sigTypInt8)
assert.Error(msg.InsertSignal(overlappingSig, 0))
assert.Error(msg.InsertSignal(overlappingSig, 7))
assert.Error(msg.InsertSignal(overlappingSig, 23))
Expand All @@ -88,10 +88,10 @@ func Test_Message_InsertSignal(t *testing.T) {
assert.NoError(msg.InsertSignal(sig4, 32))

sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, -128, 127)
massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive, 0, 100, 0, 1, nil)
massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive)
assert.Error(msg.InsertSignal(massiveSig, 0))

exidingSig, _ := NewStandardSignal("exiding_sig", sigTypInt8, 0, 100, 0, 1, nil)
exidingSig, _ := NewStandardSignal("exiding_sig", sigTypInt8)
assert.Error(msg.InsertSignal(exidingSig, 0))
assert.Error(msg.InsertSignal(exidingSig, 64))

Expand All @@ -115,11 +115,11 @@ func Test_Message_RemoveSignal(t *testing.T) {

sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"}

sig0, _ := NewStandardSignal(sigNames[0], sigTypInt8, 0, 100, 0, 1, nil)
sig1, _ := NewStandardSignal(sigNames[1], sigTypInt8, 0, 100, 0, 1, nil)
sig2, _ := NewStandardSignal(sigNames[2], sigTypInt8, 0, 100, 0, 1, nil)
sig3, _ := NewStandardSignal(sigNames[3], sigTypInt8, 0, 100, 0, 1, nil)
sig4, _ := NewStandardSignal(sigNames[4], sigTypInt32, 0, 100, 0, 1, nil)
sig0, _ := NewStandardSignal(sigNames[0], sigTypInt8)
sig1, _ := NewStandardSignal(sigNames[1], sigTypInt8)
sig2, _ := NewStandardSignal(sigNames[2], sigTypInt8)
sig3, _ := NewStandardSignal(sigNames[3], sigTypInt8)
sig4, _ := NewStandardSignal(sigNames[4], sigTypInt32)

assert.NoError(msg.AppendSignal(sig0))
assert.NoError(msg.AppendSignal(sig1))
Expand Down Expand Up @@ -147,9 +147,9 @@ func Test_Message_CompactSignals(t *testing.T) {

sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127)

sig0, _ := NewStandardSignal("sig_0", sigTypInt8, 0, 100, 0, 1, nil)
sig1, _ := NewStandardSignal("sig_1", sigTypInt8, 0, 100, 0, 1, nil)
sig2, _ := NewStandardSignal("sig_2", sigTypInt8, 0, 100, 0, 1, nil)
sig0, _ := NewStandardSignal("sig_0", sigTypInt8)
sig1, _ := NewStandardSignal("sig_1", sigTypInt8)
sig2, _ := NewStandardSignal("sig_2", sigTypInt8)

assert.NoError(msg.InsertSignal(sig0, 2))
assert.NoError(msg.InsertSignal(sig1, 18))
Expand All @@ -173,7 +173,7 @@ func Test_Message_ShiftSignalLeft(t *testing.T) {
sigTypeInt4, err := newSignalType("int4", SignalTypeKindInteger, 4, true, -8, 7)
assert.NoError(err)

sig0, err := NewStandardSignal("signal_0", sigTypeInt4, -8, 7, 0, 1, nil)
sig0, err := NewStandardSignal("signal_0", sigTypeInt4)
assert.NoError(err)
assert.NoError(msg.InsertSignal(sig0, 12))

Expand All @@ -182,13 +182,13 @@ func Test_Message_ShiftSignalLeft(t *testing.T) {
assert.Equal(1, msg.ShiftSignalLeft(sig0.EntityID(), 1))
assert.Equal(11, msg.ShiftSignalLeft(sig0.EntityID(), 16))

sig1, err := NewStandardSignal("signal_1", sigTypeInt4, -8, 7, 0, 1, nil)
sig1, err := NewStandardSignal("signal_1", sigTypeInt4)
assert.NoError(err)
assert.NoError(msg.InsertSignal(sig1, 12))

assert.Equal(8, msg.ShiftSignalLeft(sig1.EntityID(), 16))

sig2, err := NewStandardSignal("signal_2", sigTypeInt4, -8, 7, 0, 1, nil)
sig2, err := NewStandardSignal("signal_2", sigTypeInt4)
assert.NoError(err)
assert.NoError(msg.InsertSignal(sig2, 12))

Expand All @@ -208,7 +208,7 @@ func Test_Message_ShiftSignalRight(t *testing.T) {

sigTypeInt4, _ := newSignalType("int4", SignalTypeKindInteger, 4, true, -8, 7)

sig0, err := NewStandardSignal("signal_0", sigTypeInt4, -8, 7, 0, 1, nil)
sig0, err := NewStandardSignal("signal_0", sigTypeInt4)
assert.NoError(err)
assert.NoError(msg.InsertSignal(sig0, 0))

Expand All @@ -217,13 +217,13 @@ func Test_Message_ShiftSignalRight(t *testing.T) {
assert.Equal(1, msg.ShiftSignalRight(sig0.EntityID(), 1))
assert.Equal(11, msg.ShiftSignalRight(sig0.EntityID(), 16))

sig1, err := NewStandardSignal("signal_1", sigTypeInt4, -8, 7, 0, 1, nil)
sig1, err := NewStandardSignal("signal_1", sigTypeInt4)
assert.NoError(err)
assert.NoError(msg.InsertSignal(sig1, 0))

assert.Equal(8, msg.ShiftSignalRight(sig1.EntityID(), 16))

sig2, err := NewStandardSignal("signal_2", sigTypeInt4, -8, 7, 0, 1, nil)
sig2, err := NewStandardSignal("signal_2", sigTypeInt4)
assert.NoError(err)
assert.NoError(msg.InsertSignal(sig2, 0))

Expand Down
46 changes: 30 additions & 16 deletions signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,9 @@ type StandardSignal struct {
unit *SignalUnit
}

// NewStandardSignal creates a new [StandardSignal] with the given name,
// [SignalType], min, max, offset, scale, and unit.
// NewStandardSignal creates a new [StandardSignal] with the given name and [SignalType].
// It may return an error if the given [SignalType] is nil.
func NewStandardSignal(name string, typ *SignalType, min, max, offset, scale float64, unit *SignalUnit) (*StandardSignal, error) {
func NewStandardSignal(name string, typ *SignalType) (*StandardSignal, error) {
if typ == nil {
return nil, errors.New("signal type cannot be nil")
}
Expand All @@ -229,11 +228,11 @@ func NewStandardSignal(name string, typ *SignalType, min, max, offset, scale flo
signal: newSignal(name, SignalKindStandard),

typ: typ,
min: min,
max: max,
offset: offset,
scale: scale,
unit: unit,
min: typ.min,
max: typ.max,
offset: 0,
scale: 1,
unit: nil,
}, nil
}

Expand Down Expand Up @@ -285,11 +284,11 @@ func (ss *StandardSignal) Type() *SignalType {
return ss.typ
}

// SetType sets the [SignalType] of the [StandardSignal] to the given [SignalType], min, max,
// scale, and offset.
// SetType sets the [SignalType] of the [StandardSignal].
// It resets the physical values.
// It may return an error if the given [SignalType] is nil, or if the new signal type
// size cannot fit in the message payload.
func (ss *StandardSignal) SetType(typ *SignalType, min, max, offset, scale float64) error {
func (ss *StandardSignal) SetType(typ *SignalType) error {
if typ == nil {
return errors.New("signal type cannot be nil")
}
Expand All @@ -299,6 +298,21 @@ func (ss *StandardSignal) SetType(typ *SignalType, min, max, offset, scale float
}

ss.typ = typ
ss.min = typ.min
ss.max = typ.max
ss.offset = 0
ss.scale = 1

return nil
}

// SetPhysicalValues sets the physical min, max, offset, and scale of the [StandardSignal].
// It returns an error if the scale is equal to 0.
func (ss *StandardSignal) SetPhysicalValues(min, max, offset, scale float64) error {
if scale == 0 {
return ss.errorf(fmt.Errorf("scale cannot be 0"))
}

ss.min = min
ss.max = max
ss.offset = offset
Expand Down Expand Up @@ -331,16 +345,16 @@ func (ss *StandardSignal) Scale() float64 {
return ss.scale
}

// Unit returns the [SignalUnit] of the [StandardSignal].
func (ss *StandardSignal) Unit() *SignalUnit {
return ss.unit
}

// SetUnit sets the [SignalUnit] of the [StandardSignal] to the given one.
func (ss *StandardSignal) SetUnit(unit *SignalUnit) {
ss.unit = unit
}

// Unit returns the [SignalUnit] of the [StandardSignal].
func (ss *StandardSignal) Unit() *SignalUnit {
return ss.unit
}

// EnumSignal is a signal that holds a [SignalEnum].
type EnumSignal struct {
*signal
Expand Down
Loading

0 comments on commit 8abc838

Please sign in to comment.