class Tinkerforge::BrickletDualButton

Two tactile buttons with built-in blue LEDs

Constants

CALLBACK_STATE_CHANGED

This callback is called whenever a button is pressed.

Possible states for buttons are:

  • 0 = pressed

  • 1 = released

Possible states for LEDs are:

  • 0 = AutoToggleOn: Auto toggle enabled and LED on.

  • 1 = AutoToggleOff: Auto toggle enabled and LED off.

  • 2 = On: LED on (auto toggle is disabled).

  • 3 = Off: LED off (auto toggle is disabled).

Public Class Methods

new(uid, ipcon) click to toggle source

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.

Calls superclass method
# File lib/tinkerforge/bricklet_dual_button.rb, line 51
def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_SET_LED_STATE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_LED_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_BUTTON_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_SELECTED_LED_STATE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_STATE_CHANGED] = [12, 'C C C C']

  @ipcon.add_device self
end

Public Instance Methods

get_button_state() click to toggle source

Returns the current state for both buttons. Possible states are:

  • 0 = pressed

  • 1 = released

# File lib/tinkerforge/bricklet_dual_button.rb, line 96
def get_button_state
  check_validity

  send_request FUNCTION_GET_BUTTON_STATE, [], '', 10, 'C C'
end
get_identity() click to toggle source

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an :ref:`Isolator Bricklet <isolator_bricklet>` is always at position 'z'.

The device identifier numbers can be found :ref:`here <device_identifier>`. |device_identifier_constant|

# File lib/tinkerforge/bricklet_dual_button.rb, line 121
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_led_state() click to toggle source

Returns the current state of the LEDs, as set by BrickletDualButton#set_led_state.

# File lib/tinkerforge/bricklet_dual_button.rb, line 86
def get_led_state
  check_validity

  send_request FUNCTION_GET_LED_STATE, [], '', 10, 'C C'
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_dual_button.rb, line 126
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
set_led_state(led_l, led_r) click to toggle source

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 BrickletDualButton#get_led_state or you can use BrickletDualButton#set_selected_led_state.

# File lib/tinkerforge/bricklet_dual_button.rb, line 79
def set_led_state(led_l, led_r)
  check_validity

  send_request FUNCTION_SET_LED_STATE, [led_l, led_r], 'C C', 8, ''
end
set_selected_led_state(led, state) click to toggle source

Sets the state of the selected LED (0 or 1).

The other LED remains untouched.

# File lib/tinkerforge/bricklet_dual_button.rb, line 105
def set_selected_led_state(led, state)
  check_validity

  send_request FUNCTION_SET_SELECTED_LED_STATE, [led, state], 'C C', 8, ''
end