Cruft cleaned, timers more accurate

This commit is contained in:
Kyle Brown
2020-10-30 00:04:56 -07:00
parent 7566981966
commit 4cc99d487d
5 changed files with 93 additions and 67 deletions

View File

@@ -1,29 +1,23 @@
import math
import time
USE_UTIME = False
def sleep_ms(ms):
'''
Tries to sleep for a number of milliseconds in a cross-implementation
way. Will raise an ImportError if time is not available on the platform.
'''
if USE_UTIME:
return time.sleep_ms(ms)
else:
return time.sleep(ms / 1000)
return time.sleep(ms / 1000)
def ticks_ms():
if USE_UTIME:
return time.ticks_ms()
else:
return math.floor(time.monotonic() * 1000)
'''Has .25s granularity, but is cheap'''
return time.monotonic() * 1000
def ticks_diff(new, old):
if USE_UTIME:
return time.ticks_diff(new, old)
else:
return new - old
return new - old
def accurate_ticks():
'''Is more expensive, but good for time critical things'''
return time.monotonic_ns()
def accurate_ticks_diff(new, old, ms):
return bool(new - old < ms * 1000000)