Class: Tinkerforge::BrickletDualButton

Inherits:
Device
  • Object
show all
Defined in:
lib/tinkerforge/bricklet_dual_button.rb

Overview

Two tactile buttons with built-in blue LEDs

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

230
DEVICE_DISPLAY_NAME =

:nodoc:

'Dual Button Bricklet'
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).

4
FUNCTION_SET_LED_STATE =

:nodoc:

1
FUNCTION_GET_LED_STATE =

:nodoc:

2
FUNCTION_GET_BUTTON_STATE =

:nodoc:

3
FUNCTION_SET_SELECTED_LED_STATE =

:nodoc:

5
FUNCTION_GET_IDENTITY =

:nodoc:

255
LED_STATE_AUTO_TOGGLE_ON =

:nodoc:

0
LED_STATE_AUTO_TOGGLE_OFF =

:nodoc:

1
LED_STATE_ON =

:nodoc:

2
LED_STATE_OFF =

:nodoc:

3
BUTTON_STATE_PRESSED =

:nodoc:

0
BUTTON_STATE_RELEASED =

:nodoc:

1
LED_LEFT =

:nodoc:

0
LED_RIGHT =

:nodoc:

1

Constants inherited from Device

Device::RESPONSE_EXPECTED_ALWAYS_FALSE, Device::RESPONSE_EXPECTED_ALWAYS_TRUE, Device::RESPONSE_EXPECTED_FALSE, Device::RESPONSE_EXPECTED_INVALID_FUNCTION_ID, Device::RESPONSE_EXPECTED_TRUE

Instance Attribute Summary

Attributes inherited from Device

#callback_formats, #expected_response_function_id, #expected_response_sequence_number, #registered_callbacks, #uid

Instance Method Summary collapse

Methods inherited from Device

#dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletDualButton

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 50

def initialize(uid, ipcon)
  super uid, ipcon

  @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[CALLBACK_STATE_CHANGED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_SET_SELECTED_LED_STATE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_STATE_CHANGED] = 'C C C C'
end

Instance Method Details

#get_button_stateObject

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

  • 0 = pressed

  • 1 = released



92
93
94
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 92

def get_button_state
  send_request(FUNCTION_GET_BUTTON_STATE, [], '', 2, 'C C')
end

#get_identityObject

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’ or ‘d’.

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



111
112
113
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 111

def get_identity
  send_request(FUNCTION_GET_IDENTITY, [], '', 25, 'Z8 Z8 k C3 C3 S')
end

#get_led_stateObject

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



84
85
86
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 84

def get_led_state
  send_request(FUNCTION_GET_LED_STATE, [], '', 2, 'C C')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



116
117
118
119
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 116

def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end

#set_led_state(led_l, led_r) ⇒ Object

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.

The default value is (1, 1).



79
80
81
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 79

def set_led_state(led_l, led_r)
  send_request(FUNCTION_SET_LED_STATE, [led_l, led_r], 'C C', 0, '')
end

#set_selected_led_state(led, state) ⇒ Object

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

The other LED remains untouched.



99
100
101
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 99

def set_selected_led_state(led, state)
  send_request(FUNCTION_SET_SELECTED_LED_STATE, [led, state], 'C C', 0, '')
end