hybridgroup.gobot/utils.go

70 lines
1.8 KiB
Go
Raw Normal View History

2014-04-30 23:10:44 +08:00
package gobot
2013-11-24 01:12:57 +08:00
import (
"crypto/rand"
"fmt"
2014-03-27 13:24:45 +08:00
"math"
"math/big"
2013-11-24 01:12:57 +08:00
"time"
)
// Every triggers f every t time.Duration until the end of days, or when a Stop()
// is called on the Ticker that is returned by the Every function.
// It does not wait for the previous execution of f to finish before
// it fires the next f.
func Every(t time.Duration, f func()) *time.Ticker {
ticker := time.NewTicker(t)
2014-10-16 01:57:07 +08:00
2013-11-24 01:12:57 +08:00
go func() {
for {
select {
case <-ticker.C:
f()
}
2013-11-24 01:12:57 +08:00
}
}()
return ticker
2013-11-24 01:12:57 +08:00
}
2014-11-13 07:00:40 +08:00
// After triggers f after t duration.
func After(t time.Duration, f func()) {
time.AfterFunc(t, f)
2013-11-24 01:12:57 +08:00
}
2014-11-13 07:00:40 +08:00
// Rand returns a positive random int up to max
2013-11-24 01:12:57 +08:00
func Rand(max int) int {
i, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
return int(i.Int64())
2013-11-24 01:12:57 +08:00
}
2014-11-13 07:00:40 +08:00
// FromScale returns a converted input from min, max to 0.0...1.0.
2014-03-27 13:24:45 +08:00
func FromScale(input, min, max float64) float64 {
return (input - math.Min(min, max)) / (math.Max(min, max) - math.Min(min, max))
}
2014-11-13 07:00:40 +08:00
// ToScale returns a converted input from 0...1 to min...max scale.
// If input is less than min then ToScale returns min.
// If input is greater than max then ToScale returns max
2014-03-27 13:24:45 +08:00
func ToScale(input, min, max float64) float64 {
i := input*(math.Max(min, max)-math.Min(min, max)) + math.Min(min, max)
if i < math.Min(min, max) {
return math.Min(min, max)
} else if i > math.Max(min, max) {
return math.Max(min, max)
} else {
return i
}
}
// Rescale performs a direct linear rescaling of a number from one scale to another.
func Rescale(input, fromMin, fromMax, toMin, toMax float64) float64 {
return (input-fromMin)*(toMax-toMin)/(fromMax-fromMin) + toMin
}
// DefaultName returns a sensible random default name
// for a robot, adaptor or driver
func DefaultName(name string) string {
return fmt.Sprintf("%s-%X", name, Rand(int(^uint(0)>>1)))
}