简体   繁体   中英

End While-Loop from another IF-Statement

i am building a Phone with my Raspberry Pi and Twinkle. In my example i am starting Twinkle with a subprocess and checking the output for my code.

Now i need to start a while-loop when someone is calling to check the "end-call button". This works well but i need to end the loop if the far end cancelled the call. How can i break the while loop from another if condition?

Here is my code for example:

#IMPORTS
import sys
import time
import RPi.GPIO as GPIO
from multiprocessing.pool import ThreadPool
import threading

#START TWINKLE
from subprocess import Popen, PIPE

proc = Popen(["twinkle", "-c"], stdin=PIPE, stdout=PIPE, bufsize=1)

for line in iter(proc.stdout.readline, b''):
        print line

        #BUTTON WATCHER
        def button_watch():
                input_state = GPIO.input(36)
                while (input_state == False):
                        print "loop"
                        #END LOOP ON BUTTON PRESS
                        if (input_state == False):
                                print('Button Pressed')
                                input_state = GPIO.input(36)
                                GPIO.setup(32, GPIO.OUT)
                                GPIO.output(32, GPIO.LOW)
                                proc.stdin.write("answer\n")
                                time.sleep(0.2)
                                break

        #INCOMING CALL
        def main():
                if (line.find("incoming call") > 0):
                        print "call is coming"
                        GPIO.setmode(GPIO.BOARD)
                        GPIO.setup(32, GPIO.OUT)
                        GPIO.output(32, GPIO.HIGH)
                        GPIO.setmode(GPIO.BOARD)
                        GPIO.setup(36, GPIO.IN, pull_up_down=GPIO.PUD_UP)
                        #START BUTTON WATCHER ON INCOMING CALL
                        t1 = threading.Thread(target=button_watch)
                        t1.start()


        #CALL CANCELLED
        if (line.find("far end cancelled call") > 0):
                print "call was cancelled"
                GPIO.setmode(GPIO.BOARD)
                GPIO.setup(32, GPIO.OUT)
                GPIO.output(32, GPIO.LOW)
                ##############################################
                # NEED TO END WHILE LOOP FROM "button_watch" #
                ##############################################


        tmain = threading.Thread(target=main)
        tmain.start()


proc.communicate()

I added a comment seeking clarification because I am not sure I understand the intent completely. Now, eventually you will find better and more robust ways to do this but the simplest solution would be to check for a compound condition (assuming you still need input_condition == False ).

So you could define a global flag called remote_disconnected , initialize it in a way that your while loop runs in the beginning, and compound it like: while (input_condition == False and remote_disconnected == False):

使 button_watch 依赖于全局标志,因此您可以将标志变为 False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM