Class: Tinkerforge::BrickletRotaryEncoder

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

Overview

360° rotary encoder with push-button

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

236
DEVICE_DISPLAY_NAME =

:nodoc:

'Rotary Encoder Bricklet'
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.

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

8
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.

9
CALLBACK_PRESSED =

This callback is triggered when the button is pressed.

11
CALLBACK_RELEASED =

This callback is triggered when the button is released.

12
FUNCTION_GET_COUNT =

:nodoc:

1
FUNCTION_SET_COUNT_CALLBACK_PERIOD =

:nodoc:

2
FUNCTION_GET_COUNT_CALLBACK_PERIOD =

:nodoc:

3
FUNCTION_SET_COUNT_CALLBACK_THRESHOLD =

:nodoc:

4
FUNCTION_GET_COUNT_CALLBACK_THRESHOLD =

:nodoc:

5
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

6
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

7
FUNCTION_IS_PRESSED =

:nodoc:

10
FUNCTION_GET_IDENTITY =

:nodoc:

255
THRESHOLD_OPTION_OFF =

:nodoc:

'x'
THRESHOLD_OPTION_OUTSIDE =

:nodoc:

'o'
THRESHOLD_OPTION_INSIDE =

:nodoc:

'i'
THRESHOLD_OPTION_SMALLER =

:nodoc:

'<'
THRESHOLD_OPTION_GREATER =

:nodoc:

'>'

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) ⇒ BrickletRotaryEncoder

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 58

def initialize(uid, ipcon)
  super uid, ipcon

  @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[CALLBACK_COUNT] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_COUNT_REACHED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_IS_PRESSED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[CALLBACK_PRESSED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[CALLBACK_RELEASED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_COUNT] = 'l'
  @callback_formats[CALLBACK_COUNT_REACHED] = 'l'
  @callback_formats[CALLBACK_PRESSED] = ''
  @callback_formats[CALLBACK_RELEASED] = ''
end

Instance Method Details

#get_count(reset) ⇒ Object

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.



91
92
93
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 91

def get_count(reset)
  send_request(FUNCTION_GET_COUNT, [reset], '?', 4, 'l')
end

#get_count_callback_periodObject

Returns the period as set by BrickletRotaryEncoder#set_count_callback_period.



107
108
109
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 107

def get_count_callback_period
  send_request(FUNCTION_GET_COUNT_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_count_callback_thresholdObject

Returns the threshold as set by BrickletRotaryEncoder#set_count_callback_threshold.



129
130
131
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 129

def get_count_callback_threshold
  send_request(FUNCTION_GET_COUNT_CALLBACK_THRESHOLD, [], '', 9, 'k l l')
end

#get_debounce_periodObject

Returns the debounce period as set by BrickletRotaryEncoder#set_debounce_period.



149
150
151
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 149

def get_debounce_period
  send_request(FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 4, 'L')
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|



169
170
171
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 169

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

#is_pressedObject

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.



157
158
159
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 157

def is_pressed
  send_request(FUNCTION_IS_PRESSED, [], '', 1, '?')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



174
175
176
177
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 174

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

#set_count_callback_period(period) ⇒ Object

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

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

The default value is 0.



102
103
104
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 102

def set_count_callback_period(period)
  send_request(FUNCTION_SET_COUNT_CALLBACK_PERIOD, [period], 'L', 0, '')
end

#set_count_callback_threshold(option, min, max) ⇒ Object

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)"

The default value is (‘x’, 0, 0).



124
125
126
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 124

def set_count_callback_threshold(option, min, max)
  send_request(FUNCTION_SET_COUNT_CALLBACK_THRESHOLD, [option, min, max], 'k l l', 0, '')
end

#set_debounce_period(debounce) ⇒ Object

Sets the period in ms with which the threshold callback

  • CALLBACK_COUNT_REACHED

is triggered, if the thresholds

  • BrickletRotaryEncoder#set_count_callback_threshold

keeps being reached.

The default value is 100.



144
145
146
# File 'lib/tinkerforge/bricklet_rotary_encoder.rb', line 144

def set_debounce_period(debounce)
  send_request(FUNCTION_SET_DEBOUNCE_PERIOD, [debounce], 'L', 0, '')
end