from __future__ import division
import time
import subprocess
import shlex

import Adafruit_PCA9685

pwm = Adafruit_PCA9685.PCA9685()

servo_min = 150
servo_max = 550

p=subprocess.Popen(shlex.split('./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so"'))

def set_servo(channel, pulse):
    pulse_length = 1000000
    pulse_length //= 60
    pulse_length //= 4096
    pulse *= 1000
    pulse //= pulse_length
    pwm.set_pwm(channel, 0, pulse)
    
pwm.set_pwm_freq(60)

try:
    while True:
        pwm.set_pwm(0,0,servo_min)
        time.sleep(30)
	pwm.set_pwm(0,0,350)
	time.sleep(30)
        pwm.set_pwm(0,0,servo_max)
        time.sleep(30)
except KeyboardInterrupt:
    p.terminate()
