class Tinkerforge::BrickletRotaryEncoder

360° rotary encoder with push-button

Constants

CALLBACK_COUNT

This callback is triggered periodically with the period that is set by BrickletRotaryEncoder#set_count_callback_period. The parameter is the count of the encoder.

The CALLBACK_COUNT callback is only triggered if the count has changed since the last triggering.

CALLBACK_COUNT_REACHED

This callback is triggered when the threshold as set by BrickletRotaryEncoder#set_count_callback_threshold is reached. The parameter is the count of the encoder.

If the threshold keeps being reached, the callback is triggered periodically with the period as set by BrickletRotaryEncoder#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_rotary_encoder.rb, line 59
def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_GET_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_COUNT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_COUNT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_COUNT_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_COUNT_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_IS_PRESSED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_COUNT] = [12, 'l']
  @callback_formats[CALLBACK_COUNT_REACHED] = [12, 'l']
  @callback_formats[CALLBACK_PRESSED] = [8, '']
  @callback_formats[CALLBACK_RELEASED] = [8, '']

  @ipcon.add_device self
end

Public Instance Methods

get_count(reset) click to toggle source

Returns the current count of the encoder. If you set reset to true, the count is set back to 0 directly after the current count is read.

The encoder has 24 steps per rotation

Turning the encoder to the left decrements the counter, so a negative count is possible.

# File lib/tinkerforge/bricklet_rotary_encoder.rb, line 90
def get_count(reset)
  check_validity

  send_request FUNCTION_GET_COUNT, [reset], '?', 12, 'l'
end
get_count_callback_period() click to toggle source

Returns the period as set by BrickletRotaryEncoder#set_count_callback_period.

# File lib/tinkerforge/bricklet_rotary_encoder.rb, line 108
def get_count_callback_period
  check_validity

  send_request FUNCTION_GET_COUNT_CALLBACK_PERIOD, [], '', 12, 'L'
end
get_count_callback_threshold() click to toggle source

Returns the threshold as set by BrickletRotaryEncoder#set_count_callback_threshold.

# File lib/tinkerforge/bricklet_rotary_encoder.rb, line 132
def get_count_callback_threshold
  check_validity

  send_request FUNCTION_GET_COUNT_CALLBACK_THRESHOLD, [], '', 17, 'k l l'
end
get_debounce_period() click to toggle source

Returns the debounce period as set by BrickletRotaryEncoder#set_debounce_period.

# File lib/tinkerforge/bricklet_rotary_encoder.rb, line 154
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_rotary_encoder.rb, line 180
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 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_rotary_encoder.rb, line 164
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_rotary_encoder.rb, line 185
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
set_count_callback_period(period) click to toggle source

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

The CALLBACK_COUNT callback is only triggered if the count has changed since the last triggering.

# File lib/tinkerforge/bricklet_rotary_encoder.rb, line 101
def set_count_callback_period(period)
  check_validity

  send_request FUNCTION_SET_COUNT_CALLBACK_PERIOD, [period], 'L', 8, ''
end
set_count_callback_threshold(option, min, max) click to toggle source

Sets the thresholds for the CALLBACK_COUNT_REACHED callback.

The following options are possible:

"Option", "Description"

"'x'",    "Callback is turned off"
"'o'",    "Callback is triggered when the count is *outside* the min and max values"
"'i'",    "Callback is triggered when the count is *inside* the min and max values"
"'<'",    "Callback is triggered when the count is smaller than the min value (max is ignored)"
"'>'",    "Callback is triggered when the count is greater than the min value (max is ignored)"
# File lib/tinkerforge/bricklet_rotary_encoder.rb, line 125
def set_count_callback_threshold(option, min, max)
  check_validity

  send_request FUNCTION_SET_COUNT_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 8, ''
end
set_debounce_period(debounce) click to toggle source

Sets the period with which the threshold callback

is triggered, if the thresholds

keeps being reached.

# File lib/tinkerforge/bricklet_rotary_encoder.rb, line 147
def set_debounce_period(debounce)
  check_validity

  send_request FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 8, ''
end