1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4HOST="localhost" 5PORT=4223 6UID="XYZ"# Change XYZ to the UID of your Dual Button Bricklet 7 8fromtinkerforge.ip_connectionimportIPConnection 9fromtinkerforge.bricklet_dual_buttonimportBrickletDualButton1011# Callback function for state changed callback12defcb_state_changed(button_l,button_r,led_l,led_r):13ifbutton_l==BrickletDualButton.BUTTON_STATE_PRESSED:14print("Left Button: Pressed")15elifbutton_l==BrickletDualButton.BUTTON_STATE_RELEASED:16print("Left Button: Released")1718ifbutton_r==BrickletDualButton.BUTTON_STATE_PRESSED:19print("Right Button: Pressed")20elifbutton_r==BrickletDualButton.BUTTON_STATE_RELEASED:21print("Right Button: Released")2223print("")2425if__name__=="__main__":26ipcon=IPConnection()# Create IP connection27db=BrickletDualButton(UID,ipcon)# Create device object2829ipcon.connect(HOST,PORT)# Connect to brickd30# Don't use device before ipcon is connected3132# Register state changed callback to function cb_state_changed33db.register_callback(db.CALLBACK_STATE_CHANGED,cb_state_changed)3435input("Press key to exit\n")# Use raw_input() in Python 236ipcon.disconnect()
Generally, every function of the Python bindings can throw an
tinkerforge.ip_connection.Error exception that has a value and a
description property. value can have different values:
Error.TIMEOUT = -1
Error.NOT_ADDED = -6 (unused since Python bindings version 2.0.0)
led_l – Type: int, Range: See constants, Default: 1
led_r – Type: int, Range: See constants, Default: 1
Returns:
None
Sets the state of the LEDs. Possible states are:
0 = AutoToggleOn: Enables auto toggle with initially enabled LED.
1 = AutoToggleOff: Activates auto toggle with initially disabled LED.
2 = On: Enables LED (auto toggle is disabled).
3 = Off: Disables LED (auto toggle is disabled).
In auto toggle mode the LED is toggled automatically at each press of a button.
If you just want to set one of the LEDs and don't know the current state
of the other LED, you can get the state with get_led_state() or you
can use set_selected_led_state().
The following constants are available for this function:
Callbacks can be registered to receive
time critical or recurring data from the device. The registration is done
with the register_callback() function of
the device object. The first parameter is the callback ID and the second
parameter the callback function:
The available constants with inherent number and type of parameters are
described below.
Note
Using callbacks for recurring events is always preferred
compared to using getters. It will use less USB bandwidth and the latency
will be a lot better, since there is no round trip time.
Virtual functions don't communicate with the device itself, but operate only on
the API bindings device object. They can be called without the corresponding
IP Connection object being connected.
Returns the version of the API definition implemented
by this API bindings. This is neither the release version of this API bindings
nor does it tell you anything about the represented Brick or Bricklet.
Returns the response expected flag for the function specified by the function
ID parameter. It is true if the function is expected to send a response,
false otherwise.
For getter functions this is enabled by default and cannot be disabled,
because those functions will always send a response. For callback configuration
functions it is enabled by default too, but can be disabled by
set_response_expected(). For setter functions it is disabled by default
and can be enabled.
Enabling the response expected flag for a setter function allows to detect
timeouts and other error conditions calls of this setter as well. The
device will then send a response for this purpose. If this flag is disabled for
a setter function then no response is sent and errors are silently ignored,
because they cannot be detected.
The following constants are available for this function:
Changes the response expected flag of the function specified by the
function ID parameter. This flag can only be changed for setter (default value:
false) and callback configuration functions (default value: true). For
getter functions it is always enabled.
Enabling the response expected flag for a setter function allows to detect
timeouts and other error conditions calls of this setter as well. The
device will then send a response for this purpose. If this flag is disabled for
a setter function then no response is sent and errors are silently ignored,
because they cannot be detected.
The following constants are available for this function: