Skip to content

Instantly share code, notes, and snippets.

@xerp
Last active September 18, 2015 17:53
Show Gist options
  • Select an option

  • Save xerp/e18024cd9a6c9fe03679 to your computer and use it in GitHub Desktop.

Select an option

Save xerp/e18024cd9a6c9fe03679 to your computer and use it in GitHub Desktop.

Revisions

  1. xerp revised this gist Sep 18, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import RPi.GPIO as pins
    from components import Semaphore

    s = Semaphore(pins,red_pin,yellow_pin,green_pin)
    s.start(10,2,10)
  2. xerp renamed this gist Sep 18, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. xerp revised this gist Sep 18, 2015. No changes.
  4. xerp revised this gist Sep 18, 2015. No changes.
  5. xerp revised this gist Sep 18, 2015. No changes.
  6. xerp renamed this gist Sep 18, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. xerp created this gist Sep 18, 2015.
    41 changes: 41 additions & 0 deletions components.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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)