mercredi 20 mars 2019

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()

Aucun commentaire:

Enregistrer un commentaire