#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import json
import RPi.GPIO as GPIO
import time
from random import randint

servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50)  # GPIO 17 for PWM with 50Hz
p.start(2.5)  # Initialization

try:
    while True:

        p.ChangeDutyCycle(4)

        # Collect all the names of the images in out folder and the amount
        names = [name for name in os.listdir('/home/pi/Desktop/corporate/audio')]
        total = len([name for name in os.listdir('/home/pi/Desktop/corporate/audio')])

        # Generate a random number, -1 because our index start at 0
        random = randint(0, total-1)
        print(total)

        # Fetch the name belonging to this random number
        name = '/home/pi/Desktop/corporate/audio/'+names[random]
        print(name)

        #Play the audio
        os.system('mpg123 ' + name)
        time.sleep(0.2)
        p.ChangeDutyCycle(5)
        sleep = randint(10, 60)
        print(sleep)
        time.sleep(5)

except KeyboardInterrupt:

    p.stop()


		