#!/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)
tuerSock = "/run/tuer.sock"
ringPin = 18
+
# Main classes
class PinWatcher():
def __init__(self, pin, histlen):
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)
# 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:
else:
# old state is preserved
self._newstate = None
-
- def quit(self):
- self._q.put(None)
- self._t.join()
class RingWatcher(PinWatcher):
def __init__(self):
time.sleep(0.02)
except KeyboardInterrupt:
for pin in pins:
- pin.quit()
+ pin.stop()