def setup_timer(callback, delay, initial_delay=0, data=()):
    next_run = ticks_add(ticks_ms(), initial_delay)
    while True:
        now = ticks_ms()
        if ticks_less(next_run, now):
            callback(now, data)
            next_run = ticks_add(next_run, delay)
        yield
def update_all():
    updated = False
    now = ticks_ms()
    for timer in all_timers:
        if ticks_less(timer.next_run, now):
            timer.run()
            updated = True
        else:
            break
    if updated:
        all_timers.sort(key=Tick)
Exemplo n.º 3
0
# Change *_next_run to now or 0 if you want to trigger it on the first loop.
# Or "now + X" to trigger at a different time the first time.
"""Examples:
- Trigger the first run after the DELAY
    next_run  = now + DELAY
- Trigger the first run in the first loop
    next_run  = 0
- Trigger the first run after 10 seconds
    next_run  = now + 10_000
"""

now = ticks_ms()
timer_1_next_run = ticks_add(now, TIMER_1_DELAY)
timer_2_next_run = ticks_add(now, TIMER_2_DELAY)

while True:
    # set "now" only once per loop, it's the time reference for the loop.
    now = ticks_ms()
    if ticks_less(timer_1_next_run, now):
        # Next time based on last time
        # This avoids drifting, but can cause multiple triggerings if the rest
        # of the loop takes too long and delays are too short.
        timer_1_next_run = ticks_add(timer_1_next_run, TIMER_1_DELAY)
        print("TIMER 1 TRIGGERED", time.monotonic())
    if ticks_less(timer_2_next_run, now):
        # Next time based on current time.
        # This causes drifting, but guarantees a minimum delay between triggers.
        timer_2_next_run = ticks_add(now, TIMER_2_DELAY)
        print("TIMER 2 TRIGGERED             ", time.monotonic())
 def update(self, now=ticks_ms()):
     if ticks_less(self.next_run, now):
         self.run()
 def __gt__(self, other):
     return not ticks_less(self.val, other.val)
 def __lt__(self, other):
     return ticks_less(self.val, other.val)
# if this is 0, the first action will be executed immediately
START_DELAY = 0
# action for 10 seconds:
TIMER_1_DELAY = 10_000
# action for 50 seconds:
TIMER_2_DELAY = 50_000

# starting state (LED off for example)
current_state = False  # off
now = ticks_ms()
next_time = ticks_add(now, START_DELAY)

while True:
    # take the time once per loop, as the reference
    now = ticks_ms()
    if ticks_less(next_time, now):
        if current_state is False:
            # actual action (turn LED on for example)
            print("Do the thing that lasts {} seconds".format(TIMER_1_DELAY /
                                                              1000))
            print(time.time())
            # timer management
            delta = TIMER_1_DELAY
        else:
            # actual action (turn LED off for example)
            print("Do the thing that lasts {} seconds".format(TIMER_2_DELAY /
                                                              1000))
            print(time.time())
            # timer management
            delta = TIMER_2_DELAY
        # set the next event