Last active
August 29, 2015 14:04
-
-
Save ticklemynausea/d980b6c9f10d01f062ed to your computer and use it in GitHub Desktop.
Rasberry Pi led test
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 | |
import threading | |
import signal | |
import sys | |
import os | |
counter = 0 | |
mode = 0 | |
GPIO_num = [11, 12, 13, 15, 16] | |
GPIO_val = [False] * len(GPIO_num) | |
def init(): | |
print "Hi! Pid %s" % os.getpid() | |
# init | |
def gpio_init(): | |
global GPIO_num | |
print "Init GPIO" | |
GPIO.setmode(GPIO.BOARD) | |
for num in GPIO_num: | |
GPIO.setup(num, GPIO.OUT) | |
#cleanup | |
def cleanup(): | |
print "Cleanup GPIO" | |
GPIO.cleanup() | |
def sigint_handler(signal, frame): | |
print "Cleaning up..." | |
cleanup() | |
sys.exit(0) | |
def sigint_init(): | |
print "Init SIGINT" | |
signal.signal(signal.SIGINT, sigint_handler) | |
def pattern(counter, n): | |
def pattern_binary(counter, n): | |
return counter & (1 << len(GPIO_num) - n) > 0 | |
def pattern_binary_blank(counter, n): | |
return counter & (1 << len(GPIO_num) - n) == 0 | |
def pattern_move(counter, n): | |
return counter % len(GPIO_num) == n | |
def pattern_move_backwards(counter, n): | |
return counter % len(GPIO_num) == len(GPIO_num) - n - 1 | |
def pattern_move_blank(counter, n): | |
return counter % len(GPIO_num) != n | |
def pattern_move_blank_backwards(counter, n): | |
return counter % len(GPIO_num) != len(GPIO_num) - n - 1 | |
global mode | |
if mode == 0: | |
return pattern_binary(counter, n) | |
elif mode == 1: | |
return pattern_binary_blank(counter, n) | |
elif mode == 2: | |
return pattern_move(counter, n) | |
elif mode == 3: | |
return pattern_move_backwards(counter, n) | |
elif mode == 4: | |
return pattern_move_blank(counter, n) | |
elif mode == 5: | |
return pattern_move_blank_backwards(counter, n) | |
raise | |
def routine(): | |
global GPIO_num, GPIO_val, counter, mode | |
for i in range(0, len(GPIO_num)): | |
GPIO_val[i] = pattern(counter, i) | |
for i in range(0, len(GPIO_num)): | |
GPIO.output(GPIO_num[i], GPIO_val[i]) | |
counter = counter + 1 | |
if counter % 65 == 0: | |
mode = (mode + 1) % 6 | |
routine_init() | |
def routine_init(): | |
threading.Timer(0.05, routine).start() | |
init() | |
gpio_init() | |
sigint_init() | |
routine_init() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment