Pergunta

I made some hardware for my Raspberry Pi's GPIO that I'd like to test. I found some neat python code that makes 8 buttons for the 8 outputs, and lets you toggle their state. I have almost no knowledge of python, but I'd like to be able to toggle the 8 outputs via keyboard (ex. numbers 1-8). I don't know how to ask for a keyboard input without pausing the flow of the program then continuing after response.

How can I make the numbers 1-8 "interrupt" my program and jump to 1 of the 8 corresponding functions?

My code:

from Tkinter import *
import RPi.GPIO as GPIO
import time

GPIO.setmode( GPIO.BCM )
GPIO.setup( 4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)

class App:
    io4=0
    io17=0
    io18=0
    io21=0
    io22=0
    io23=0
    io24=0
    io25=0

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.p1 = Button(frame, text="GPIO 25",fg="green", command=self.gpio25)
        self.p1.pack(side=LEFT)
        self.p1.grid(row=0,column=0)

        self.p2 = Button(frame, text="GPIO 24",fg="red", command=self.gpio24)
        self.p2.pack(side=LEFT)
        self.p2.grid(row=0,column=1)

        self.p3 = Button(frame, text="GPIO 23",fg="red", command=self.gpio23)
        self.p3.pack(side=LEFT)
        self.p3.grid(row=0,column=2)

        self.p4 = Button(frame, text="GPIO 22",fg="red", command=self.gpio22)
        self.p4.pack(side=LEFT)
        self.p4.grid(row=0,column=3)

        self.p5 = Button(frame, text="GPIO 21",fg="red", command=self.gpio21)
        self.p5.pack(side=LEFT)
        self.p5.grid(row=0,column=4)

        self.p6 = Button(frame, text="GPIO 18",fg="red", command=self.gpio18)
        self.p6.pack(side=LEFT)
        self.p6.grid(row=0,column=5)

        self.p7 = Button(frame, text="GPIO 17",fg="red", command=self.gpio17)
        self.p7.pack(side=LEFT)
        self.p7.grid(row=0,column=6)


        self.p8 = Button(frame, text="GPIO 4", fg="red",command=self.gpio4)
        self.p8.pack(side=LEFT)
        self.p8.grid(row=0,column=7)

    def gpio4(self):
        if self.io4==0:
          GPIO.output(4, GPIO.HIGH)
          self.io4=1
        else:
          GPIO.output(4, GPIO.LOW)
          self.io4=0
        return

    def gpio17(self):
        if self.io17==0:
          GPIO.output(17, GPIO.HIGH)
          self.io17=1
        else:
          GPIO.output(17, GPIO.LOW)
          self.io17=0
        return

    def gpio18(self):
        if self.io18==0:
          GPIO.output(18, GPIO.HIGH)
          self.io18=1
        else:
          GPIO.output(18, GPIO.LOW)
          self.io18=0
        return

    def gpio21(self):
        if self.io21==0:
          GPIO.output(21, GPIO.HIGH)
          self.io21=1
        else:
          GPIO.output(21, GPIO.LOW)
          self.io21=0
        return

    def gpio22(self):
        if self.io22==0:
          GPIO.output(22, GPIO.HIGH)
          self.io22=1
        else:
          GPIO.output(22, GPIO.LOW)
          self.io22=0
        return

    def gpio23(self):
        if self.io23==0:
          GPIO.output(23, GPIO.HIGH)
          self.io23=1
        else:
          GPIO.output(23, GPIO.LOW)
          self.io23=0
        return

    def gpio24(self):
        if self.io24==0:
          GPIO.output(24, GPIO.HIGH)
          self.io24=1
        else:
          GPIO.output(24, GPIO.LOW)
          self.io24=0
        return

    def gpio25(self):
        if self.io25==0:
          GPIO.output(25, GPIO.HIGH)
          self.io25=1
        else:
          GPIO.output(25, GPIO.LOW)
          self.io25=0
        return

    def reserved(self):
        return

root = Tk()
app = App(root)
root.mainloop()
Foi útil?

Solução

Add master.bind(...) commands to the __init__ method:

def __init__(self, master):

    frame = Frame(master)
    frame.pack()
    master.bind('1', self.gpio25)
    master.bind('2', self.gpio24)
    master.bind('3', self.gpio23)
    ...

master.bind('1', self.gpio25) binds the keypress 1 event to the method call self.gpio25(event). You will need 7 more master.bind calls -- one for each key.

Next, modify the definition of the callback functions. You will need to add a second argument, event, to each of them. Change, for example,

def gpio25(self):

to

def gpio25(self, event = None):

Explanation:

When you use the bind method, you are binding an Event to a callback function (e.g. self.gpio25). Pressing a key is a KeyPress Event. Information about the event is sent to the callback in an Event object. Thus the callback function must take one argument. See Tkinter events and bindings for more information.

Buttons work differently. Their callback functions are called with zero arguments.

Since we are using the same callback function, self.gpio25, as both a Keypress event callback and as a Button callback, it must be a function that can take zero or one argument.

In Python, the way to do that is to define gpio25 with a call signature like:

def gpio25(self, event = None):

self is an instance of the class App. self.gpio25 is a bound method with self bound as its first argument. So calling self.gpio25() will call the gpio25 function with self as the first argument (and the local variable event will be assigned the value None). Calling self.gpio25(foo) will call gpio25 with self as the first argument, and foo as the second argument (and the local variable event will be assigned the value foo).

In your case, the value assigned to event does not really matter, since event is not used in the body of gpio25. We just needed to set up self.gpio25 to be ready to accept zero or one argument.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top