-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTKilgore.py
305 lines (225 loc) · 5.16 KB
/
TKilgore.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# TEST version doesn't do GPIO
# Kilgore Trout
#-- Tools --
# Location
#/home/pi/trout
# Terminal
#H:\DROPBOX\Dropbox\DEV\RaspPi\putty
#username: pi
#password: raspberry
# Transfer files (same login)
#D:\Program Files (x86)\FileZilla FTP Client\filezilla.exe
# Test audio
#aplay file.wav
#-- Hardware GPIO --
# 7 -> LED
# 8,9 Mouth
# 10,11 Turn
# 12,13 Tail
# Power
# Need 3Amp power hooked up to micro USB + motors + Audio amp / speakers
import signal
import sys
import os
#import RPi.GPIO as GPIO
import time
import wave
import getopt
#import alsaaudio
import random
#-- Defines --
TAIL = 0
HEAD0 = 1
HEAD1 = 2
MOUTH = 3
#-- Data --
pins = [11, 15, 16, 18]
# array of fortune wav sounds
fortunes = ['03','05','06','07','10','12','14','16','17','18','19','21','22','26','27','30','32']
#-- Code --
#classes
class ProfileTimer:
#
function = time.clock
# is providing a start time
@staticmethod
def value():
return Timer.function()
# calculating delta between current time and start time.
@staticmethod
def delta(start):
return Timer.function() - start
class Timer:
#
function = time.time
# is providing a start time
@staticmethod
def value():
return Timer.function()
# calculating delta between current time and start time.
@staticmethod
def delta(start):
return Timer.function() - start
#subroutines
#def setSampWidth(f):
# 8bit is unsigned in wav files
# if f.getsampwidth() == 1:
# device.setformat(alsaaudio.PCM_FORMAT_U8)
# Otherwise we assume signed data, little endian
# elif f.getsampwidth() == 2:
# device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
# elif f.getsampwidth() == 3:
# device.setformat(alsaaudio.PCM_FORMAT_S24_LE)
# elif f.getsampwidth() == 4:
# device.setformat(alsaaudio.PCM_FORMAT_S32_LE)
# else:
# raise ValueError('Unsupported format')
def headUp():
print "headUp"
#GPIO.output(pins[HEAD0], True)
#GPIO.output(pins[HEAD1], False)
def headDown():
print "headDown"
#GPIO.output(pins[HEAD0], False)
#GPIO.output(pins[HEAD1], True)
def tailStart():
print "tailStart"
#GPIO.output(pins[TAIL], True)
def tailStop():
print "tailStop"
#GPIO.output(pins[TAIL], False)
def mouthStart():
print "mouthStart"
#GPIO.output(pins[MOUTH], True)
def mouthStop():
print "mouthStop"
#GPIO.output(pins[MOUTH], False)
def headMove():
a = 1
while (a < 10000):
headUp()
s1 = Timer.value()
while Timer.delta(s1) < 2.9:
yield a
headDown()
s2 = Timer.value()
while Timer.delta(s2) < 3.0:
#print "foooo" + str(Timer.delta(s2))
yield a
yield a
def tailFlap():
#print Timer.delta(start)
a = 1
while (a < 10000):
tailStart()
s1 = Timer.value()
while Timer.delta(s1) < 0.4:
yield a
tailStop()
s2 = Timer.value()
while Timer.delta(s2) < 0.4:
yield a
yield a
def mouthFlap():
#print Timer.delta(start)
a = 1
while (a < 10000):
mouthStart()
s1 = Timer.value()
while Timer.delta(s1) < 0.2:
yield a
mouthStop()
s2 = Timer.value()
while Timer.delta(s2) < 0.3:
yield a
yield a
def resetPins():
for n in pins:
#GPIO.output(n, False)
pass
def signal_handler(signal, frame):
print 'You pressed Ctrl+C!'
resetPins()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
#
#more efficient way to get the size of array? ( I forgot how in Python )
maxfortune = 0
for file in fortunes:
maxfortune = maxfortune + 1
# setup the pins as output
for n in pins:
#GPIO.setup(n, GPIO.OUT)
#GPIO.output(n, False)
pass
#device = alsaaudio.PCM()
#card = 'default'
while 1:
print "START -------"
t0 = Timer.value()
while Timer.delta(t0) < 8.0:
pass
head = headMove()
tail = tailFlap()
mouth = mouthFlap()
fortune_pick = random.randrange(0,maxfortune)
fortuneFile = "Recording" + fortunes[fortune_pick]+".wav"
print fortuneFile
#f = wave.open('wave/Recording32.wav', 'rb')
# Set attributes
#device.setchannels(f.getnchannels())
#device.setrate(f.getframerate())
#setSampWidth(f)
#device.setperiodsize(320)
#data = f.readframes(320)
# tail flap starts it
t0 = Timer.value()
while Timer.delta(t0) < 4.0:
b = tail.next()
# head forward still tail flapping
headUp()
t0 = Timer.value()
while Timer.delta(t0) < 1.0:
b = tail.next()
# play audio with lip flap
#while data:
# Read data from stdin
#a = 1
#device.write(data)
#data = f.readframes(320)
#a = head.next()
#b = tail.next()
#c = mouth.next()
#temp version without audio
t0 = Timer.value()
while Timer.delta(t0) < 2.0:
a = head.next()
b = tail.next()
c = mouth.next()
#stop mouth and put head down
mouthStop()
headDown()
#but continue to flap tail for a few seconds
t0 = Timer.value()
while Timer.delta(t0) < 2.0:
b = tail.next()
#stop tail
tailStop()
#t0 = Timer.value()
#while Timer.delta(t0) < 0.5:
# pass
resetPins()
#f.close()
#Wait for button press
#Pick random audio sound from array
# load audio into memory 48K wave file
# Create Generator for Tail flap
# Create Generator for Turn control
# Create Generator for mouth lip sync - pass in audio raw data
# start audio playing
# update tail flap
# update turn control
# update lip sync
# until audio finished
# return fish to neutral
# To top of program