我有一個紅外傳感器的樹莓派。在我的Python腳本中,有一個線程類監聽一個imap服務器(捕獲像START或STOP之類的指令)。我的想法是現在通過電子郵件發送命令,一旦腳本收到命令,應該禁用某些功能,直到收到新命令。但主要問題是現在我不知道如何實現它。 謝謝你提供有用和有用的建議。Python:如何啓用和禁用函數?
def take_picture():
...
def take_video():
...
class EmailThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
....
if get_mail == 1:
if var_subject == 'STOP':
#TODO: stop take_picture(), take_video()
pass
elif var_subject == 'START':
#TODO: start take_picture(), take_video()
pass
else:
print u'Wrong command'
time.sleep(600) #google doesn't like many connections within 10min
def main():
# Start eMail Thread
email = EmailThread()
email.start()
try:
print "Waiting for PIR to settle ..."
# Loop until PIR output is 0
while GPIO.input(GPIO_PIR) == 1:
Current_State = 0
print "Ready"
# Loop until threadingusers quits with CTRL-C
while True :
# Read PIR state
Current_State = GPIO.input(GPIO_PIR)
if Current_State == 1 and Previous_State == 0:
counter = counter + 1
# PIR is triggered
start_time = time.time()
log_to_file('Motion Nr. %s detected!' % counter)
take_picture()
# Record previous state
Previous_State = 1
elif Current_State == 0 and Previous_State == 1:
# PIR has returned to ready state
stop_time=time.time()
print " Ready ",
elapsed_time=int(stop_time-start_time)
print " (Elapsed time : " + str(elapsed_time) + " secs)"
Previous_State = 0
except KeyboardInterrupt:
print "Quit "
# Reset GPIO settings
GPIO.cleanup()
if __name__ == '__main__':
main()
我使用'python3'來獲取,除非你有特殊的理由使用'python2'。即使你決定使用'python2',我建議使用'print'作爲一個函數。 –
我使用python2.7,因爲一些模塊只能在python2下運行,比如picamera – Seppo
你是否在尋找一些奇特的建築物?我只需在電子郵件線程中設置一個布爾標誌,並在主循環中放入一個'if email.enable_picture:'。 – dornhege