refactorings.

This commit is contained in:
2022-08-21 11:17:42 +02:00
parent ae68679228
commit b303a623f6
14 changed files with 166 additions and 122 deletions
+4 -29
View File
@@ -3,7 +3,6 @@ package effects
import (
"fmt"
"git.aiterp.net/lucifer3/server/device"
"math"
"time"
)
@@ -14,41 +13,17 @@ type Gradient struct {
Interpolate bool `json:"interpolate,omitempty"`
}
func (e Gradient) State(index, length, round, _ int) device.State {
func (e Gradient) State(index, length, round int) device.State {
if len(e.States) == 0 {
return device.State{}
}
if e.Reverse {
index = length - (index + 1)
round = -round
}
walkedIndex := ((length + index) - round) % length
walkedIndex := (index + round) % length
indexFactor := math.Min(float64(walkedIndex)/float64(length-1), 1)
stateIncrement := 1.0 / float64(len(e.States)-1)
for i := range e.States {
a := float64(i) * stateIncrement
if indexFactor >= a {
si := e.States[i]
sj := e.States[(i+1)%len(e.States)]
f := (indexFactor - a) / stateIncrement
if f < 0 || f > 1 {
panic(f)
}
if e.Interpolate {
return si.Interpolate(sj, f)
} else if f < 0.5 {
return si
} else {
return sj
}
}
}
return e.States[len(e.States)-1]
return gradientState(e.States, e.Interpolate, walkedIndex, length)
}
func (e Gradient) Frequency() time.Duration {
+2 -2
View File
@@ -15,14 +15,14 @@ type Manual struct {
}
func (e Manual) EffectDescription() string {
return fmt.Sprintf("Manual%s", e.State(0, 0, 0, 0).String())
return fmt.Sprintf("Manual%s", e.State(0, 0, 0).String())
}
func (e Manual) Frequency() time.Duration {
return 0
}
func (e Manual) State(int, int, int, int) device.State {
func (e Manual) State(int, int, int) device.State {
return device.State{
Power: e.Power,
Temperature: e.Temperature,
+1 -1
View File
@@ -11,7 +11,7 @@ type Pattern struct {
AnimationMS int64 `json:"animationMs,omitempty"`
}
func (e Pattern) State(index, _, round, _ int) device.State {
func (e Pattern) State(index, _, round int) device.State {
if len(e.States) == 0 {
return device.State{}
}
+2 -26
View File
@@ -13,36 +13,12 @@ type Random struct {
AnimationMS int64 `json:"animationMs,omitempty"`
}
func (e Random) State(_, _, _, _ int) device.State {
func (e Random) State(_, _, _ int) device.State {
if len(e.States) == 0 {
return device.State{}
}
indexFactor := rand.Float64()
stateIncrement := 1.0 / float64(len(e.States)-1)
for i := range e.States {
a := float64(i) * stateIncrement
if indexFactor >= a {
si := e.States[i]
sj := e.States[(i+1)%len(e.States)]
f := (indexFactor - a) / stateIncrement
if f < 0 || f > 1 {
panic(f)
}
if e.Interpolate {
return si.Interpolate(sj, f)
} else if f < 0.5 {
return si
} else {
return sj
}
}
}
return e.States[len(e.States)-1]
return gradientStateFactor(e.States, e.Interpolate, rand.Float64())
}
func (e Random) Frequency() time.Duration {
+40
View File
@@ -1,7 +1,9 @@
package effects
import (
"fmt"
"git.aiterp.net/lucifer3/server/device"
"math"
"strings"
)
@@ -21,3 +23,41 @@ func statesDescription(states []device.State) string {
return sb.String()
}
func gradientState(states []device.State, interpolate bool, index, length int) device.State {
indexFactor := math.Min(float64(index)/float64(length-1), 1)
return gradientStateFactor(states, interpolate, indexFactor)
}
func gradientStateFactor(states []device.State, interpolate bool, factor float64) device.State {
var stateIncrement float64
if interpolate {
stateIncrement = 1.0 / float64(len(states)-1)
} else {
stateIncrement = 1.0 / float64(len(states))
}
for i := range states {
a := float64(i) * stateIncrement
b := float64(i+1) * stateIncrement
if factor >= a && factor < b {
si := states[i]
if !interpolate || i+1 == len(states) {
return si
}
sj := states[i+1]
f := (factor - a) / stateIncrement
if f < 0 || f > 1 {
panic(fmt.Sprintf("assert(0 <= f <= 1) f = %.2f (if = %.2f, a = %.2f, si = %.2f)", f, factor, a, stateIncrement))
}
return si.Interpolate(sj, f)
}
}
return states[len(states)-1]
}