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-18 02:34:18 -04:00
|
|
|
from threading import Event
|
2017-06-02 02:17:32 -04:00
|
|
|
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):
|
2017-06-18 16:03:39 -04:00
|
|
|
'''
|
|
|
|
Controls one LED on a GPIO pin. LED brightness can be control via
|
|
|
|
pulse-width modulation (pwm) and can be set to a constant brightness or
|
|
|
|
fluctuate as a triangle wave or square wave, each with varying periods.
|
|
|
|
'''
|
2016-12-30 02:51:56 -05:00
|
|
|
def __init__(self, pin, cyclePeriod=2):
|
|
|
|
self._stopper = Event()
|
2017-06-17 14:26:29 -04:00
|
|
|
self._blink = Event()
|
2017-06-18 16:03:39 -04:00
|
|
|
self._triangle = Event()
|
2017-06-17 14:26:29 -04:00
|
|
|
|
2017-06-18 02:34:18 -04:00
|
|
|
# number of pwm adjustments madeper duty cycle, note stepsize is in half
|
|
|
|
# because we spend first half of period decreasing duty cycle and the
|
|
|
|
# second half increasing (between 0 and 100)
|
|
|
|
self._steps = 40
|
2017-06-18 15:00:01 -04:00
|
|
|
self._stepsize = int(100/(self._steps/2))
|
2017-06-18 02:34:18 -04:00
|
|
|
|
2016-12-30 02:51:56 -05:00
|
|
|
self._pin = pin
|
|
|
|
|
|
|
|
self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds
|
|
|
|
|
|
|
|
GPIO.setup(pin, GPIO.OUT)
|
|
|
|
pwm = GPIO.PWM(self._pin, 60)
|
|
|
|
|
2017-06-18 16:03:39 -04:00
|
|
|
def triangleLoop():
|
|
|
|
'''
|
|
|
|
Controls the brightness in triangle-wave mode. Note that this will
|
|
|
|
exit as soon as _triangle or _blink events are cleared...this may
|
|
|
|
seem convoluted but is necessary to ensure clean response times
|
|
|
|
when the mode is changed
|
|
|
|
'''
|
2017-06-18 02:34:18 -04:00
|
|
|
for dc in chain(range(100, -1, -self._stepsize), range(0, 101, self._stepsize)):
|
2017-06-18 16:03:39 -04:00
|
|
|
t = (self._triangle.is_set(), self._blink.is_set())
|
2017-06-18 02:34:18 -04:00
|
|
|
if t == (True, True):
|
|
|
|
pwm.ChangeDutyCycle(dc)
|
|
|
|
time.sleep(self._sleeptime)
|
|
|
|
else:
|
|
|
|
return t
|
|
|
|
return (True, True)
|
|
|
|
|
2016-12-30 02:51:56 -05:00
|
|
|
def blinkLights():
|
2017-06-18 16:03:39 -04:00
|
|
|
'''
|
|
|
|
Uses mode to control brightness. This function has three phases in
|
|
|
|
its lifetime:
|
|
|
|
1) start PWM on the GPIO pin
|
|
|
|
2) brightness control loop, which exits on setting _stopper event
|
|
|
|
3) stop PWM (if this doesn't happen we segfault)
|
|
|
|
|
|
|
|
Within the brightness control loop, flow is controled by events,
|
|
|
|
which ensure good response times when we transition b/t states as
|
|
|
|
well as high cpu efficiency (no busy waits).
|
|
|
|
'''
|
2016-12-30 02:51:56 -05:00
|
|
|
pwm.start(0)
|
|
|
|
while not self._stopper.isSet():
|
2017-06-11 18:45:23 -04:00
|
|
|
if self._blink.is_set():
|
2017-06-18 16:03:39 -04:00
|
|
|
triangleSet, blinkSet = triangleLoop()
|
2017-06-18 02:34:18 -04:00
|
|
|
|
|
|
|
if not blinkSet:
|
|
|
|
continue
|
2017-06-18 16:03:39 -04:00
|
|
|
elif not triangleSet:
|
2017-06-18 15:00:01 -04:00
|
|
|
t = self._sleeptime*self._steps/2
|
2017-06-18 02:34:18 -04:00
|
|
|
|
|
|
|
pwm.ChangeDutyCycle(100)
|
|
|
|
|
2017-06-18 16:11:31 -04:00
|
|
|
if self._triangle.wait(timeout=t) or not self._blink.is_set():
|
2017-06-18 02:34:18 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
pwm.ChangeDutyCycle(0)
|
2017-06-18 16:03:39 -04:00
|
|
|
self._triangle.wait(timeout=t)
|
2016-12-30 02:51:56 -05:00
|
|
|
else:
|
|
|
|
pwm.ChangeDutyCycle(100)
|
2017-06-17 14:44:29 -04:00
|
|
|
self._blink.wait()
|
2017-06-18 16:03:39 -04:00
|
|
|
pwm.stop()
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
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()
|
2017-06-17 14:44:29 -04:00
|
|
|
self._blink.set()
|
2017-06-18 16:03:39 -04:00
|
|
|
self._triangle.set()
|
2017-06-10 01:52:06 -04:00
|
|
|
logger.debug('Stopping LED on pin %s', self._pin)
|
2017-06-07 01:42:58 -04:00
|
|
|
|
|
|
|
def setCyclePeriod(self, cyclePeriod):
|
2017-06-18 02:34:18 -04:00
|
|
|
self._sleeptime = cyclePeriod/self._steps
|
|
|
|
|
2017-06-18 16:03:39 -04:00
|
|
|
def setTriangle(self, toggle):
|
2017-06-18 02:34:18 -04:00
|
|
|
if toggle:
|
2017-06-18 16:03:39 -04:00
|
|
|
self._triangle.set()
|
2017-06-18 02:34:18 -04:00
|
|
|
else:
|
2017-06-18 16:03:39 -04:00
|
|
|
self._triangle.clear()
|
2017-06-18 02:34:18 -04: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()
|
2017-06-18 16:03:39 -04:00
|
|
|
# unblock the _triangle Event if threads are waiting on it
|
|
|
|
if not self._triangle.is_set():
|
|
|
|
self._triangle.set()
|
|
|
|
self._triangle.clear()
|
2017-06-11 18:45:23 -04:00
|
|
|
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()
|