replace blink flag with event

This commit is contained in:
petrucci4prez 2017-06-11 18:45:23 -04:00
parent 839ac618ad
commit ef00f2e540
2 changed files with 10 additions and 4 deletions

View File

@ -1,3 +1,6 @@
'''
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 from threading import Event
@ -11,7 +14,7 @@ class Blinkenlights(ExceptionThread):
self._stopper = Event() self._stopper = Event()
self._pin = pin self._pin = pin
self._blink = False self._blink = Event()
self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds self.setCyclePeriod(cyclePeriod) #cyclePeriod is length of one blink cycle in seconds
GPIO.setup(pin, GPIO.OUT) GPIO.setup(pin, GPIO.OUT)
@ -21,7 +24,7 @@ class Blinkenlights(ExceptionThread):
pwm.start(0) pwm.start(0)
while not self._stopper.isSet(): while not self._stopper.isSet():
t = self._sleeptime t = self._sleeptime
if self._blink: if self._blink.is_set():
for dc in chain(range(100, -1, -5), range(0, 101, 5)): for dc in chain(range(100, -1, -5), range(0, 101, 5)):
pwm.ChangeDutyCycle(dc) pwm.ChangeDutyCycle(dc)
time.sleep(t) time.sleep(t)
@ -45,7 +48,10 @@ class Blinkenlights(ExceptionThread):
self._sleeptime = cyclePeriod/20/2 self._sleeptime = cyclePeriod/20/2
def setBlink(self, toggle): def setBlink(self, toggle):
self._blink = toggle if toggle:
self._blink.set()
else:
self._blink.clear()
def __del__(self): def __del__(self):
self.stop() self.stop()

View File

@ -11,7 +11,7 @@ the signal-originating child thread.
''' '''
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time, logging, enum, os import time, logging, enum, os
from threading import Lock from threading import Lock, Event
from functools import partial from functools import partial
from collections import namedtuple from collections import namedtuple