Help python loop

2

I am starting in the programming with Python and I have come across a problem .... I want to make an infinite loop which comes out if I press the "s" option or if the raspberry button is activated. This is what I programmed, but it does not work correctly, any ideas?

import RPi.GPIO as gpio

gpio.setmode(gpio.BCM)

def prueba()
    boton=12

    gpio.setup(boton, gpio.IN, gpio.PUD_UP)

    salir='n'

    while True:
        print ("para salir puse s: ")
        salir=input()

        if salir =='s':
            return 0
            break
        if gpio.input(boton)==gpio.HIGH:
            return 1
            break

while True:

    if prueba() == 1
        print("Ha pulsado s")
    if prueba() == 0
        print("Ha pulsado boton")

Edit: The problem is in the While loop, since I have the feeling that using the input () function, it stays "stuck" and does not continue with the program until a value is entered by keyboard and although the button is pressed he is waiting for the keyboard.

    
asked by Ines V. 05.07.2017 в 19:28
source

2 answers

2

You have two options to achieve the task you need, or you start two different programs, separating both logics, one for the input of the gpio and another for the keyboard input, or if you have to share data or want to have them in the same script then the only way to achieve what you want to do is with Threading because as you have noticed, the code is executed sequentially and the raspberry or listens to the user input or listens to the input of the GPIO.

The solution is to have two trhead (threads) running in the background. One of them will listen to the keyboard input and the other will listen to the input of the gpio.

Following an example of how it could be solved there are several options to create the Threads.

from threading import Thread
import RPI.GPIO as gpio

gpio.setmode(gpio.BCM)



class UserInput:  
    def __init__(self):
        self._running = True

    def terminate(self):  
        self._running = False  

    def run(self):
        while self._running:
            salir=input("Para salir pulse s:")
            if salir == 's':
                print "Saliendo User Input"
                return 0


class GpioInput:  
    def __init__(self):
        self._running = True

    def terminate(self):  
        self._running = False  

    def run(self):
        button = 12
        gpio.setup(button, gpio.IN, gpio.PUD_UP)
        while self._running:
            if gpio.input(button) == gpio.HIGH:
                print "Saliendo GPIO"
                return 1


#Creando la Clase
user_input = UserInput()
#Creando el Thread
user_input_thread = Thread(target=user_input.run) 
#Iniciando el Thread
user_input_thread.start()


gpio_input = GpioInput()

gpio_input_thread = Thread(target=gpio_input.run) 

gpio_input_thread.start()
    
answered by 06.07.2017 в 13:27
0

Technically what you want to do would be like this:

import RPI.GPIO as gpio

gpio.setmode(gpio.BCM)

def prueba():
    button = 12
    gpio.setup(button, gpio.IN, gpio.PUD_UP)

    while True:
        salir=input("Para salir pulse s:")
        if salir == 's':
            return 0
        if gpio.input(button) == gpio.HIGH:
            return 1

exits_type = prueba()
if exits_type == 1:
    print("a pulsado el boton")
else:
    print("a pulsado la teclas s")

You do not necessarily need the two while cycles to achieve it unless you want to never leave the program. try it if it does not work with pleasure I can help you.

note: breaks are not necessary. the return causes it to exit the function so the lines after a return are never executed.

A useful post that you could use: Physical computing with Raspberry Pi is in English but could be useful.

    
answered by 06.07.2017 в 01:23