add variable system + fix bugs here and there.

This commit is contained in:
2023-01-13 01:19:34 +01:00
parent 88036cfcae
commit 842f96d8e3
22 changed files with 368 additions and 43 deletions
+5
View File
@@ -11,6 +11,7 @@ type serializedEffect struct {
Gradient *Gradient `json:"gradient,omitempty"`
Pattern *Pattern `json:"pattern,omitempty"`
Random *Random `json:"random,omitempty"`
VRange *VRange `json:"vrange,omitempty"`
}
type Serializable struct {
@@ -33,6 +34,8 @@ func (s *Serializable) UnmarshalJSON(raw []byte) error {
s.Effect = *value.Pattern
case value.Random != nil:
s.Effect = *value.Random
case value.VRange != nil:
s.Effect = *value.VRange
default:
return errors.New("unsupported effect")
}
@@ -50,6 +53,8 @@ func (s *Serializable) MarshalJSON() ([]byte, error) {
return json.Marshal(serializedEffect{Pattern: &effect})
case Random:
return json.Marshal(serializedEffect{Random: &effect})
case VRange:
return json.Marshal(serializedEffect{VRange: &effect})
default:
panic(s.Effect.EffectDescription() + "is not understood by serializer")
}
+46
View File
@@ -0,0 +1,46 @@
package effects
import (
"fmt"
"git.aiterp.net/lucifer3/server/device"
"time"
)
type VRange struct {
States []device.State `json:"states,omitempty"`
Variable string `json:"variable"`
Min float64 `json:"min"`
Max float64 `json:"max"`
}
func (e VRange) VariableName() string {
return e.Variable
}
func (e VRange) VariableState(_, _ int, value float64) device.State {
if value <= e.Min {
return e.States[0]
} else if value >= e.Max {
return e.States[len(e.States)-1]
}
return gradientStateFactor(e.States, true, (value-e.Min)/(e.Max-e.Min))
}
func (e VRange) State(_, _, _ int) device.State {
if len(e.States) == 0 {
return device.State{}
}
return e.States[0]
}
func (e VRange) Frequency() time.Duration {
return 0
}
func (e VRange) EffectDescription() string {
return fmt.Sprintf(
"VRange(states:%s, name:%s, range:%.1f..%.1f)",
statesDescription(e.States), e.Variable, e.Min, e.Max,
)
}