-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathchippyRuxpin.py
116 lines (98 loc) · 3.83 KB
/
chippyRuxpin.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
#!/usr/bin/python
# Chippy Ruxpin by Next Thing Co
# Powered by C.H.I.P., the world's first $9 computer!
# apt-get install python-setuptools python-dev build-essential espeak alsa-utils
# apt-get install python-alsaaudio python-numpy python-twitter python-bottle mplayer
# IMPORTANT NOTE ABOUT TWITTER STUFF!
# In order to retrieve tweets, you need to authorize this code to use your twitter account.
# This involves obtaining some special tokens that are specific to you.
# Please visit Twitter's website to obtain this information and put the values in the variables below.
# For more information, visit this URL:
# https://dev.twitter.com/oauth/overview/application-owner-access-tokens
consumerKey='INSERT YOUR CONSUMER KEY HERE FROM TWITTER'
consumerSecret='INSERT YOUR CONSUMER SECRET HERE FROM TWITTER'
accessTokenKey='INSERT YOUR ACCESS TOKEN KEY HERE FROM TWITTER'
accessTokenSecret='INSERT YOUR ACCESS TOKEN SECRET HERE FROM TWITTER'
import sys
import time
import subprocess
import os
from random import randint
from threading import Thread
from chippyRuxpin_audioPlayer import AudioPlayer
from chippyRuxpin_gpio import GPIO
from chippyRuxpin_twitter import ChippyTwitter
from chippyRuxpin_webFramework import WebFramework
fullMsg = ""
MOUTH_OPEN = 408 # GPIO pin assigned to open the mouth. XIO-P0
MOUTH_CLOSE = 412 # GPIO pin assigned to close the mouth. XIO-P2
EYES_OPEN = 410 # GPIO pin assigned to open the eyes. XIO-P4
EYES_CLOSE = 414 # GPIO pin assigned to close the eyes. XIO-P6
io = GPIO() #Establish connection to our GPIO pins.
io.setup( MOUTH_OPEN )
io.setup( EYES_OPEN )
io.setup( MOUTH_CLOSE )
io.setup( EYES_CLOSE )
audio = None
isRunning = True
def updateMouth():
lastMouthEvent = 0
lastMouthEventTime = 0
while( audio == None ):
time.sleep( 0.1 )
while isRunning:
if( audio.mouthValue != lastMouthEvent ):
lastMouthEvent = audio.mouthValue
lastMouthEventTime = time.time()
if( audio.mouthValue == 1 ):
io.set( MOUTH_OPEN, 1 )
io.set( MOUTH_CLOSE, 0 )
else:
io.set( MOUTH_OPEN, 0 )
io.set( MOUTH_CLOSE, 1 )
else:
if( time.time() - lastMouthEventTime > 0.4 ):
io.set( MOUTH_OPEN, 0 )
io.set( MOUTH_CLOSE, 0 )
# A routine for blinking the eyes in a semi-random fashion.
def updateEyes():
while isRunning:
io.set( EYES_CLOSE, 1 )
io.set( EYES_OPEN, 0 )
time.sleep(0.4)
io.set( EYES_CLOSE, 0 )
io.set( EYES_OPEN, 1 )
time.sleep(0.4)
io.set( EYES_CLOSE, 1 )
io.set( EYES_OPEN, 0 )
time.sleep(0.4)
io.set( EYES_CLOSE, 0 )
io.set( EYES_OPEN, 0 )
time.sleep( randint( 0,7) )
def talk(myText):
if( myText.find( "twitter" ) >= 0 ):
myText += "0"
myText = myText[7:-1]
try:
myText = twitter.getTweet( myText )
except:
print( "!!!ERROR: INVALID TWITTER CREDENTIALS. Please read README.md for instructions.")
return
os.system( "espeak \",...\" 2>/dev/null" ) # Sometimes the beginning of audio can get cut off. Insert silence.
time.sleep( 0.5 )
os.system( "espeak -w speech.wav \"" + myText + "\" -s 130" )
audio.play("speech.wav")
return myText
mouthThread = Thread(target=updateMouth)
mouthThread.start()
eyesThread = Thread(target=updateEyes)
eyesThread.start()
audio = AudioPlayer()
if( consumerKey.find( 'TWITTER' ) >= 0 ):
print( "WARNING: INVALID TWITTER CREDENTIALS. Please read README.md for instructions." )
else:
twitter = ChippyTwitter(consumerKey,consumerSecret,accessTokenKey,accessTokenSecret)
web = WebFramework(talk)
isRunning = False
io.cleanup()
sys.exit(1)