# This will move channel 0 from min to max position repeatedly.
# Author: Tony DiCola (with additions by Patrick Peters)
# License: Public Domain
from __future__ import division
import time

# Import the PCA9685 module.
import Adafruit_PCA9685

# Import sys module for accepting variables
import sys


# Uncomment to enable debug output.
#import logging
#logging.basicConfig(level=logging.DEBUG)

# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()

# Alternatively specify a different address and/or bus:
#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)

#Time to set variables for which servo to use and what location 
#to move to.
servo_number = sys.argv[1]
location = sys.argv[2]

#The location_list compares the location variable with the 
#13 locations I have on the clock face
#PWM list is the PWM off values I need to enter to point in the right directions
location_list = ['Home','Lessons','Concert','Lost','Tavern','Mortal Peril','Opera','Siblings','Travel','Doctors','Work','Parents','School']
PWM_list = [246, 287, 328, 369, 410, 450, 491, 532, 573, 614, 655, 696, 717]

for idx, location in enumerate(location_list):
	PWM_index = idx
	servo_max = PWM_list[idx]

# Configure min and max servo pulse lengths
servo_min = 246  # Min pulse length out of 4096. This sets you back to Home


# Helper function to make setting a servo pulse width simpler.
def set_servo_pulse(channel, pulse):
    pulse_length = 1000000    # 1,000,000 us per second
    pulse_length //= 60       # 60 Hz
    print('{0}us per period'.format(pulse_length))
    pulse_length //= 4096     # 12 bits of resolution
    print('{0}us per bit'.format(pulse_length))
    pulse *= 1000
    pulse //= pulse_length
    pwm.set_pwm(channel, 0, pulse)

# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)

print('Moving servo on channel 0, press Ctrl-C to quit...')
while True:
    # Move servo on channel O between extremes.
    pwm.set_pwm(servo_number, 0, servo_min)
    time.sleep(1)
    pwm.set_pwm(servo_number, 0, servo_max)
    time.sleep(1)