pyledriver/blinkenLights.py

52 lines
1.3 KiB
Python
Raw Normal View History

2016-12-30 02:51:56 -05:00
import RPi.GPIO as GPIO
import time, logging
from threading import Event
from exceptionThreading import ExceptionThread
2016-12-30 02:51:56 -05:00
from itertools import chain
logger = logging.getLogger(__name__)
class Blinkenlights(ExceptionThread):
2016-12-30 02:51:56 -05:00
def __init__(self, pin, cyclePeriod=2):
self._stopper = Event()
self._pin = pin
2017-06-08 02:15:50 -04:00
self._blink = False
2016-12-30 02:51:56 -05:00
self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds
GPIO.setup(pin, GPIO.OUT)
pwm = GPIO.PWM(self._pin, 60)
def blinkLights():
pwm.start(0)
while not self._stopper.isSet():
t = self._sleeptime
2017-06-08 02:15:50 -04:00
if self._blink:
2016-12-30 02:51:56 -05:00
for dc in chain(range(100, -1, -5), range(0, 101, 5)):
pwm.ChangeDutyCycle(dc)
time.sleep(t)
else:
pwm.ChangeDutyCycle(100)
time.sleep(t)
pwm.stop() # required to avoid core dumps when process terminates
super().__init__(target=blinkLights, daemon=True)
def start(self):
ExceptionThread.start(self)
2016-12-30 02:51:56 -05:00
logger.debug('Starting LED on pin %s', self._pin)
def stop(self):
2017-06-10 01:52:06 -04:00
if self.is_alive():
self._stopper.set()
logger.debug('Stopping LED on pin %s', self._pin)
def setCyclePeriod(self, cyclePeriod):
self._sleeptime = cyclePeriod/20/2
2016-12-30 02:51:56 -05:00
2017-06-08 02:15:50 -04:00
def setBlink(self, toggle):
self._blink = toggle
2016-12-30 02:51:56 -05:00
def __del__(self):
self.stop()