-
Notifications
You must be signed in to change notification settings - Fork 16
/
scheduler.py
38 lines (30 loc) · 1.09 KB
/
scheduler.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
from machine import Timer
import time
class Scheduler:
class Schedule:
def __init__(self, name, duration, callback):
self.name = name
self.duration = duration
self.callback = callback
self.lastrun = time.ticks_ms()
count = 0
def __init__(self):
self.schedules = []
def start(self):
self.start = time.ticks_ms()
self.timer = Timer(period=1, callback=self.event_callback)
def schedule(self, name, duration, callback):
self.schedules.append(self.Schedule(name, duration, callback))
def remove(self, name):
for schedule in self.schedules:
if schedule.name == name:
self.schedules.remove(schedule)
def event_callback(self, t):
for schedule in self.schedules:
if schedule.duration == 1:
schedule.callback(t)
else:
tm = time.ticks_ms()
if time.ticks_diff(tm, schedule.lastrun) > schedule.duration:
schedule.callback(t)
schedule.lastrun = tm