Python脚本只运行120次。有任何想法吗

时间:2013-09-22 21:01:33

标签: python pygame raspberry-pi

我在Raspberry Pi上运行python脚本,我使用4个gpio引脚,3个作为输出,1个作为输入。当输入设置为低时,它会激活一个程序,输出全部在不同时间打开和关闭,并使用pygame.mixer播放声音效果。我开始有一些问题,输入不再激活程序。当我进一步研究它时,我发现它只能工作120次,然后输入不再激活程序,我不会得到任何错误信息。我绝不是python中编码的专家所以我的代码可能看起来很乱。这是代码。

    import RPi.GPIO as GPIO
    import sys
    import time
    import pygame
    import os
    from array import *

    print "loading program"
    GPIO.setmode(GPIO.BCM)
    GpioPinsOut=[4,17,27]
    for a in range (0, len(GpioPinsOut)):
        GPIO.setup(GpioPinsOut[a], GPIO.OUT)
    GpioPinsIn=[7,]
    for y in range (0, len(GpioPinsIn)):
        GPIO.setup(GpioPinsIn[y], GPIO.IN)
    start=0
    prevTriggered=0
    triggered=0
    elapsed=0
    vars=[0,]
    inVars=[0]
    prevInVars=[5,5]
    sceneNames=["Night"]
    onoffVar=  [1, 0]
    onoffVar2= ["On", "Off"]
    outputNamesFinal=['output1','output2','output3']
    outputNames= [[['output1','output2','output3'],['output1','output2','output3','reset']]]
    outputs=     [[[4        ,17       ,27       ],[4        ,17       ,27       ,'reset']]]
    times=       [[[0.0      ,0.0      ,0.0      ],[11.7     ,18.0     ,18.0     ,18.0   ]]]
    printGpioVar=[[[0        ,0        ,0        ],[0        ,0        ,0        ,0      ]]]
    outputsAlwaysOn=[[]]
    for va in range (0, len(GpioPinsOut)):
        GPIO.output(GpioPinsOut[va], False)
    for za in range (0, len(outputsAlwaysOn[0])):
        GPIO.output(outputsAlwaysOn[0][za], True)               
    effects=[[],[]]
    effectslocations=[['/root/audio/soundeffect.wav']]
    pygame.mixer.init(44100, -16, 2, 2048)
    for ba in range (0, len(effectslocations)):
        for bb in range (0, len(effectslocations[ba])):
            effectsVar = pygame.mixer.Sound(effectslocations[ba][bb])
            effects[ba].append(effectsVar)
    effectsNumbers=[[0  ]]
    effectsTimes=  [[0.0]]
    sfxVAR = 0
    numberOfTimes=0
    print "program ready"
    try:
        while True:
            for z in range (0, len(GpioPinsIn)):
                if GPIO.input(GpioPinsIn[z]) == False:
                    inVars[z]=0
                else:
                    inVars[z]=1
            if inVars[0] == 0:
                triggered = 1

            if triggered == 1:
                if prevTriggered != triggered:
                    print "Trigger Activated"
                    if start == 0:
                        print "Start Time Activated"
                        numberOfTimes+=1
                        print numberOfTimes
                        start = time.clock()
                elapsed = time.clock() - start
                for g in range(0, len(effectsTimes[0])):
                    if elapsed >= effectsTimes[0][g]:
                        if sfxVAR == 0:
                            effects[0][effectsNumbers[0][g]].play()
                            sfxVAR = 1

                for c in range (0, len(times[0])):
                    for d in range (0, len(times[0][c])):
                        if elapsed >= times[0][c][d]:
                            if outputs[0][c][d] == "reset":
                                print "done with %s procedures"%sceneNames[0]
                                for e in range (0, len(printGpioVar[0])):
                                    for f in range (0, len(printGpioVar[0][e])):
                                        printGpioVar[0][e][f]=0 
                                start=0
                                prevTriggered=0
                                sfxVAR=0
                                triggered=0

                            else:
                                if printGpioVar[0][c][d]==0:
                                    GPIO.output(outputs[0][c][d], onoffVar[c])
                                    print "Turned %s %s"%(onoffVar2[c], outputNames[0][c][d])
                                    printGpioVar[0][c][d]=1

                prevTriggered = triggered
            for g in range (0, len(inVars)):
                prevInVars[g]=inVars[g]
    except KeyboardInterrupt:
        GPIO.cleanup()
        sys.exit()

1 个答案:

答案 0 :(得分:0)

您必须处理应用程序循环中的事件。分别见pygame.event.get() pygame.event.pump()

<块引用>

对于游戏的每一帧,您都需要对事件队列进行某种调用。这可确保您的程序可以在内部与操作系统的其余部分进行交互。

#create some data
      a10 = ts(rnorm(200,10,10), start=1992, frequency = 12)
 

    #intitalize values       
        k=60
    n <- length(a10)
    mae1 <- mae2  <- matrix(NA,n-k,12)
    st <- tsp(a10)[1]+(k-2)/12
   
#calculate training errors 
    for(i in 1:(n-k))
    {
    xshort <- window(a10, end=st + i/12)
    xnext <- window(a10, start=st + (i+1)/12, end=st + (i+12)/12)
    fit1 <- arima(xshort, order=c(1,0,1))
    pred1 <- predict(fit1, n.ahead=12,se.fit = FALSE)
    fit2 <- HoltWinters(xshort)
    pred2 <- predict(fit2, n.ahead=12,se.fit=FALSE)
    mae1[i,1:length(xnext)] <- abs(pred1[1:length(xnext)]-xnext)
    mae2[i,1:length(xnext)] <- abs(pred2[1:length(xnext)]-xnext)
}

plot(1:12, colMeans(mae1,na.rm=TRUE), type="l", col=2, xlab="horizon", ylab="MAE",
     ylim=c(0,25))
lines(1:12, colMeans(mae2,na.rm=TRUE), type="l",col=3)

legend("topleft",legend=c("ARIMA","HOLTWINTERS"),col=2:4,lty=1)