add square and linear modes to blinkenlights
This commit is contained in:
parent
0923eef297
commit
5d5c3ff74e
|
@ -3,7 +3,7 @@ Controls an LED using a GPIO pin
|
||||||
'''
|
'''
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import time, logging
|
import time, logging
|
||||||
from threading import Event, Lock
|
from threading import Event
|
||||||
from exceptionThreading import ExceptionThread
|
from exceptionThreading import ExceptionThread
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
|
@ -13,8 +13,14 @@ class Blinkenlights(ExceptionThread):
|
||||||
def __init__(self, pin, cyclePeriod=2):
|
def __init__(self, pin, cyclePeriod=2):
|
||||||
self._stopper = Event()
|
self._stopper = Event()
|
||||||
self._blink = Event()
|
self._blink = Event()
|
||||||
self._lock = Lock()
|
self._linear = Event()
|
||||||
|
|
||||||
|
# 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
|
||||||
|
self._stepsize = 100/(self._steps/2)
|
||||||
|
|
||||||
self._pin = pin
|
self._pin = pin
|
||||||
|
|
||||||
self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds
|
self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds
|
||||||
|
@ -22,13 +28,35 @@ class Blinkenlights(ExceptionThread):
|
||||||
GPIO.setup(pin, GPIO.OUT)
|
GPIO.setup(pin, GPIO.OUT)
|
||||||
pwm = GPIO.PWM(self._pin, 60)
|
pwm = GPIO.PWM(self._pin, 60)
|
||||||
|
|
||||||
|
def linearLoop():
|
||||||
|
for dc in chain(range(100, -1, -self._stepsize), range(0, 101, self._stepsize)):
|
||||||
|
t = (self._linear.is_set(), self._blink.is_set())
|
||||||
|
if t == (True, True):
|
||||||
|
pwm.ChangeDutyCycle(dc)
|
||||||
|
time.sleep(self._sleeptime)
|
||||||
|
else:
|
||||||
|
return t
|
||||||
|
return (True, True)
|
||||||
|
|
||||||
def blinkLights():
|
def blinkLights():
|
||||||
pwm.start(0)
|
pwm.start(0)
|
||||||
while not self._stopper.isSet():
|
while not self._stopper.isSet():
|
||||||
if self._blink.is_set():
|
if self._blink.is_set():
|
||||||
for dc in chain(range(100, -1, -5), range(0, 101, 5)):
|
linearSet, blinkSet = linearLoop()
|
||||||
pwm.ChangeDutyCycle(dc)
|
|
||||||
time.sleep(self._sleeptime)
|
if not blinkSet:
|
||||||
|
continue
|
||||||
|
elif not linearSet:
|
||||||
|
t = self._sleeptime*self._stepsize
|
||||||
|
|
||||||
|
pwm.ChangeDutyCycle(100)
|
||||||
|
self._linear.wait(timeout=t)
|
||||||
|
|
||||||
|
if self._linear.is_set() or not self._blink.is_set():
|
||||||
|
continue
|
||||||
|
|
||||||
|
pwm.ChangeDutyCycle(0)
|
||||||
|
self._linear.wait(timeout=t)
|
||||||
else:
|
else:
|
||||||
pwm.ChangeDutyCycle(100)
|
pwm.ChangeDutyCycle(100)
|
||||||
self._blink.wait()
|
self._blink.wait()
|
||||||
|
@ -44,15 +72,25 @@ class Blinkenlights(ExceptionThread):
|
||||||
if self.is_alive():
|
if self.is_alive():
|
||||||
self._stopper.set()
|
self._stopper.set()
|
||||||
self._blink.set()
|
self._blink.set()
|
||||||
|
self._linear.set()
|
||||||
logger.debug('Stopping LED on pin %s', self._pin)
|
logger.debug('Stopping LED on pin %s', self._pin)
|
||||||
|
|
||||||
def setCyclePeriod(self, cyclePeriod):
|
def setCyclePeriod(self, cyclePeriod):
|
||||||
with self._lock:
|
self._sleeptime = cyclePeriod/self._steps
|
||||||
self._sleeptime = cyclePeriod/20/2
|
|
||||||
|
def setLinear(self, toggle):
|
||||||
|
if toggle:
|
||||||
|
self._linear.set()
|
||||||
|
else:
|
||||||
|
self._linear.clear()
|
||||||
|
|
||||||
def setBlink(self, toggle):
|
def setBlink(self, toggle):
|
||||||
if toggle:
|
if toggle:
|
||||||
self._blink.set()
|
self._blink.set()
|
||||||
|
# unblock the _linear Event if threads are waiting on it
|
||||||
|
if not self._linear.is_set():
|
||||||
|
self._linear.set()
|
||||||
|
self._linear.clear()
|
||||||
else:
|
else:
|
||||||
self._blink.clear()
|
self._blink.clear()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue