-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
122 lines (107 loc) · 3.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from __future__ import division
import speech_recognition as sr
import pygame
import time
import Adafruit_PCA9685
# sudo pip install adafruit-pca9685
pwm = Adafruit_PCA9685.PCA9685(address=0x40, busnum=1)
pwm.set_pwm_freq(60)
living = True
pygame.mixer.init()
offline = False
def main():
playSounds("intro")
playSounds("commands")
while living:
command = listen()
pickAction(command)
time.sleep(0.1)
def playSounds(name):
time.sleep(0.1)
pygame.mixer.music.load("/home/pi/mole/audio/" + name + "N.mp3")
time.sleep(0.1)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
pwm.set_pwm(1, 0, 375)
pwm.set_pwm(0, 0, 375)
pwm.set_pwm(2, 0, 250)
time.sleep(.25)
pwm.set_pwm(2, 0, 325)
time.sleep(.25)
pwm.set_pwm(1, 0, 500)
pwm.set_pwm(0, 0, 250)
pwm.set_pwm(2, 0, 250)
time.sleep(.25)
pwm.set_pwm(2, 0, 325)
time.sleep(.25)
continue
if (name == "magic"):
pwm.set_pwm(1, 0, 375)
time.sleep(0.5)
pwm.set_pwm(1, 0, 500)
time.sleep(0.5)
pwm.set_pwm(1, 0, 375)
elif (name == "dance"):
for i in range(2):
pwm.set_pwm(1, 0, 375)
pwm.set_pwm(0, 0, 250)
time.sleep(.5)
pwm.set_pwm(1, 0, 500)
pwm.set_pwm(0, 0, 375)
time.sleep(.5)
pwm.set_pwm(1, 0, 375)
def listen():
global offline
print("Ready")
pygame.mixer.music.load("/home/pi/mole/audio/readyN.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
print("Recognizing ...")
pygame.mixer.music.load("/home/pi/mole/audio/recognizing.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
if (offline):
pygame.mixer.music.load("/home/pi/mole/audio/offline.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
output = r.recognize_sphinx(audio)
else:
output = r.recognize_google(audio)
print("You said: " + output)
return output
except sr.UnknownValueError:
return "Unknown"
except sr.RequestError as e:
offline = True
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return "Error"
return "Error"
def pickAction(command):
command = command.lower()
if (command == "mole" or command == "information" or command == "chemistry"):
playSounds("mole")
elif (command == "magic"):
playSounds("magic")
elif (command == "dance"):
playSounds("dance")
elif (command == "command"):
playSounds("commands")
elif (command == "jump"):
playSounds("other")
elif (command == "Error"):
playSounds("error")
elif (command == "Unknown"):
playSounds("confused")
elif (command == "halt"):
exit()
else:
playSounds("confused")
main()