#!/usr/bin/env python

#IMPORTS
import webiopi
import time
import urllib
import os
import PicoBorgRev
PBR = PicoBorgRev.PicoBorgRev()
PBR.Init()

webiopi.setDebug()

GPIO = webiopi.GPIO

# -------------------------------------------------- #
# Left Motor Functions                               #
# -------------------------------------------------- #

def left_stop():
    SetMotor2(0)

def left_forward():
    SetMotor2(1)

def left_backward():
    SetMotor2(-0.5)

# -------------------------------------------------- #
# Right Motor Functions                              #
# -------------------------------------------------- #
def right_stop():
    SetMotor1(0)

def right_forward():
    SetMotor1(1)

def right_backward():
    SetMotor1(-0.5)
    
# -------------------------------------------------- #
def allforward():
    SetMotors(1)
        
def allbackward():
    SetMotors(-0.5)
# -------------------------------------------------- #
# Macro definition part                              #
# -------------------------------------------------- #

# switch MotorA & MotorB on forwards //forwards
@webiopi.macro
def forward():
 	allforward()	

# switch MotorA & MotorB on backwards //reverse
@webiopi.macro
def reverse():
	allbackward()

# switch MotorA on forwards & switch MotorB on Backwards // rotate right
@webiopi.macro
def right():
	left_forward()
	right_backward()

# switch MotorA on backwards & switch MotorB on forwards // rotate left
@webiopi.macro
def left():
	left_backward()
	right_forward()

# switch MotorA & MotorB off //all off
@webiopi.macro
def stop():
	left_stop()
	right_stop()
	MotorsOff()
#--------------------------------------------------#       
# text to speech
@webiopi.macro
def speak(text):
    # decode url encoded parameters
    text = urllib.parse.unquote(text)
    webiopi.debug("Speaking: %s" % (text))
    os.system('echo "' + text + '" | festival --tts')
    #subprocess.Popen(["/home/pi/Desktop/webcontrol/Speech.sh", "\"" + str(text) + "\""])
 #--------------------------------------------------#
 # shutdown & reboot buttons
@webiopi.macro
def shutdown():
    os.system("sudo shutdown -h now") # Send shutdown command to os
    time.sleep(1) # Allow a sleep time of 1 second to reduce CPU usage
    
@webiopi.macro
def reboot():
    os.system("sudo shutdown -r now") # Send reboot command to os
    time.sleep(1) # Allow a sleep time of 1 second to reduce CPU usage    

 #--------------------------------------------------#     
 #initialisation section							#
 #--------------------------------------------------#   

# Called by WebIOPi at script loading
 # Setup GPIOs
#def setup():
    #GPIO.setFunction(1, GPIO.OUT)
    #GPIO.setFunction(2, GPIO.OUT)
    #GPIO.setFunction(3, GPIO.OUT)
    #GPIO.setFunction(4, GPIO.PWM)
    #GPIO.setFunction(5, GPIO.OUT)
    #GPIO.setFunction(6, GPIO.OUT)
    
    #set_speed(1)
    #stop()

# Called by WebIOPi at server shutdown
# Reset GPIO functions
#ef destroy():
    #GPIO.setFunction(1, GPIO.IN)
    #GPIO.setFunction(2, GPIO.IN)
    #GPIO.setFunction(3, GPIO.IN)
    #GPIO.setFunction(4, GPIO.IN)
    #GPIO.setFunction(5, GPIO.IN)
    #GPIO.setFunction(6, GPIO.IN)