1packagemain 2 3import( 4"fmt" 5"github.com/Tinkerforge/go-api-bindings/dual_button_bricklet" 6"github.com/Tinkerforge/go-api-bindings/ipconnection" 7) 8 9constADDRstring="localhost:4223"10constUIDstring="XYZ"// Change XYZ to the UID of your Dual Button Bricklet.1112funcmain(){13ipcon:=ipconnection.New()14deferipcon.Close()15db,_:=dual_button_bricklet.New(UID,&ipcon)// Create device object.1617ipcon.Connect(ADDR)// Connect to brickd.18deferipcon.Disconnect()19// Don't use device before ipcon is connected.2021db.RegisterStateChangedCallback(func(buttonLdual_button_bricklet.ButtonState,buttonRdual_button_bricklet.ButtonState,ledLdual_button_bricklet.LEDState,ledRdual_button_bricklet.LEDState){2223ifbuttonL==dual_button_bricklet.ButtonStatePressed{24fmt.Println("Left Button: Pressed")25}elseifbuttonL==dual_button_bricklet.ButtonStateReleased{26fmt.Println("Left Button: Released")27}2829ifbuttonR==dual_button_bricklet.ButtonStatePressed{30fmt.Println("Right Button: Pressed")31}elseifbuttonR==dual_button_bricklet.ButtonStateReleased{32fmt.Println("Right Button: Released")33}3435fmt.Println()36})3738fmt.Print("Press enter to exit.")39fmt.Scanln()40}
The Dual Button Bricklet API is defined in the package github.com/Tinkerforge/go-api-bindings/dual_button_bricklet
Nearly every function of the Go bindings can return an
ipconnection.DeviceError, implementing the error interface. The error can have one of the following values:
ipconnection.DeviceErrorSuccess = 0
ipconnection.DeviceErrorInvalidParameter = 1
ipconnection.DeviceErrorFunctionNotSupported = 2
ipconnection.DeviceErrorUnknownError = 3
which correspond to the values returned from Bricks and Bricklets.
ledL – Type: uint8, Range: See constants, Default: 1
ledR – Type: uint8, Range: See constants, Default: 1
Returns:
err – Type: error
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 GetLEDState() or you
can use SetSelectedLEDState().
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 corresponding Register*Callback function, which returns a unique callback ID.
This ID can be used to deregister the callback later with the corresponding Deregister*Callback
function.
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
SetResponseExpected(). 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: