Class: Tinkerforge::BrickletDualButton
- 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::DEVICE_IDENTIFIER_CHECK_MATCH, Device::DEVICE_IDENTIFIER_CHECK_MISMATCH, Device::DEVICE_IDENTIFIER_CHECK_PENDING, 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, #high_level_callbacks, #registered_callbacks, #replaced, #uid
Instance Method Summary collapse
-
#get_button_state ⇒ Object
Returns the current state for both buttons.
-
#get_identity ⇒ Object
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.
-
#get_led_state ⇒ Object
Returns the current state of the LEDs, as set by BrickletDualButton#set_led_state.
-
#initialize(uid, ipcon) ⇒ BrickletDualButton
constructor
Creates an object with the unique device ID
uid
and adds it to the IP Connectionipcon
. -
#register_callback(id, &block) ⇒ Object
Registers a callback with ID
id
to the blockblock
. -
#set_led_state(led_l, led_r) ⇒ Object
Sets the state of the LEDs.
-
#set_selected_led_state(led, state) ⇒ Object
Sets the state of the selected LED (0 or 1).
Methods inherited from Device
#check_validity, #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
.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 52 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 |
Instance Method Details
#get_button_state ⇒ Object
Returns the current state for both buttons. Possible states are:
-
0 = pressed
-
1 = released
97 98 99 100 101 |
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 97 def check_validity send_request FUNCTION_GET_BUTTON_STATE, [], '', 10, 'C C' end |
#get_identity ⇒ Object
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|
122 123 124 |
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 122 def get_identity send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S' end |
#get_led_state ⇒ Object
Returns the current state of the LEDs, as set by BrickletDualButton#set_led_state.
87 88 89 90 91 |
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 87 def get_led_state check_validity send_request FUNCTION_GET_LED_STATE, [], '', 10, 'C C' end |
#register_callback(id, &block) ⇒ Object
Registers a callback with ID id
to the block block
.
127 128 129 130 |
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 127 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.
80 81 82 83 84 |
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 80 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) ⇒ Object
Sets the state of the selected LED (0 or 1).
The other LED remains untouched.
106 107 108 109 110 |
# File 'lib/tinkerforge/bricklet_dual_button.rb', line 106 def set_selected_led_state(led, state) check_validity send_request FUNCTION_SET_SELECTED_LED_STATE, [led, state], 'C C', 8, '' end |