Jump to content

Python: LCD20x4 Display - Button Pressed und Callback


Recommended Posts

Hallo,

ich bin mit meinem marginalen Python-Kenntnissen ans vorläufige Ende gekommen und hoffe auf Tipps der Python-Kenner hier im Forum.

Gegeben und funktionierend, basierend auf den Beispielen im Tinkerforge Website, ist folgender Python-Code:

HOST = "localhost"
PORT = 4223
UID = "XXX" # Change XYZ to the UID of your LCD 20x4 Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_lcd_20x4 import BrickletLCD20x4
import os

# Callback function for button pressed callback
def cb_button_pressed(button):
    print("Button Pressed: " + str(button))
    print("backlight=",lcd.is_backlight_on())
    if button == 0:
      if lcd.is_backlight_on() is True:
        lcd.backlight_off()
      else:
        lcd.backlight_on()
    if button == 3:
      os.system("sudo systemctl reboot")   


# Callback function for button released callback
def cb_button_released(button):
    print("Button Released: " + str(button))

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    lcd = BrickletLCD20x4(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Register button pressed callback to function cb_button_pressed
    lcd.register_callback(lcd.CALLBACK_BUTTON_PRESSED, cb_button_pressed)

    # Register button released callback to function cb_button_released
    #lcd.register_callback(lcd.CALLBACK_BUTTON_RELEASED, cb_button_released)

    input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()

Wo ich nun nicht mehr weiter weiss ist, wie kann ich für Button 3 eine funktionale Abfrage einbauen in der Art "Bitte Button 3 nochmals drücken für System Reboot!" Button 3 soll dabei innerhalb von 5 Sekunden nochmals gedrückt werden, ansonsten würde alles wieder in den Ausganszustand zurückfallen.

Vielen Dank für hilfreiche Tipps im Voraus,

topi

 

Link to comment
Share on other sites

Moin topi.

 

Anbei mal ein einfaches Beispiel wie du es machen könntest:

 

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_lcd_20x4 import BrickletLCD20x4
import os, time

def callback_wrapper():
    timestamp = -1
    def cb_button_pressed(button):
        print("Button {0} pressed...".format(button))

        if button == 0:
            if lcd.is_backlight_on():
                lcd.backlight_off()
            else:
                lcd.backlight_on()
        elif button == 3:
            nonlocal timestamp
            if time.time() - timestamp > 5:
                print('Press button 3 within the next 5 seconds again...')
                timestamp = time.time()
            else:
                print('Shutdown now...')
                timestamp = -1      # This line is only necessary for testing purpose without real shutdown!
    return cb_button_pressed


if __name__ == "__main__":

    UID = "XYZ" # Change XYZ to the UID of your LCD 20x4 Bricklet
    HOST = "127.0.0.1"
    PORT = 4223

    ipcon = IPConnection()
    lcd = BrickletLCD20x4(UID, ipcon)
    ipcon.connect(HOST, PORT)

    button_pressed_callback = callback_wrapper()
    lcd.register_callback(lcd.CALLBACK_BUTTON_PRESSED, button_pressed_callback)

    input("Press key to exit\n")
    ipcon.disconnect()

 

Hoffe es entspricht dem was du dir vorgestellt hast. Bei Fragen einfach fragen.

 

Viele Grüße

__LC__

 

Nachtrag 10.09.2017: Zur besseren Übersichtlichkeit habe ich den Code-Schnipsel nochmal leicht abgeändert.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...