Last active
September 18, 2015 17:53
-
-
Save xerp/e18024cd9a6c9fe03679 to your computer and use it in GitHub Desktop.
Semaphore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
class LED: | |
def __init__(self,gpio,pin): | |
gpio.setmode(GPIO.BCM) | |
gpio.cleanup(pin) | |
gpio.setup(pin,GPIO.OUT) | |
self.__gpio = gpio | |
self.__pin = pin | |
def on(self): | |
self.__gpio.output(self.__pin,True) | |
def off(self): | |
self.__gpio.output(self.__pin,False) | |
class Semaphore: | |
def __init__(self,gpio,red_pin,yellow_pin,green_pin): | |
gpio.setmode(GPIO.BCM) | |
self._led_red = LED(gpio,red_pin) | |
self._led_yellow = LED(gpio,yellow_pin) | |
self._led_green = LED(gpio,green_pin) | |
def reset(self): | |
self._led_red.off() | |
self._led_yellow.off() | |
self._led_green.off() | |
def __led(self,color,led_time): | |
led = getattr(self,'_led_{0}'.format(color)) | |
if isinstance(led,LED): | |
led.on() | |
time.sleep(led_time) | |
led.off() | |
def start(self,red_time,yellow_time,green_time): | |
while True: | |
self.__led('red',red_time) | |
self.__led('yellow',yellow_time) | |
self.__led('green',green_time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as pins | |
from components import Semaphore | |
s = Semaphore(pins,red_pin,yellow_pin,green_pin) | |
s.start(10,2,10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment