我是Python新手,这是一个非常简单的脚本。它运行良好,但随着时间的推移会变慢。我用的是配有自动化帽的Pi 4。另外,我有一个MIDI到USB接口。其目的是根据接收到的MIDI命令切换HAT上的继电器
当脚本启动时,它可以完美地运行。随着时间的推移,它逐渐变慢
由于我是一个新手,我相信这可以写得更优雅,但我仍然没有看到任何明显会减慢脚本速度的东西
from guizero import App, TextBox, PushButton, Text, Combo
from guizero import App, Drawing
from gpiozero import CPUTemperature
import pygame, pygame.midi
import automationhat
import time
automationhat.light.power.write(0)
automationhat.relay.one.write(0)
automationhat.relay.two.write(0)
automationhat.relay.three.write(0)
pygame.midi.init()
app = App(title="TwiggyBlues", bg = "white", height=230, width=325)
MidiChnl = Combo(app, selected="MIDI 5", options=["MIDI 1", "MIDI 2", "MIDI 3", "MIDI 4", "MIDI 5", "MIDI 6", "MIDI 7", "MIDI 8"],)
MidiChn = (MidiChnl.value)
drawing = Drawing(app,height=150, width=305)
REVERB = drawing.oval(20, 20, 45, 45, color="black")
SLAP = drawing.oval(100, 20, 125, 45, color="black")
DISTORTION = drawing.oval(180, 20, 205, 45, color="black")
COMP = drawing.oval(260, 20, 285, 45, color="black")
drawing.text(5, 75, "REVERB", color="black",size=10)
drawing.text(95, 75, "SLAP", color="black",size=10)
drawing.text(150, 75, "DISTORTION", color="black",size=10)
drawing.text(255, 75, "COMP", color="black",size=10)
reverbstat = automationhat.input.one.read()
slapstat = automationhat.input.two.read()
diststat = automationhat.input.three.read()
compstat = automationhat.analog.one.read()
print("compstat ", compstat)
#######MIDI INTERFACE##############
inp = pygame.midi.Input(3,100)
def reverb_tog():
automationhat.light.power.write(1)
automationhat.relay.one.on()
#drawing = Drawing(app,grid=[40,10])
time.sleep(0.2)
automationhat.relay.one.off()
automationhat.light.power.write(0)
def slap_tog():
automationhat.light.power.write(1)
automationhat.relay.two.on()
#drawing = Drawing(app,grid=[40,10])
time.sleep(0.2)
automationhat.relay.two.off()
automationhat.light.power.write(0)
def dist_tog():
automationhat.light.power.write(1)
automationhat.relay.three.on()
#drawing = Drawing(app,grid=[40,10])
time.sleep(0.2)
automationhat.relay.three.off()
automationhat.light.power.write(0)
def comp_tog():
automationhat.light.power.write(1)
automationhat.output.one.on()
#drawing = Drawing(app,grid=[40,10])
time.sleep(0.2)
automationhat.output.one.off()
automationhat.light.power.write(0)
def midiloop():
reverbstat = automationhat.input.one.read()
slapstat = automationhat.input.two.read()
diststat = automationhat.input.three.read()
compstat = automationhat.analog.one.read()
cpu = CPUTemperature()
print(cpu.temperature)
if MidiChnl.value == "MIDI 1":
chnl = 192
elif MidiChnl.value == "MIDI 2":
chnl = 193
elif MidiChnl.value == "MIDI 3":
chnl = 194
elif MidiChnl.value == "MIDI 4":
chnl = 195
elif MidiChnl.value == "MIDI 5":
chnl = 196
elif MidiChnl.value == "MIDI 6":
chnl = 197
elif MidiChnl.value == "MIDI 7":
chnl = 198
elif MidiChnl.value == "MIDI 8":
chnl = 199
elif MidiChnl.value == "MIDI 9":
chnl = 200
elif MidiChnl.value == "MIDI 10":
chnl = 201
elif MidiChnl.value == "MIDI 11":
chnl = 202
elif MidiChnl.value == "MIDI 12":
chnl = 203
elif MidiChnl.value == "MIDI 13":
chnl = 204
elif MidiChnl.value == "MIDI 14":
chnl = 205
elif MidiChnl.value == "MIDI 15":
chnl = 206
elif MidiChnl.value == "MIDI 16":
chnl = 207
#print("MIDI ", chnl)
if reverbstat>0:
REVERB = drawing.oval(20, 20, 45, 45, color="blue")
elif reverbstat<1:
REVERB = drawing.oval(20, 20, 45, 45, color="black")
if slapstat>0:
SLAP = drawing.oval(100, 20, 125, 45, color="green")
elif slapstat<1:
SLAP = drawing.oval(100, 20, 125, 45, color="black")
if diststat>0:
DISTORTION = drawing.oval(180, 20, 205, 45, color="red")
elif diststat<1:
DISTORTION = drawing.oval(180, 20, 205, 45, color="black")
if compstat>2:
COMP = drawing.oval(260, 20, 285, 45, color="yellow")
elif compstat<2:
COMP = drawing.oval(260, 20, 285, 45, color="black")
read = inp.read(100)
for event in read:
((status,data1,data2,data3),timestamp) = event
MIDI_Cmd = (data1)+1
if status == chnl and MIDI_Cmd == 1:
reverbstat = automationhat.input.one.read()
print("Reverb Status ", reverbstat)
print(MIDI_Cmd,' received')
if reverbstat<1:
reverb_tog()
elif status == chnl and MIDI_Cmd == 10:
reverbstat = automationhat.input.one.read()
print("Reverb Status ", reverbstat)
print(MIDI_Cmd,' received')
if reverbstat>0:
reverb_tog()
if status == chnl and MIDI_Cmd == 2:
slapstat = automationhat.input.two.read()
print("Slap Status ",slapstat)
print(MIDI_Cmd,' received')
if slapstat<1:
slap_tog()
elif status == chnl and MIDI_Cmd == 20:
slapstat = automationhat.input.two.read()
print("Slap Status ",slapstat)
print(MIDI_Cmd,' received')
if slapstat>0:
slap_tog()
if status == chnl and MIDI_Cmd == 3:
diststat = automationhat.input.three.read()
print("Dist Status ",diststat)
print(MIDI_Cmd,' received')
if diststat<1:
dist_tog()
elif status == chnl and MIDI_Cmd == 30:
diststat = automationhat.input.three.read()
print("Dist Status ",diststat)
print(MIDI_Cmd,' received')
if diststat>0:
dist_tog()
if status == chnl and MIDI_Cmd == 4:
compstat = automationhat.analog.one.read()
print("Dist Status ",diststat)
print(MIDI_Cmd,' received')
if compstat<1:
comp_tog()
elif status == chnl and MIDI_Cmd == 40:
compstat = automationhat.analog.one.read()
print("Dist Status ",diststat)
print(MIDI_Cmd,' received')
if compstat>3:
comp_tog()
#######END MIDI INTERFACE##############
Rverb = PushButton(app, text="VERB", align="left", padx=20, command=reverb_tog, width=5)
Slap = PushButton(app, text="SLAP", align="left", padx=20, command=slap_tog, width=5)
Dist = PushButton(app, text="DIST", align="left", padx=20, command=dist_tog, width=5)
Comp = PushButton(app, text="COMP", align="left", padx=20, command=comp_tog, width=5)
app.repeat(100, midiloop)
app.display()
目前没有回答
相关问题 更多 >
编程相关推荐