-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
60 lines (49 loc) · 1.74 KB
/
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""libraries for a set of timers.
"""
import time
__author__ = "Jonathan Fromentin"
__credits__ = ["Jonathan Fromentin"]
__license__ = "CeCILL version 2.1"
__version__ = "0.2.0"
__maintainer__ = "Jonathan Fromentin"
class OneShotTimer:
"""A simple one shot timer without interrupt that returns True when
activated. It can be started or restarted avec the start method."""
def __init__(self, period, start=True):
self.period = period
if start:
self.start()
else:
self.is_started = False
def start(self, period=None):
"""Start/restart the timer and set the period if specified. this method
is useful if you want to start the timer after its instantiation or if
you want to reuse the timer without having to recreate a new
instance."""
if period:
self.period = period
self.beginning = time.ticks_ms()
self.is_started = True
@property
def is_activated(self):
"""Get the calculated value of is_activated."""
if self.is_started:
delta = time.ticks_diff(time.ticks_ms(), self.beginning)
if delta > self.period:
self.is_started = False
return True
return False
class PeriodicTimer:
"""A simple piriodic timer without interrupt that returns true when
activated."""
def __init__(self, period):
self.period = period
self.beginning = time.ticks_ms()
@property
def is_activated(self):
"""Get the calculated value of is_activated."""
delta = time.ticks_diff(time.ticks_ms(), self.beginning)
if delta > self.period:
self.beginning = time.ticks_ms()
return True
return False