2014-04-30 23:10:44 +08:00
|
|
|
package gobot
|
2013-11-24 01:12:57 +08:00
|
|
|
|
|
|
|
import (
|
2014-07-24 00:48:09 +08:00
|
|
|
"crypto/rand"
|
2017-02-02 22:29:56 +08:00
|
|
|
"fmt"
|
2014-03-27 13:24:45 +08:00
|
|
|
"math"
|
2014-07-24 00:48:09 +08:00
|
|
|
"math/big"
|
2013-11-24 01:12:57 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-12-09 03:32:02 +08:00
|
|
|
// 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.
|
2016-05-14 08:21:21 +08:00
|
|
|
// It does not wait for the previous execution of f to finish before
|
|
|
|
// it fires the next f.
|
2016-12-09 03:32:02 +08:00
|
|
|
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 {
|
2023-05-19 20:16:22 +08:00
|
|
|
<-ticker.C
|
|
|
|
f()
|
2013-11-24 01:12:57 +08:00
|
|
|
}
|
|
|
|
}()
|
2016-05-14 08:21:21 +08:00
|
|
|
|
2016-12-09 03:32:02 +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.
|
2014-05-03 18:20:02 +08:00
|
|
|
func After(t time.Duration, f func()) {
|
|
|
|
time.AfterFunc(t, f)
|
2013-11-24 01:12:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-01 19:54:20 +08:00
|
|
|
// Rand returns a positive random int up to maximum
|
|
|
|
func Rand(maximum int) int {
|
|
|
|
i, _ := rand.Int(rand.Reader, big.NewInt(int64(maximum)))
|
2014-07-24 00:48:09 +08:00
|
|
|
return int(i.Int64())
|
2013-11-24 01:12:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-01 19:54:20 +08:00
|
|
|
// FromScale returns a converted input from minimum, maximum to 0.0...1.0.
|
|
|
|
func FromScale(input, minimum, maximum float64) float64 {
|
|
|
|
return (input - math.Min(minimum, maximum)) / (math.Max(minimum, maximum) - math.Min(minimum, maximum))
|
2014-03-27 13:24:45 +08:00
|
|
|
}
|
|
|
|
|
2024-11-01 19:54:20 +08:00
|
|
|
// ToScale returns a converted input from 0...1 to minimum...maximum scale.
|
|
|
|
// If input is less than minimum then ToScale returns minimum.
|
|
|
|
// If input is greater than maximum then ToScale returns maximum
|
|
|
|
func ToScale(input, minimum, maximum float64) float64 {
|
|
|
|
i := input*(math.Max(minimum, maximum)-math.Min(minimum, maximum)) + math.Min(minimum, maximum)
|
2023-11-16 03:51:52 +08:00
|
|
|
switch {
|
2024-11-01 19:54:20 +08:00
|
|
|
case i < math.Min(minimum, maximum):
|
|
|
|
return math.Min(minimum, maximum)
|
|
|
|
case i > math.Max(minimum, maximum):
|
|
|
|
return math.Max(minimum, maximum)
|
2023-11-16 03:51:52 +08:00
|
|
|
default:
|
2014-03-27 13:24:45 +08:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 22:29:56 +08:00
|
|
|
|
2018-08-15 23:06:52 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-02-02 22:29:56 +08:00
|
|
|
// 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)))
|
|
|
|
}
|