github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/pkg/gpio/delay.go (about) 1 package gpio 2 3 import ( 4 "time" 5 _ "unsafe" 6 ) 7 8 // nanotime returns the current time in nanoseconds from a monotonic clock. 9 //go:linkname nanotime runtime.nanotime 10 func nanotime() int64 11 12 // delay waits for given duration using busy waiting. The godoc for 13 // time.Sleep states: 14 // 15 // Sleep pauses the current goroutine for *at least* the duration d 16 // 17 // This means that for sub-millisecond sleep durations it will pause the 18 // current goroutine for longer than we can afford as for us the sleep duration 19 // needs to be as precise as possible to send out the correct codes to the 20 // outlets. 21 // time.Sleep causes sleep pauses to be off by 100+ microseconds on average 22 // whereas, depending on the platform, we can bring this down to ~1 23 // microseconds at worst using busy waiting in combination with 24 // runtime.nanotime. 25 func delay(duration time.Duration) { 26 end := nanotime() + int64(duration) 27 28 for nanotime() < end { 29 } 30 }