time.py 743 B

12345678910111213141516171819202122232425262728293031323334
  1. class Lapse:
  2. def __init__(self,intv=1.0,oneshot=None):
  3. self.cunit = intv
  4. self.intv = int( intv * 1000000 )
  5. self.next = self.intv
  6. self.last = Time.ticks_us()
  7. self.count = 1.0
  8. if oneshot:
  9. self.shot = False
  10. return
  11. self.shot = None
  12. #FIXME: pause / resume(reset)
  13. def __bool__(self):
  14. if self.shot is True:
  15. return False
  16. t = Time.ticks_us()
  17. self.next -= ( t - self.last )
  18. self.last = t
  19. if self.next <= 0:
  20. if self.shot is False:
  21. self.shot = True
  22. self.count+= self.cunit
  23. self.next = self.intv
  24. return True
  25. return False
  26. export('Lapse')