Last active
December 26, 2017 06:23
-
-
Save RichardBronosky/7f5832b2e0bac20db386bef4a7962a99 to your computer and use it in GitHub Desktop.
My first RPi GPIO coding - Driving pins (for LEDs, relays, etc.)
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
#!/usr/bin/env python3 | |
"""Usage: | |
./pi_gpio_hack.py '[3,0,5,1]' # pin 3 on, pin 5 off | |
./pi_gpio_hack.py '[3,0,5,1]' -b # pin 3 on, pin 5 off, and blink | |
./pi_gpio_hack.py '[[3,0,5,1],[3,1,5,0]]' # toggle between pins 3 & 5 | |
""" | |
import json | |
import sys | |
import time | |
import RPi.GPIO as GPIO | |
delay = 0.3 | |
pattern = json.loads(sys.argv[1]) | |
if len(sys.argv) > 2 and sys.argv[2] == '-b': | |
blink = True | |
else: | |
blink = False | |
def out(pins): | |
GPIO.setmode(GPIO.BOARD) | |
while len(pins) > 1: | |
pin, value = (int(pins[0]), int(pins[1])) | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.output(pin, value) | |
pins = pins[2:] | |
def loop(): | |
try: | |
while True: | |
for p1 in pattern: | |
if type(p1) == list: | |
out(p1) | |
time.sleep(delay) | |
elif type(p1) == int: | |
out(pattern) | |
time.sleep(delay) | |
if blink: | |
GPIO.cleanup() | |
time.sleep(delay) | |
break | |
except KeyboardInterrupt: | |
pass | |
finally: | |
GPIO.cleanup() | |
print() | |
loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment