|
# Requires: RPi.GPIO |
|
|
|
import RPi.GPIO as GPIO |
|
|
|
from threading import Event, Thread, Timer |
|
|
|
|
|
class ImprovedThread(Thread): |
|
def __init__(self, *args, **kwargs): |
|
super(ImprovedThread, self).__init__(*args, **kwargs) |
|
self._stopEvent = Event() |
|
|
|
def stop(self): |
|
self._stopEvent.set() |
|
|
|
def is_stopped(self): |
|
return self._stopEvent.isSet() |
|
|
|
def wait(self, duration): |
|
self._stopEvent.wait(duration); |
|
|
|
|
|
class LEDFlasher(ImprovedThread): |
|
def __init__(self, pin, duration): |
|
super(LEDFlasher, self).__init__(target=self._flashLED) |
|
self.setDaemon(True) |
|
|
|
self._pin = pin |
|
self._duration = duration |
|
self._state = False |
|
|
|
GPIO.setmode(GPIO.BOARD) |
|
GPIO.setup(self._pin, GPIO.OUT) |
|
|
|
self.start() |
|
|
|
def _flashLED(self): |
|
while not self.is_stopped(): |
|
GPIO.output(self._pin, self._state) |
|
self._state = not self._state |
|
self.wait(self._duration/2) |
|
|
|
flasher = LEDFlasher(7, 0.5) |
|
Timer(60, flasher.stop).start() |
|
|
|
flasher.join() |
|
|
|
GPIO.cleanup() |