Class: Tinkerforge::BrickletJoystick

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

Overview

2-axis joystick with push-button

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

210
DEVICE_DISPLAY_NAME =

:nodoc:

'Joystick Bricklet'
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.

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

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

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

18
CALLBACK_PRESSED =

This callback is triggered when the button is pressed.

19
CALLBACK_RELEASED =

This callback is triggered when the button is released.

20
FUNCTION_GET_POSITION =

:nodoc:

1
FUNCTION_IS_PRESSED =

:nodoc:

2
FUNCTION_GET_ANALOG_VALUE =

:nodoc:

3
FUNCTION_CALIBRATE =

:nodoc:

4
FUNCTION_SET_POSITION_CALLBACK_PERIOD =

:nodoc:

5
FUNCTION_GET_POSITION_CALLBACK_PERIOD =

:nodoc:

6
FUNCTION_SET_ANALOG_VALUE_CALLBACK_PERIOD =

:nodoc:

7
FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD =

:nodoc:

8
FUNCTION_SET_POSITION_CALLBACK_THRESHOLD =

:nodoc:

9
FUNCTION_GET_POSITION_CALLBACK_THRESHOLD =

:nodoc:

10
FUNCTION_SET_ANALOG_VALUE_CALLBACK_THRESHOLD =

:nodoc:

11
FUNCTION_GET_ANALOG_VALUE_CALLBACK_THRESHOLD =

:nodoc:

12
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

13
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

14
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::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

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

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tinkerforge/bricklet_joystick.rb', line 82

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

Instance Method Details

#calibrateObject

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.



158
159
160
161
162
# File 'lib/tinkerforge/bricklet_joystick.rb', line 158

def calibrate
  check_validity

  send_request FUNCTION_CALIBRATE, [], '', 8, ''
end

#get_analog_valueObject

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.



146
147
148
149
150
# File 'lib/tinkerforge/bricklet_joystick.rb', line 146

def get_analog_value
  check_validity

  send_request FUNCTION_GET_ANALOG_VALUE, [], '', 12, 'S S'
end

#get_analog_value_callback_periodObject

Returns the period as set by BrickletJoystick#set_analog_value_callback_period.



194
195
196
197
198
# File 'lib/tinkerforge/bricklet_joystick.rb', line 194

def get_analog_value_callback_period
  check_validity

  send_request FUNCTION_GET_ANALOG_VALUE_CALLBACK_PERIOD, [], '', 12, 'L'
end

#get_analog_value_callback_thresholdObject

Returns the threshold as set by BrickletJoystick#set_analog_value_callback_threshold.



242
243
244
245
246
# File 'lib/tinkerforge/bricklet_joystick.rb', line 242

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_periodObject

Returns the debounce period as set by BrickletJoystick#set_debounce_period.



266
267
268
269
270
# File 'lib/tinkerforge/bricklet_joystick.rb', line 266

def get_debounce_period
  check_validity

  send_request FUNCTION_GET_DEBOUNCE_PERIOD, [], '', 12, '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’, ‘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|



282
283
284
# File 'lib/tinkerforge/bricklet_joystick.rb', line 282

def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end

#get_positionObject

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.



119
120
121
122
123
# File 'lib/tinkerforge/bricklet_joystick.rb', line 119

def get_position
  check_validity

  send_request FUNCTION_GET_POSITION, [], '', 12, 's s'
end

#get_position_callback_periodObject

Returns the period as set by BrickletJoystick#set_position_callback_period.



176
177
178
179
180
# File 'lib/tinkerforge/bricklet_joystick.rb', line 176

def get_position_callback_period
  check_validity

  send_request FUNCTION_GET_POSITION_CALLBACK_PERIOD, [], '', 12, 'L'
end

#get_position_callback_thresholdObject

Returns the threshold as set by BrickletJoystick#set_position_callback_threshold.



218
219
220
221
222
# File 'lib/tinkerforge/bricklet_joystick.rb', line 218

def get_position_callback_threshold
  check_validity

  send_request FUNCTION_GET_POSITION_CALLBACK_THRESHOLD, [], '', 17, 'k s s s 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.



129
130
131
132
133
# File 'lib/tinkerforge/bricklet_joystick.rb', line 129

def is_pressed
  check_validity

  send_request FUNCTION_IS_PRESSED, [], '', 9, '?'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



287
288
289
290
# File 'lib/tinkerforge/bricklet_joystick.rb', line 287

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

#set_analog_value_callback_period(period) ⇒ Object

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.



187
188
189
190
191
# File 'lib/tinkerforge/bricklet_joystick.rb', line 187

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

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


235
236
237
238
239
# File 'lib/tinkerforge/bricklet_joystick.rb', line 235

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

Sets the period with which the threshold callbacks

  • CALLBACK_POSITION_REACHED,

  • CALLBACK_ANALOG_VALUE_REACHED

are triggered, if the thresholds

  • BrickletJoystick#set_position_callback_threshold,

  • BrickletJoystick#set_analog_value_callback_threshold

keep being reached.



259
260
261
262
263
# File 'lib/tinkerforge/bricklet_joystick.rb', line 259

def set_debounce_period(debounce)
  check_validity

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

#set_position_callback_period(period) ⇒ Object

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.



169
170
171
172
173
# File 'lib/tinkerforge/bricklet_joystick.rb', line 169

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

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


211
212
213
214
215
# File 'lib/tinkerforge/bricklet_joystick.rb', line 211

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