-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_parms.py
executable file
·297 lines (244 loc) · 8.4 KB
/
key_parms.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
#!/usr/bin/python3
##!env python3
import json
import os
import RPi.GPIO as GPIO
import sys
import signal
import subprocess
from time import sleep
# Set RPi ports
key_A = 22
key_B = 27
key_C = 17
key_D = 4
spider_parmfile = "/home/pi/python/spider/spiderparms.json"
speech_loc = "/home/pi/python/spider/speech/"
voices_file = speech_loc + "speech_legend.json"
voice_volume = 40000 # range: 0 - max (1000000?)
max_volume = 1000000 # range: 0 - max (1000000?)
max_eye_intensity = 80 # max is 100, but may draw too much power.
min_eye_intensity = 40
#default values
spider_parms = {"SOUND_ON": False, "VOLUME":10000, "MAX_INT":25,
"END_REQUEST": False} # 0 <= VOLUME <= 100000+
#---------------------------------------------------------------
def get_spider_parms():
global spider_parms
try:
with open(spider_parmfile, "r") as f:
spider_parms = json.load(f)
except FileNotFoundError:
print(spider_parmfile + ": not found. Using defaults.")
# use defaults defined above
pass
#---------------------------------------------------------------
def put_spider_parms():
global spider_parms
with open(spider_parmfile, "w") as f:
json.dump(spider_parms, f)
#---------------------------------------------------------------------------------
# load voices structure
def get_voices_file():
global voices
try:
with open(voices_file, "r") as f:
voices = json.load(f)
except FileNotFoundError:
print(voices_file + ": not found. key_parms.py is now terminating.")
sys.exit()
#---------------------------------------------------------------------------------
# set sound: T/F
def set_sound():
global spider_parms
changed = False
speak("08-Press-A-Sound")
speak("11-Press-D-Exit")
while True:
if spider_parms["SOUND_ON"]:
speak("09-Sound-On")
else:
speak("10-Sound-Off")
while True:
letter = get_letter()
if letter == "A":
spider_parms["SOUND_ON"] = not spider_parms["SOUND_ON"]
changed = True
break
if letter == "D":
return changed
#---------------------------------------------------------------------------------
def volume_increase(volume):
if volume == 0:
return 1000
volume += max(int(volume * 0.25), 10000)
return round(min(volume, max_volume), -3)
def volume_decrease(volume):
volume -= max(int(volume * 0.25), 10000)
return round(max(0, volume), -3)
def set_volume():
global spider_parms
changed = False
if spider_parms["VOLUME"] == 0:
speak("10-Sound-Off")
elif spider_parms["VOLUME"] == max_volume:
speak("15-Sound-Max")
# Read menu
speak("18-Press-AC-IncrDecr")
speak("19-Press-B-Play")
speak("11-Press-D-Exit")
while True:
letter = get_letter()
if letter == "A":
spider_parms["VOLUME"] = volume_increase(spider_parms["VOLUME"])
if spider_parms["VOLUME"] == max_volume:
speak("15-Sound-Max")
else:
speak("16-Volume-Up", volume=spider_parms["VOLUME"])
changed = True
continue
if letter == "C":
spider_parms["VOLUME"] = volume_decrease(spider_parms["VOLUME"])
if spider_parms["VOLUME"] == 0:
speak("10-Sound-Off")
else:
speak("17-Volume-Down", volume=spider_parms["VOLUME"])
changed = True
continue
if letter == "B":
speak("01-Hello-Spider", volume=spider_parms["VOLUME"])
continue
if letter == "D":
return changed
#---------------------------------------------------------------------------------
# set eye intensity values.
def eye_increase(intensity):
intensity += 10
return min(intensity, max_eye_intensity)
def eye_decrease(intensity):
intensity -= 10
return max(min_eye_intensity, intensity)
def set_eyepower():
global spider_parms
changed = False
# Read menu
speak("25-Press-AC-IncrDecr-Eye")
speak("11-Press-D-Exit")
while True:
letter = get_letter()
if letter == "A":
spider_parms["MAX_INT"] = eye_increase(spider_parms["MAX_INT"])
if spider_parms["MAX_INT"] >= max_eye_intensity:
speak("21-Eye-Max")
else:
speak("23-Eye-Up")
changed = True
continue
if letter == "C":
spider_parms["MAX_INT"] = eye_decrease(spider_parms["MAX_INT"])
if spider_parms["MAX_INT"] <= min_eye_intensity:
speak("22-Eye-Min")
else:
speak("24-Eye-Down")
changed = True
continue
if letter == "D":
return changed
#---------------------------------------------------------------------------------
# get input
def get_letter():
while True:
for key, letter in [(key_A, "A"), (key_B, "B"), (key_C, "C"), (key_D, "D")]:
if GPIO.input(key): # check key
return letter
sleep(0.1) # 100ms
#---------------------------------------------------------------------------------
# Wait for D key
def wait_for_D():
only_D = True
while get_letter() != "D":
only_D = False
return only_D
#---------------------------------------------------------------------------------
def speak(voice_key, volume=voice_volume):
if voice_key not in voices["files"]:
print("speak: voice_key not found:" , voice_key)
return
sound_file = speech_loc + voices["files"][voice_key]["filename"]
mpg123 = "exec sudo -u pi mpg123 -q -f " + str(volume) + " " + sound_file
popen = subprocess.Popen(mpg123, stdout=subprocess.PIPE, shell=True,
preexec_fn=os.setsid)
popen.wait()
#---------------------------------------------------------------------------------
def shutdown_spider():
speak("07-Goodbye")
spider_parms["END_REQUEST"] = True
put_spider_parms()
sleep(3) # sleep to endure spider comes down.
# and rest to false for next startup
spider_parms["END_REQUEST"] = False
put_spider_parms()
command = "/usr/bin/sudo /sbin/shutdown now"
popen = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = popen.communicate()[0]
print(output)
popen.wait()
sleep(100)
#---------------------------------------------------------------------------------
# Arm signal to end program.
def sig_handler(signum, frame):
''' A sigint interrupt will get caught here, and will in effect be the same as
getting a ^C at the keyboard.'''
raise KeyboardInterrupt
#---------------------------------------------------------------
# Initialize the Pi
GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False) # Turn off warning if we didn't a GPIO cleanup
# Arm sigint to cause proceed to graceful end.
signal.signal(signal.SIGINT, sig_handler)
# Init key lines
for key in [key_A, key_B, key_C, key_D]:
GPIO.setup(key, GPIO.IN) # activate input - don't use pull_up_down
sleep(5) # Need this sleep to have the sound card stabilize after a reboot. Not necessary
# after just a simple reboot.
# Get voice file
get_voices_file()
speak("01-Hello-Spider")
# Get spider parms
get_spider_parms()
d_count = 0
try:
while True:
speak("02-Press-D-Attention")
only_D = wait_for_D()
if only_D and d_count >= 2 :
shutdown_spider()
d_count = 0
speak("12-Press-D-Menu")
while True:
key = get_letter()
if key == "A":
changed = set_sound()
break
elif key == "B":
changed = set_volume()
break
elif key == "C":
changed = set_eyepower()
break
elif key == "D":
d_count += 1
if d_count >= 2:
break
changed = False
speak("08-Press-A-Sound")
speak("13-Press-B-Volume")
speak("14-Press-C-Eye")
speak("11-Press-D-Exit")
if changed:
speak("20-Values-Changed")
put_spider_parms()
sleep(0.5) # cosmetic pause before next sound message.
except KeyboardInterrupt:
print()
GPIO.cleanup() # clean up GPIO