#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import sys

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

pins = [14,10,11,9]

GPIO.setup(pins,GPIO.OUT)
GPIO.output(pins, GPIO.LOW)

for pin in pins:
        print "setup pin",pin

seq = [ [1,0,0,0],
        [1,1,0,0],
        [0,1,0,0],
        [0,1,1,0],
        [0,0,1,0],
        [0,0,1,1],
        [0,0,0,1],
        [1,0,0,1] ]


step_count = len(seq)
if len(sys.argv)>1:
        direction = int(sys.argv[1])
else:
        direction = 1
step_counter = 0

if len(sys.argv)>2:
        radius = 512 * int(sys.argv[2])
else:
        radius = 512

for i in range(radius):
        print i+1,"from ",radius
        for halfstep in range(8):
                for pin in range(4):
                        if seq[step_counter][pin]!=0:
                                GPIO.output(pins[pin], True)
                        else:
                                GPIO.output(pins[pin], False)

                step_counter += direction

                if (step_counter>=step_count):
                    step_counter = 0
                if (step_counter<0):
                    step_counter = step_count+direction

                time.sleep(0.001)

GPIO.cleanup()

