class Tinkerforge::BrickletJoystick

2-axis joystick with push-button

Constants

CALLBACK_ANALOG_VALUE

This callback is triggered periodically with the period that is set by BrickletJoystick#set_analog_value_callback_period. The parameters are the analog values of the joystick.

The CALLBACK_ANALOG_VALUE callback is only triggered if the values have changed since the last triggering.

CALLBACK_ANALOG_VALUE_REACHED

This callback is triggered when the threshold as set by BrickletJoystick#set_analog_value_callback_threshold is reached. The parameters are the analog values of the joystick.

If the threshold keeps being reached, the callback is triggered periodically with the period as set by BrickletJoystick#set_debounce_period.

CALLBACK_POSITION

This callback is triggered periodically with the period that is set by BrickletJoystick#set_position_callback_period. The parameter is the position of the joystick.

The CALLBACK_POSITION callback is only triggered if the position has changed since the last triggering.

CALLBACK_POSITION_REACHED

This callback is triggered when the threshold as set by BrickletJoystick#set_position_callback_threshold is reached. The parameters are the position of the joystick.

If the threshold keeps being reached, the callback is triggered periodically with the period as set by BrickletJoystick#set_debounce_period.

CALLBACK_PRESSED

This callback is triggered when the button is pressed.

CALLBACK_RELEASED

This callback is triggered when the button is released.

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_joystick.rb, line 81
def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_GET_POSITION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_IS_PRESSED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_ANALOG_VALUE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_CALIBRATE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_SET_POSITION_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_POSITION_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_POSITION_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_POSITION_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_DEBOUNCE_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_DEBOUNCE_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_POSITION] = [12, 's s']
  @callback_formats[CALLBACK_ANALOG_VALUE] = [12, 'S S']
  @callback_formats[CALLBACK_POSITION_REACHED] = [12, 's s']
  @callback_formats[CALLBACK_ANALOG_VALUE_REACHED] = [12, 'S S']
  @callback_formats[CALLBACK_PRESSED] = [8, '']
  @callback_formats[CALLBACK_RELEASED] = [8, '']

  @ipcon.add_device self
end

Public Instance Methods

calibrate() click to toggle source

Calibrates the middle position of the joystick. If your Joystick Bricklet does not return x=0 and y=0 in the middle position, call this function while the joystick is standing still in the middle position.

The resulting calibration will be saved on the EEPROM of the Joystick Bricklet, thus you only have to calibrate it once.

# File lib/tinkerforge/bricklet_joystick.rb, line 157
def calibrate
  check_validity

  send_request FUNCTION_CALIBRATE, [], '', 8, ''
end
get_analog_value() click to toggle source

Returns the values as read by a 12-bit analog-to-digital converter.

.. note

The values returned by BrickletJoystick#get_position are averaged over several samples to yield less noise, while BrickletJoystick#get_analog_value gives back raw unfiltered analog values. The only reason to use BrickletJoystick#get_analog_value is, if you need the full resolution of the analog-to-digital converter.

If you want the analog values periodically, it is recommended to use the CALLBACK_ANALOG_VALUE callback and set the period with BrickletJoystick#set_analog_value_callback_period.

# File lib/tinkerforge/bricklet_joystick.rb, line 145
def get_analog_value
  check_validity

  send_request FUNCTION_GET_ANALOG_VALUE, [], '', 12, 'S S'
end
get_analog_value_callback_period() click to toggle source

Returns the period as set by BrickletJoystick#set_analog_value_callback_period.

# File lib/tinkerforge/bricklet_joystick.rb, line 193
def get_analog_value_callback_period
  check_validity

  send_request FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD, [], '', 12, 'L'
end
get_analog_value_callback_threshold() click to toggle source

Returns the threshold as set by BrickletJoystick#set_analog_value_callback_threshold.

# File lib/tinkerforge/bricklet_joystick.rb, line 241
def get_analog_value_callback_threshold
  check_validity

  send_request FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD, [], '', 17, 'k S S S S'
end
get_debounce_period() click to toggle source

Returns the debounce period as set by BrickletJoystick#set_debounce_period.

# File lib/tinkerforge/bricklet_joystick.rb, line 265
def get_debounce_period
  check_validity

  send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, 'L'
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_joystick.rb, line 281
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_position() click to toggle source

Returns the position of the joystick. The middle position of the joystick is x=0, y=0. The returned values are averaged and calibrated (see BrickletJoystick#calibrate).

If you want to get the position periodically, it is recommended to use the CALLBACK_POSITION callback and set the period with BrickletJoystick#set_position_callback_period.

# File lib/tinkerforge/bricklet_joystick.rb, line 118
def get_position
  check_validity

  send_request FUNCTION_GET_POSITION, [], '', 12, 's s'
end
get_position_callback_period() click to toggle source

Returns the period as set by BrickletJoystick#set_position_callback_period.

# File lib/tinkerforge/bricklet_joystick.rb, line 175
def get_position_callback_period
  check_validity

  send_request FUNCTION_GET_POSITION_CALLBACK_PERIOD, [], '', 12, 'L'
end
get_position_callback_threshold() click to toggle source

Returns the threshold as set by BrickletJoystick#set_position_callback_threshold.

# File lib/tinkerforge/bricklet_joystick.rb, line 217
def get_position_callback_threshold
  check_validity

  send_request FUNCTION_GET_POSITION_CALLBACK_THRESHOLD, [], '', 17, 'k s s s s'
end
is_pressed() click to toggle source

Returns true if the button is pressed and false otherwise.

It is recommended to use the CALLBACK_PRESSED and CALLBACK_RELEASED callbacks to handle the button.

# File lib/tinkerforge/bricklet_joystick.rb, line 128
def is_pressed
  check_validity

  send_request FUNCTION_IS_PRESSED, [], '', 9, '?'
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_joystick.rb, line 286
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
set_analog_value_callback_period(period) click to toggle source

Sets the period with which the CALLBACK_ANALOG_VALUE callback is triggered periodically. A value of 0 turns the callback off.

The CALLBACK_ANALOG_VALUE callback is only triggered if the analog values have changed since the last triggering.

# File lib/tinkerforge/bricklet_joystick.rb, line 186
def set_analog_value_callback_period(period)
  check_validity

  send_request FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD, [period], 'L', 8, ''
end
set_analog_value_callback_threshold(option, min_x, max_x, min_y, max_y) click to toggle source

Sets the thresholds for the CALLBACK_ANALOG_VALUE_REACHED callback.

The following options are possible:

"Option", "Description"

"'x'",    "Callback is turned off"
"'o'",    "Callback is triggered when the analog values are *outside* the min and max values"
"'i'",    "Callback is triggered when the analog values are *inside* the min and max values"
"'<'",    "Callback is triggered when the analog values are smaller than the min values (max is ignored)"
"'>'",    "Callback is triggered when the analog values are greater than the min values (max is ignored)"
# File lib/tinkerforge/bricklet_joystick.rb, line 234
def set_analog_value_callback_threshold(option, min_x, max_x, min_y, max_y)
  check_validity

  send_request FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD, [option, min_x, max_x, min_y, max_y], 'k S S S S', 8, ''
end
set_debounce_period(debounce) click to toggle source

Sets the period with which the threshold callbacks

are triggered, if the thresholds

keep being reached.

# File lib/tinkerforge/bricklet_joystick.rb, line 258
def set_debounce_period(debounce)
  check_validity

  send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, ''
end
set_position_callback_period(period) click to toggle source

Sets the period with which the CALLBACK_POSITION callback is triggered periodically. A value of 0 turns the callback off.

The CALLBACK_POSITION callback is only triggered if the position has changed since the last triggering.

# File lib/tinkerforge/bricklet_joystick.rb, line 168
def set_position_callback_period(period)
  check_validity

  send_request FUNCTION_SET_POSITION_CALLBACK_PERIOD, [period], 'L', 8, ''
end
set_position_callback_threshold(option, min_x, max_x, min_y, max_y) click to toggle source

Sets the thresholds for the CALLBACK_POSITION_REACHED callback.

The following options are possible:

"Option", "Description"

"'x'",    "Callback is turned off"
"'o'",    "Callback is triggered when the position is *outside* the min and max values"
"'i'",    "Callback is triggered when the position is *inside* the min and max values"
"'<'",    "Callback is triggered when the position is smaller than the min values (max is ignored)"
"'>'",    "Callback is triggered when the position is greater than the min values (max is ignored)"
# File lib/tinkerforge/bricklet_joystick.rb, line 210
def set_position_callback_threshold(option, min_x, max_x, min_y, max_y)
  check_validity

  send_request FUNCTION_SET_POSITION_CALLBACK_THRESHOLD, [option, min_x, max_x, min_y, max_y], 'k s s s s', 8, ''
end