2015-04-04 65 views
1

我对Pi上的GPIO编程不太熟悉,但是在查看GPIO lib和picamera上的一些教程后我写了这个。我有一个连接到针脚4和地面的按钮,当按下时应该为相机加上星号,拍摄照片,然后关闭相机。我下面的代码拍摄一张照片,但不断地保持调用关闭功能。我不太明白为什么。Raspberry Pi相机GPIO ...关闭语句导致错误

import picamera 
import RPi.GPIO as GPIO 
import datetime 
import time 

GPIO.setmode(GPIO.BCM) 
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) 

class OpenCamera: 
    def __init__(self): 
     self.camera = picamera.PiCamera() 

    def setres(self): 
     self.camera.resolution = (640, 480) 
     self.camera.brightness = 50 
     self.camera.sharpness = 10 

    def takepic(self): 
     currenttime = time.localtime() 
     day = time.strftime('%m-%d-%Y', currenttime) 
     exacttime = time.strftime('%H:%M:%S', currenttime) 
     self.camera.capture(day + exacttime + '.jpg') 

    def close(self): 
     self.close() 


while True: 
    inputstate = GPIO.input(4) 
    if inputstate == False:   
     startcam = OpenCamera()   
     startcam.setres()  
     time.sleep(4)  
     print('5 4 3 2...cheese!')  
     startcam.takepic()  
     startcam.close() 

我从这里的一些代码:http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/

如果我删除的close()然后我用尽资源......我试图做一个“事件检测”,但我仍然得到同样的问题以上。

回答

2

这些行调用close()函数本身,因此会导致无限调用。

def close(self): 
    self.close() 

您可能想改为拨打self.camera.close()

+0

我知道这一点,但起初并不完全明白!谢谢 – ohbrobig 2015-04-06 16:38:40