class Tinkerforge::BrickletAccelerometer

Measures acceleration in three axis

Constants

CALLBACK_ACCELERATION

This callback is triggered periodically with the period that is set by BrickletAccelerometer#set_acceleration_callback_period. The parameters are the X, Y and Z acceleration. The range is configured with BrickletAccelerometer#set_configuration.

The CALLBACK_ACCELERATION callback is only triggered if the acceleration has changed since the last triggering.

CALLBACK_ACCELERATION_REACHED

This callback is triggered when the threshold as set by BrickletAccelerometer#set_acceleration_callback_threshold is reached. The parameters are the X, Y and Z acceleration. The range is configured with BrickletAccelerometer#set_configuration.

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

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

  @api_version = [2, 0, 1]

  @response_expected[FUNCTION_GET_ACCELERATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_ACCELERATION_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_ACCELERATION_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_ACCELERATION_CALLBACK_THRESHOLD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_ACCELERATION_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_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CONFIGURATION] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_LED_ON] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_LED_OFF] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_IS_LED_ON] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_ACCELERATION] = [14, 's s s']
  @callback_formats[CALLBACK_ACCELERATION_REACHED] = [14, 's s s']

  @ipcon.add_device self
end

Public Instance Methods

get_acceleration() click to toggle source

Returns the acceleration in x, y and z direction. The values are given in gₙ/1000 (1gₙ = 9.80665m/s²). The range is configured with BrickletAccelerometer#set_configuration.

If you want to get the acceleration periodically, it is recommended to use the CALLBACK_ACCELERATION callback and set the period with BrickletAccelerometer#set_acceleration_callback_period.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 112
def get_acceleration
  check_validity

  send_request FUNCTION_GET_ACCELERATION, [], '', 14, 's s s'
end
get_acceleration_callback_period() click to toggle source

Returns the period as set by BrickletAccelerometer#set_acceleration_callback_period.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 130
def get_acceleration_callback_period
  check_validity

  send_request FUNCTION_GET_ACCELERATION_CALLBACK_PERIOD, [], '', 12, 'L'
end
get_acceleration_callback_threshold() click to toggle source

Returns the threshold as set by BrickletAccelerometer#set_acceleration_callback_threshold.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 154
def get_acceleration_callback_threshold
  check_validity

  send_request FUNCTION_GET_ACCELERATION_CALLBACK_THRESHOLD, [], '', 21, 'k s s s s s s'
end
get_configuration() click to toggle source

Returns the configuration as set by BrickletAccelerometer#set_configuration.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 205
def get_configuration
  check_validity

  send_request FUNCTION_GET_CONFIGURATION, [], '', 11, 'C C C'
end
get_debounce_period() click to toggle source

Returns the debounce period as set by BrickletAccelerometer#set_debounce_period.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 176
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_accelerometer.rb, line 242
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_temperature() click to toggle source

Returns the temperature of the accelerometer.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 183
def get_temperature
  check_validity

  send_request FUNCTION_GET_TEMPERATURE, [], '', 10, 's'
end
is_led_on() click to toggle source

Returns true if the LED is enabled, false otherwise.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 226
def is_led_on
  check_validity

  send_request FUNCTION_IS_LED_ON, [], '', 9, '?'
end
led_off() click to toggle source

Disables the LED on the Bricklet.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 219
def led_off
  check_validity

  send_request FUNCTION_LED_OFF, [], '', 8, ''
end
led_on() click to toggle source

Enables the LED on the Bricklet.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 212
def led_on
  check_validity

  send_request FUNCTION_LED_ON, [], '', 8, ''
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

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

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

The CALLBACK_ACCELERATION callback is only triggered if the acceleration has changed since the last triggering.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 123
def set_acceleration_callback_period(period)
  check_validity

  send_request FUNCTION_SET_ACCELERATION_CALLBACK_PERIOD, [period], 'L', 8, ''
end
set_acceleration_callback_threshold(option, min_x, max_x, min_y, max_y, min_z, max_z) click to toggle source

Sets the thresholds for the CALLBACK_ACCELERATION_REACHED callback.

The following options are possible:

"Option", "Description"

"'x'",    "Callback is turned off"
"'o'",    "Callback is triggered when the acceleration is *outside* the min and max values"
"'i'",    "Callback is triggered when the acceleration is *inside* the min and max values"
"'<'",    "Callback is triggered when the acceleration is smaller than the min value (max is ignored)"
"'>'",    "Callback is triggered when the acceleration is greater than the min value (max is ignored)"
# File lib/tinkerforge/bricklet_accelerometer.rb, line 147
def set_acceleration_callback_threshold(option, min_x, max_x, min_y, max_y, min_z, max_z)
  check_validity

  send_request FUNCTION_SET_ACCELERATION_CALLBACK_THRESHOLD, [option, min_x, max_x, min_y, max_y, min_z, max_z], 'k s s s s s s', 8, ''
end
set_configuration(data_rate, full_scale, filter_bandwidth) click to toggle source

Configures the data rate, full scale range and filter bandwidth. Possible values are:

  • Data rate of 0Hz to 1600Hz.

  • Full scale range of ±2gₙ up to ±16gₙ.

  • Filter bandwidth between 50Hz and 800Hz.

Decreasing data rate or full scale range will also decrease the noise on the data.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 198
def set_configuration(data_rate, full_scale, filter_bandwidth)
  check_validity

  send_request FUNCTION_SET_CONFIGURATION, [data_rate, full_scale, filter_bandwidth], 'C C C', 8, ''
end
set_debounce_period(debounce) click to toggle source

Sets the period with which the threshold callback

is triggered, if the threshold

keeps being reached.

# File lib/tinkerforge/bricklet_accelerometer.rb, line 169
def set_debounce_period(debounce)
  check_validity

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