From 4c9723561af6e75f1cbdcc3d4ef4acacc1c0500f Mon Sep 17 00:00:00 2001 From: Emanuele Date: Mon, 30 Nov 2020 15:17:19 +0100 Subject: [PATCH] debounce, held --- pyButtons.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pyButtons.py b/pyButtons.py index 03b0b83..2056ed2 100644 --- a/pyButtons.py +++ b/pyButtons.py @@ -18,6 +18,7 @@ MQTT_RECEIVE = "buttons/receive" #variabili globali isRunning = True client = None +debounce = 0.25 def on_message(client, userdata, msg): print(f"{msg.topic} -> {str(msg.payload)}") @@ -54,12 +55,12 @@ def main(): global client global isRunning - btn_su = Button(SU) - btn_giu = Button(GIU) - btn_sx = Button(SX) - btn_dx = Button(DX) - btn_ok = Button(OK) - btn_can = Button(CAN) + btn_su = Button(SU, bounce_time=debounce, hold_repeat=True) + btn_giu = Button(GIU, bounce_time=debounce, hold_repeat=True) + btn_sx = Button(SX, bounce_time=debounce, hold_repeat=True) + btn_dx = Button(DX, bounce_time=debounce, hold_repeat=True) + btn_ok = Button(OK, bounce_time=debounce, hold_repeat=True) + btn_can = Button(CAN, bounce_time=debounce, hold_repeat=True) while isRunning: try: @@ -68,12 +69,21 @@ def main(): client.connect('localhost',1883) client.subscribe(MQTT_RECEIVE) client.on_message = on_message + #evento premuto btn_su.when_pressed = send_SU btn_giu.when_pressed = send_GIU btn_sx.when_pressed = send_SX btn_dx.when_pressed = send_DX btn_ok.when_pressed = send_OK btn_can.when_pressed = send_CAN + #evento tenuto + btn_su.when_held = send_SU + btn_giu.when_held = send_GIU + btn_sx.when_held = send_SX + btn_dx.when_held = send_DX + btn_ok.when_held = send_OK + btn_can.when_held = send_CAN + while isRunning: client.loop(timeout=1) time.sleep(1)