我写了一个小脚本来制作一个Raspberry pi零的网络收音机。我首先在PI 3上编写了此代码。当我尝试将其编译为零时,出现此错误
Traceback (most recent call last):<br>
File "Radio.py", line 193, in <module> ChangeRadioChannel(0)<br>
File "Radio.py", line 87, in ChangeRadioChannel<br>
MyProcess.stdin.write('q')<br>
NameError: global name 'MyProcess' is not defined<br>
我添加了一些代码
import RPi.GPIO as GPIO
import os
import keyboard
from time import sleep
import shlex, subprocess
from MAX9744 import MAX9744
# Channels
CurrentChannel = 0
Channels = [
'Radio P.R.O.S.', 'http://audiostreamen.nl:8012',
'Star Radio', 'http://internetradio.radiostar.be:8000/;stream.nsv&type=mp3',
]
isPlaying = False
GPIO.setup(Pin_Next, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(Pin_Prev, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def ChangeRadioChannel(choice):
print('Change radio channel')
global CurrentChannel
global Channels
global MyProcess
global Amplifier
CurrentChannel = CurrentChannel + choice*2
if CurrentChannel < 0:
CurrentChannel = len(Channels)-2
if CurrentChannel > len(Channels)-1:
CurrentChannel = 0
MyProcess.stdin.write('q')
sleep(0.5)
Amplifier.set_volume(SPEECH_VOLUME)
os.system('echo "' + Channels[CurrentChannel] + '" | festival --tts')
Amplifier.set_volume(Volume)
MyProcess = subprocess.Popen(['omxplayer', '-o', 'local', Channels[CurrentChannel+1]], stdin=subprocess.PIPE)
file = open("LastChannel.txt","w") #Store latest chosen channel in txt file
file.write(str(CurrentChannel)+"\n")
file.close()
def Next(dummy):
print('Next radio channel')
ChangeRadioChannel(1)
def Previous(dummy):
print('Previous radio channel')
ChangeRadioChannel(-1)
GPIO.add_event_detect(Pin_Next, GPIO.RISING, callback = Next, bouncetime = 200)
GPIO.add_event_detect(Pin_Prev, GPIO.RISING, callback = Previous, bouncetime = 200)
try:
try:
# At boot
# 1) restore volume
try:
file = open("lastvolume.txt", "r")
Volume = int(file.read())
Amplifier.set_volume(Volume)
file.close()
print('Volume is set')
except: pass
# 2) restore channel
file = open("LastChannel.txt","r")
CurrentChannel = int(file.read())
file.close()
Amplifier.set_volume(SPEECH_VOLUME)
os.system('echo "' + Channels[CurrentChannel] + '" | festival --tts')
Amplifier.set_volume(Volume)
MyProcess = subprocess.Popen(['omxplayer', '-o', 'local', Channels[CurrentChannel+1]], stdin=subprocess.PIPE)
except:
CurrentChannel=0
ChangeRadioChannel(0)
while True:
sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
有人有想法吗?我将“ MyProcess”设置为全局,然后将其导入 最好的祝福 尼克