From: Ralf Jung Date: Sat, 26 Oct 2013 20:08:59 +0000 (+0200) Subject: change the way the waker works: let others register to be called by it X-Git-Url: https://git.ralfj.de/saartuer.git/commitdiff_plain/dfe98478e79cf926b2867677b3e1b90e6b679024?hp=-c change the way the waker works: let others register to be called by it --- dfe98478e79cf926b2867677b3e1b90e6b679024 diff --git a/statemachine.py b/statemachine.py index 0f325a8..59951d4 100644 --- a/statemachine.py +++ b/statemachine.py @@ -304,12 +304,13 @@ class StateMachine(): return StateMachine.StateAuf(self.state_machine) return super().handle_pins_event() - def __init__(self, actor, fallback = False): + def __init__(self, actor, waker, fallback = False): self.actor = actor self.callback = ThreadFunction(self._callback, name="StateMachine") self.current_state = StateMachine.StateStart(self, fallback) self.pins = None self.old_pins = None + waker.register(lambda: self.callback(StateMachine.CMD_WAKEUP), 1.0) # wake up every second def stop (self): self.callback.stop() diff --git a/tuerd b/tuerd index 92c4122..e828ebb 100755 --- a/tuerd +++ b/tuerd @@ -30,10 +30,10 @@ GPIO.setmode(GPIO.BOARD) # bring 'em all up the_actor = actor.Actor() -the_machine = statemachine.StateMachine(the_actor, args.fallback) +the_waker = waker.Waker(the_machine) +the_machine = statemachine.StateMachine(the_actor, the_waker, args.fallback) the_socket = tysock.TySocket(the_machine) the_pins = pins.PinsWatcher(the_machine) -the_waker = waker.Waker(the_machine) # we do the socket accept thing in the main thread try: @@ -44,8 +44,8 @@ except KeyboardInterrupt: pass # bring 'em all down -the_waker.stop() -the_pins.stop() +the_waker.stop() # this one first, it "randomly" calls other threads +the_pins.stop() # as does this the_machine.stop() the_actor.stop() diff --git a/waker.py b/waker.py index 7b212a1..1cf2884 100644 --- a/waker.py +++ b/waker.py @@ -1,13 +1,35 @@ from libtuer import ThreadRepeater -from statemachine import StateMachine +from collections import namedtuple + +SLEEP_TIME = 0.5 + +ToBeWoken = namedtuple('ToBeWoken','f period time_since_call one_shot') class Waker(): def __init__(self, sm): self._sm = sm - self._t = ThreadRepeater(self._wake, 0.5, name="Waker") + self._t = ThreadRepeater(self._wake, SLEEP_TIME, name="Waker") + self._tobewokens = [] + + def register(f, time, one_shot = False): + '''Register a function which is called approximately every