2017-06-11 18:45:23 -04:00
|
|
|
'''
|
|
|
|
Controls an LED using a GPIO pin
|
|
|
|
'''
|
2016-12-30 02:51:56 -05:00
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
import time, logging
|
2017-06-02 02:17:32 -04:00
|
|
|
from threading import Event
|
|
|
|
from exceptionThreading import ExceptionThread
|
2016-12-30 02:51:56 -05:00
|
|
|
from itertools import chain
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-06-02 02:17:32 -04:00
|
|
|
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-11 18:45:23 -04:00
|
|
|
self._blink = Event()
|
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-11 18:45:23 -04:00
|
|
|
if self._blink.is_set():
|
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)
|
2017-06-07 01:42:58 -04:00
|
|
|
|
|
|
|
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)
|
2017-06-07 01:42:58 -04:00
|
|
|
|
|
|
|
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):
|
2017-06-11 18:45:23 -04:00
|
|
|
if toggle:
|
|
|
|
self._blink.set()
|
|
|
|
else:
|
|
|
|
self._blink.clear()
|
2017-06-08 02:15:50 -04:00
|
|
|
|
2016-12-30 02:51:56 -05:00
|
|
|
def __del__(self):
|
|
|
|
self.stop()
|