add a ThreadFunction class and use it
[saartuer.git] / ringd
diff --git a/ringd b/ringd
index 201322f4f640ff80f46d594e1946578e5b08ec12..b6a27f940ea9fe5ebccd23a77ba07e439bfce699 100755 (executable)
--- a/ringd
+++ b/ringd
@@ -1,7 +1,7 @@
 #!/usr/bin/python3
 import time, socket, atexit
 import queue, threading, select
-from libtuer import log
+from libtuer import log, ThreadFunction
 import RPi.GPIO as GPIO
 GPIO.setmode(GPIO.BOARD)
 atexit.register(GPIO.cleanup)
@@ -9,6 +9,7 @@ atexit.register(GPIO.cleanup)
 tuerSock = "/run/tuer.sock"
 ringPin = 18
 
+
 # Main classes
 class PinWatcher():
        def __init__(self, pin, histlen):
@@ -21,17 +22,8 @@ class PinWatcher():
                self._newstate = None # != None iff we are currently seeing a state change
                self._newstatelen = 0 # only valid if newstate != None
                # start state change handler thread
-               self._q = queue.Queue()
-               self._t = threading.Thread(target=self.queue_consumer)
-               self._t.start()
-       
-       def queue_consumer(self):
-               while True:
-                       el = self._q.get()
-                       if el is None: return # we are supposed to terminate
-                       # handle the state change
-                       (oldstate, newstate) = el
-                       self.callback(oldstate, newstate)
+               self._callback = ThreadFunction(self.callback)
+               self.stop = self._callback.stop
        
        def read(self):
                curstate = GPIO.input(self._pin)
@@ -42,7 +34,7 @@ class PinWatcher():
                                # we already saw this new state
                                self._newstatelen += 1
                                if self._newstatelen >= self._histlen:
-                                       self._q.put((self._state, curstate)) # send stuff to the other thread
+                                       self._callback(self._state, curstate) # send stuff to the other thread
                                        self._state = curstate
                                        self._newstate = None
                        else:
@@ -52,10 +44,6 @@ class PinWatcher():
                else:
                        # old state is preserved
                        self._newstate = None
-       
-       def quit(self):
-               self._q.put(None)
-               self._t.join()
 
 class RingWatcher(PinWatcher):
        def __init__(self):
@@ -96,4 +84,4 @@ try:
                time.sleep(0.02)
 except KeyboardInterrupt:
        for pin in pins:
-               pin.quit()
+               pin.stop()