Answer the question
In order to leave comments, you need to log in
Are there normal global objects in python?
Prehistory: there is a pi on which the python script is running and waiting for data via serial when it receives a command to do something, for example, 2 necessary functions - turn on and off a certain track in omxplayer. Everything would be fine, but you need to make a smooth transition between tracks, and this is already missing in the player's functionality. I found a crutch solution in the form of a wrapper for this player, it creates an object of this type player=OMXPlayer(filename) which gives the player.play command, etc.
The script itself is built as a bunch of elif (command)
And the problem is that if you start the player in one of these if, then it is not visible from the other, the global parameter does not change anything, it says that there is no such object
the whole script revolves around while(true) of one def. I suspect that the problem is related to the fact that global objects are updated only after leaving the def, but not a fact. What can be done in this situation?
ps I myself spent only a couple of hours in python, so I don’t know a lot of
code in it
#!/usr/bin/python
import serial, time, os, sys
from omxplayer import OMXPlayer
ser = serial.Serial ()
try:
ser = serial.Serial(
port='/dev/ttyACM0',
baudrate = 115200,
timeout=1
)
print 'ttyACM0'
except:
try:
ser = serial.Serial(
port='/dev/ttyACM1',
baudrate = 115200,
timeout=1
)
print 'ttyACM1'
except:
try:
ser = serial.Serial(
baudrate = 115200,
port='/dev/ttyACM2',
timeout=1
)
print 'ttyACM2'
except:
try:
ser = serial.Serial(
baudrate = 115200,
port='/dev/ttyUSB0',
timeout=1
)
print 'ttyUSB0'
except:
pass
#if you only want to send data to arduino (i.e. a signal to move a servo)
def send( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
break
except:
pass
time.sleep(0.1)
#if you would like to tell the arduino that you would like to receive data from the arduino
def send_and_receive( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
state = ser.readline()
print state
return state
except:
pass
time.sleep(0.1)
def readlineCR(port):
rv = ""
while True:
ch = port.read()
if ch=='\r' or ch=='':
return rv
else:
rv += ch
#if you would like to tell the arduino that you would like to receive data from the arduino
def receive( ):
while True:
try:
print "try to read..."
time.sleep(0.01)
state = readlineCR(ser)
if len(state)>1:
print "State: " + state
if "playsound" in state:
os.system("clear > tty1")
plstr="cat t|omxplayer -o both --vol \"-200\" "
name=state.split(" ",1)[1]
name= "/home/pi/"+name
name=name.replace("\n", "")
name=name.replace("\r", "")
ender=" >> /dev/null &"
command= plstr
command=command+ name
command=command+ ender
print command
os.system(command)
elif "pl1play" in state:
name=state.split(" ",1)[1]
name=name.replace("\n", "")
name=name.replace("\r", "")
name= "/home/pi/"+name
global player1
player1= OMXPlayer(name)
player1.play()
print command
elif "pl2play" in state:
name=state.split(" ",1)[1]
name=name.replace("\n", "")
name=name.replace("\r", "")
name= "/home/pi/"+name
global player2
player2 = OMXPlayer(name)
player2.play()
print command
elif "pl3play" in state:
name=state.split(" ",1)[1]
name=name.replace("\n", "")
name=name.replace("\r", "")
name= "/home/pi/"+name
global player3
player3 = OMXPlayer(name)
player3.play()
print command
elif "stoppl1" in state:
vol =0
while(vol<6000):
vol=vol+150
player1.set_volume(0,-vol)
time.sleep(0.1)
player1.pause()
player1.quit()
print command
elif "stoppl2" in state:
vol =0
while(vol<6000):
vol=vol+150
player2.set_volume(0,-vol)
time.sleep(0.1)
player2.pause()
player2.quit()
print command
elif "stoppl3" in state:
vol =0
while(vol<6000):
vol=vol+150
player3.set_volume(0,-vol)
time.sleep(0.1)
player3.pause()
player3.quit()
print command
elif "stopsound" in state:
os.system("kill -9 `ps au | grep omxplayer | awk '{print $2}'`")
print command
return state
else:
print "failed no data..."
return "nodata"
except Exception, e:
time.sleep(0.1)
print ""
print "Unexpected error:", str(e)
#f = open('dataFile.txt','a')
ser.flushInput()
while 1 :
receive()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question