2017-06-25 6 views
0

最新のraspian jessieがインストールされたrasperry piを使用しています。ボタンがrasperry piのpythonでkeydownトリガーのように押されたときのトリガー機能3

私はkeydownイベントに反応したいので、関数を呼び出して一度だけ実行し、パラメータをインクリメントする必要があります。

私が現在持っていることはどのように私は私の関数は一度だけ呼び出され、私の現在の変数が正しくインクリメント機能が一度トリガーされたときに0にリセットされていないことを確認してください

# Import Libraries 
from gpiozero import LED,Button 
from signal import pause 
from time import sleep 

# LED Declaration 
LED1 = LED(21) 
LED2 = LED(16) 
LED3 = LED(20) 

# Button declaration 
button = Button(19) 

LED1.off() 
LED2.off() 
LED3.off() 

current = 0 

# Function for setting Outputs 
def output(current): 
    print(current) 
    if current == 0: 
     LED1.off() 
     LED2.off() 
     LED3.off() 
    if current == 1: 
     LED1.on() 
     LED2.off() 
     LED3.off() 
    if current == 2: 
     LED1.off() 
     LED2.on() 
     LED3.off() 
    if current == 3: 
     LED1.on() 
     LED2.on() 
     LED3.off() 
    if current == 4: 
     LED1.off() 
     LED2.off() 
     LED3.on() 
    if current == 5: 
     LED1.on() 
     LED2.off() 
     LED3.on() 
    if current == 6: 
     LED1.off() 
     LED2.on() 
     LED3.on() 
    if current == 7: 
     LED1.on() 
     LED2.on() 
     LED3.on() 

def increment(current): 
    print(current) 
    if current < 7: 
     current += 1 
     output(current) 
     return 
    if current == 7: 
     current = 0 
     output(current) 
     return 


# Check for pressed button 
while True: 
    if button.when_pressed True: 
     increment(current) 

を保つことができていますか?

答えて

1

など、グローバル変数を使用して避けることができます、あなたはインクリメンタの状態を追跡するためにcurrentを使用するようにしてください。しかし、increment内のcurrentは、グローバル(モジュール)名前空間のcurrentと同じではありません。これを回避する最も簡単な方法は、インクリメント機能にglobalようcurrentを宣言することですが、私はそれが私がbitwise&を使用して複数のLEDのためにも、より柔軟なコードを作っ

class MyLeds() 

    def __init__(self, *leds, current=0): 
     self._leds = leds 
     self._current = current 

    def increment(): 
     self._current = (self._current + 1) % (2 ** len(self._leds) - 1) 
     self._output() 

    def _output(): 
     for i, led in enumerate(leds): 
      on = self._current & 2**i 
      led.on() if on else led.off() 
      # print('led %i is %s' % (i+1, on)) 

この状態を維持するためにクラスを作成するのが最善だと思います。

スクリプトの残りの部分は、その後、次のようになります。

if __name__ == '__main__': 
    from gpiozero 
    from signal import pause 
    from time import sleep 

    # LED Declaration 
    led_ports = (21, 16, 20) 
    leds = tuple(gpiozero.LED(led) for led in led_ports) 

    for led in leds: 
     led.off() 
    my_leds = MyLeds(leds) 
    # Button declaration 
    button = gpiozero.Button(19) 

    while True: 
     if button.when_pressed: 
      my_leds.increment() 
+0

私はこれをテストすることができませんでしたので、私はGPIOボードとのRPIを持っていません。私は 'increment'で+1を忘れてしまったように見えますが、' while True'ループで条件を修正しました –

1

whileループはGUIが機能しないようにします。そのため、ボタンのコマンドオプションを使用して関数を呼び出してみてください。また、クラスを使用してGUIを作成することをお勧めします。これは、この時点で

from tkinter import * 

class Counter: 

    def __init__(self, master): 
     self.current = 0 
     # Button declaration 
     button = Button(master, text='+', command=self.increment) 
     button.grid() 

    def increment(self): 
     self.current += 1 
     print(self.current) 

root = Tk() 
app = Counter(root) 
root.mainloop() 
関連する問題