#!/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)

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:
        for pin in pins:
                print "setup pin",pin
        direction = int(sys.argv[1])
else:
        #direction = 1
        print ""
        print " PIN LAYOUT"
        print " IN1 -> GPIO14"
        print " IN2 -> GPIO10"
        print " IN3 -> GPIO11"
        print " IN4 -> GPIO09"
        print ""
        print " Usage: rot direction cycles"
        print ""
        print " Example: rot 1 1.5"
        print " // will rotate 540 degree clockwise"
        print ""
        print " Example: rot -1 0.125"
        print " // will rotate 45 degree counter clockwise"
        print ""
        GPIO.cleanup()
        sys.exit(0)

step_counter = 0

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

for i in range(int(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()

