This is the description of the Ruby API bindings for the Tilt Bricklet. General information and technical specifications for the Tilt Bricklet are summarized in its hardware description.
An installation guide for the Ruby API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
1#!/usr/bin/env ruby
2# -*- ruby encoding: utf-8 -*-
3
4require 'tinkerforge/ip_connection'
5require 'tinkerforge/bricklet_tilt'
6
7include Tinkerforge
8
9HOST = 'localhost'
10PORT = 4223
11UID = 'XYZ' # Change XYZ to the UID of your Tilt Bricklet
12
13ipcon = IPConnection.new # Create IP connection
14t = BrickletTilt.new UID, ipcon # Create device object
15
16ipcon.connect HOST, PORT # Connect to brickd
17# Don't use device before ipcon is connected
18
19# Get current tilt state
20state = t.get_tilt_state
21
22if state == BrickletTilt::TILT_STATE_CLOSED
23 puts "Tilt State: Closed"
24elsif state == BrickletTilt::TILT_STATE_OPEN
25 puts "Tilt State: Open"
26elsif state == BrickletTilt::TILT_STATE_CLOSED_VIBRATING
27 puts "Tilt State: Closed Vibrating"
28end
29
30puts 'Press key to exit'
31$stdin.gets
32ipcon.disconnect
Download (example_callback.rb)
1#!/usr/bin/env ruby
2# -*- ruby encoding: utf-8 -*-
3
4require 'tinkerforge/ip_connection'
5require 'tinkerforge/bricklet_tilt'
6
7include Tinkerforge
8
9HOST = 'localhost'
10PORT = 4223
11UID = 'XYZ' # Change XYZ to the UID of your Tilt Bricklet
12
13ipcon = IPConnection.new # Create IP connection
14t = BrickletTilt.new UID, ipcon # Create device object
15
16ipcon.connect HOST, PORT # Connect to brickd
17# Don't use device before ipcon is connected
18
19# Enable tilt state callback
20t.enable_tilt_state_callback
21
22# Register tilt state callback
23t.register_callback(BrickletTilt::CALLBACK_TILT_STATE) do |state|
24 if state == BrickletTilt::TILT_STATE_CLOSED
25 puts "Tilt State: Closed"
26 elsif state == BrickletTilt::TILT_STATE_OPEN
27 puts "Tilt State: Open"
28 elsif state == BrickletTilt::TILT_STATE_CLOSED_VIBRATING
29 puts "Tilt State: Closed Vibrating"
30 end
31end
32
33puts 'Press key to exit'
34$stdin.gets
35ipcon.disconnect
All functions listed below are thread-safe.
| Parameters: |
|
|---|---|
| Returns: |
|
Creates an object with the unique device ID uid:
tilt = BrickletTilt.new 'YOUR_DEVICE_UID', ipcon
This object can then be used after the IP Connection is connected.
| Returns: |
|
|---|
Returns the current tilt state. The state can either be
0 = Closed: The ball in the tilt switch closes the circuit.
1 = Open: The ball in the tilt switch does not close the circuit.
2 = Closed Vibrating: The tilt switch is in motion (rapid change between open and close).
The following constants are available for this function:
For state:
BrickletTilt::TILT_STATE_CLOSED = 0
BrickletTilt::TILT_STATE_OPEN = 1
BrickletTilt::TILT_STATE_CLOSED_VIBRATING = 2
| Return Array: |
|
|---|
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 Isolator Bricklet is always at position 'z'.
The device identifier numbers can be found here. There is also a constant for the device identifier of this Bricklet.
| Parameters: |
|
|---|
Registers the given block with the given callback_id.
The available callback IDs with corresponding function signatures are listed below.
Enables the ::CALLBACK_TILT_STATE callback.
Disables the ::CALLBACK_TILT_STATE callback.
| Returns: |
|
|---|
Returns true if the ::CALLBACK_TILT_STATE callback is enabled.
Callbacks can be registered to receive time critical or recurring data from
the device. The registration is done with the
#register_callback function of
the device object. The first parameter is the callback ID and the second
parameter is a block:
tilt.register_callback BrickletTilt::CALLBACK_EXAMPLE, do |param|
puts "#{param}"
end
The available constants with inherent number and type of parameters are described below.
Note
Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.
| Callback Parameters: |
|
|---|
This callback provides the current tilt state. It is called every time the state changes.
See #get_tilt_state for a description of the states.
The following constants are available for this function:
For state:
BrickletTilt::TILT_STATE_CLOSED = 0
BrickletTilt::TILT_STATE_OPEN = 1
BrickletTilt::TILT_STATE_CLOSED_VIBRATING = 2
Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.
| Return Array: |
|
|---|
Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
| Parameters: |
|
|---|---|
| Returns: |
|
Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.
For getter functions this is enabled by default and cannot be disabled,
because those functions will always send a response. For callback configuration
functions it is enabled by default too, but can be disabled by
#set_response_expected. For setter functions it is disabled by default
and can be enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For function_id:
BrickletTilt::FUNCTION_ENABLE_TILT_STATE_CALLBACK = 2
BrickletTilt::FUNCTION_DISABLE_TILT_STATE_CALLBACK = 3
| Parameters: |
|
|---|
Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For function_id:
BrickletTilt::FUNCTION_ENABLE_TILT_STATE_CALLBACK = 2
BrickletTilt::FUNCTION_DISABLE_TILT_STATE_CALLBACK = 3
| Parameters: |
|
|---|
Changes the response expected flag for all setter and callback configuration functions of this device at once.
This constant is used to identify a Tilt Bricklet.
The #get_identity() function and the
IPConnection::CALLBACK_ENUMERATE
callback of the IP Connection have a device_identifier parameter to specify
the Brick's or Bricklet's type.
This constant represents the human readable name of a Tilt Bricklet.