#!/usr/bin/env python3

# This script is an adaption of the servo_demo.py scritpt for the AIY voice HAT,
# optimized for the AIY jumping jack

import aiy.audio
import aiy.cloudspeech
import aiy.voicehat

from gpiozero import LED
from gpiozero import AngularServo
from gpiozero import Button

from time import sleep


def main():
    
 recognizer = aiy.cloudspeech.get_recognizer()

 recognizer.expect_phrase('right up')
 recognizer.expect_phrase('right down')
 recognizer.expect_phrase('Right Center') # the capital letters are there on purpose

 recognizer.expect_phrase('left up')
 recognizer.expect_phrase('left down')
 recognizer.expect_phrase('left Center')
 
 recognizer.expect_phrase('hands up')
 recognizer.expect_phrase('hands down')
 recognizer.expect_phrase('hands Center')

 recognizer.expect_phrase('Dance')

 recognizer.expect_phrase('LED on')
 recognizer.expect_phrase('LED off')

 recognizer.expect_phrase('goodbye')

 
 # button = aiy.voicehat.get_button()   # change Button status
 # led = aiy.voicehat.get_led()         # change Button-LED status

 aiy.audio.get_recorder().start()
 
 servo0 = AngularServo(26, min_angle=-40,max_angle=40) # 1st connector, GPIO 26
 servo2 = AngularServo(13, min_angle=-40,max_angle=40) # 3rd connector, GPIO 13
 led0 = LED(24)                                        # LEDs are connected to servo5/GPIO 24
 distance= Button(5)                                   # distance sensor connected to servo3/GPIO 05
# others: GPIO 6 at servo1, 12 at servo4

 aiy.audio.say("Hello!", lang="en-GB")   
 aiy.audio.say("To start, move your hand close to the sensor", lang="en-GB")

 while True:

    led0.on() # LEDs on

    print("To activate voice recognition, move a hand near to the distance sensor, then speak")
    print('Expected keywords are: hands/left/right up/down/center, ')
    print('LED on/off, dance and goodbye.')
    print()

    distance.wait_for_press()
    print('Listening...')
    aiy.audio.say("Please give your orders", lang="en-GB")    

    led0.blink()                    # light blink
    text = recognizer.recognize()

    if text is None:
       aiy.audio.say('Sorry, I did not hear you.', lang="en-GB")

    else:
        print('You said "', text, '"') # Lets you check the systems interpretation

        if 'right up' in text:
            print('Moving servo0 to maximum position')
            servo0.angle=35

        elif 'right down' in text:
            print('Moving servo0 to minimum position')
            servo0.angle=-35

        elif 'Right Center' in text:   #correct captials are critical
             print('Moving servo0 to middle position')
             servo0.angle=0

        elif 'left up' in text:
            print('Moving servo2 to maximum position')
            servo2.angle=-35

        elif 'left down' in text:
            print('Moving servo2 to minimum position')
            servo2.angle=35

        elif 'left Center' in text:
             print('Moving servo2 to middle position')
             servo2.angle=0

        elif 'hands up' in text:
            print('Moving servo2 to maximum position')
            servo2.angle=-35
            servo0.angle=35

        elif 'hands down' in text:
            print('Moving servo2 to minimum position')
            servo2.angle=35
            servo0.angle=-35

        elif 'hands Center' in text:
             print('Moving servo2 to middle position')
             servo2.angle=0
             servo0.angle=0

        elif 'LED off' in text:
             print ('switching off external LED 0') 
             led0.off()

        elif 'LED on' in text:
             print ('switching on external LED 0') 
             led0.on() # light

        elif 'dance' in text:
             print ('now performing dance number one') 
             aiy.audio.say("Well, I will try my best!", lang="en-GB") 
             
             led0.on() # lights on

             for i in range (3):

                servo0.angle=0
                servo2.angle=0
                sleep(1)

                servo0.angle=35
                servo2.angle=-35
                sleep(1)


                servo0.angle=0
                servo2.angle=-35
                sleep(1)


                servo0.angle=-25
                servo2.angle=0
                sleep(1)


                servo0.angle=30
                servo2.angle=20
                sleep(1)


                servo0.angle=0
                servo2.angle=0
             
             led0.off() # light off


        elif 'goodbye' in text:
             aiy.audio.say("Goodbye", lang="en-GB")              
             aiy.audio.say('Arrivederci', lang="it-IT")
             aiy.audio.say('Auf Wiedersehen', lang="de-DE") 
             servo0.angle=0
             servo2.angle=0
             led0.off()
             sleep (3)
             print('bye!')
             break

        else:
             print('no keyword recognized!')
             aiy.audio.say("Sorry, I did not understand you", lang="en-GB")

             
if __name__ == '__main__':
 main()
