Commit 373dfe2c authored by Constantin's avatar Constantin
Browse files

LED blinker thread

parent 90ce9cd8
from libtuer import logger
#import RPi.GPIO as GPIO
import time
import threading
import signal
class Blinker(threading.Thread):
def __init__(self, name, pin, pattern=[(False,60.0)]):
super().__init__()
self._name = name
self._pin = pin
self._pattern = pattern
# if set, pattern will be reloaded and iteration of the pattern will restart
self._notifyer = threading.Event()
# Do you want to terminate this thread? Because this is how you terminate this thread.
# Additionally you should probably set the notifyer s.t. it will terminate immediately
self._terminate = False
def run(self):
logger.debug("""LED blinker thread for LED "%s" on pin %d started.""" % (self._name,self._pin))
while not self._terminate:
assert len(self._pattern) > 0
self._notifyer.clear()
# list is copied to avoid concurrent modification exceptions
for (state, delay) in list(self._pattern):
GPIO.output(self._pin, state)
if (delay > 0):
self._notifyer.wait(delay)
if self._notifyer.is_set() or self._terminate:
break
# LEDs off in the end
GPIO.output(self._pin, False)
def _getTid(self):
return self._thread.ident
def _sendSig(self,sig):
self._terminate = True
def stop(self):
self._terminate = True
self.join()
def setPattern(self, pattern):
logger.debug("""LED "%s" got new pattern: %s""" % (self._name,str(pattern)))
assert len(pattern) > 0
self._pattern = pattern
self._notifyer.set()
class LedActor:
ledPins = {
"red" : 26,
"green" : 23,
}
def __init__(self):
# launch threads, all running the "_execute" method
self._threads = {}
for (name,pin) in ledPins.items():
logger.debug("Setting pin %d to output as \"%s\" LED, initial state: False" % (pin,name))
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, False)
self._threads[name] = Blinker(name,pin)
def setPattern(self, ledName, pattern):
self._threads[ledName].setPattern(pattern)
def stop(self):
for blinker in self._threads.values():
blinker.stop()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment