github.com/simpleiot/simpleiot@v0.18.3/sim/sim.go (about) 1 package sim 2 3 // Sim represents a simulation. 4 type Sim struct { 5 currentValue float64 6 step float64 7 max float64 8 min float64 9 up bool 10 } 11 12 // NewSim creates a new simulation 13 func NewSim(start, step, min, max float64) Sim { 14 return Sim{ 15 currentValue: start, 16 step: step, 17 min: min, 18 max: max, 19 } 20 } 21 22 // Sim runs a simulation step 23 func (s *Sim) Sim() float64 { 24 if s.up { 25 s.currentValue += s.step 26 if s.currentValue > s.max { 27 s.currentValue = s.max 28 s.up = false 29 } 30 } else { 31 s.currentValue -= s.step 32 if s.currentValue < s.min { 33 s.currentValue = s.min 34 s.up = true 35 } 36 } 37 38 return s.currentValue 39 }