Class: Tinkerforge::BrickletTilt

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

Overview

Detects inclination of Bricklet (tilt switch open/closed)

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

239
DEVICE_DISPLAY_NAME =

:nodoc:

'Tilt Bricklet'
CALLBACK_TILT_STATE =

This callback provides the current tilt state. It is called every time the state changes.

See BrickletTilt#get_tilt_state for a description of the states.

5
FUNCTION_GET_TILT_STATE =

:nodoc:

1
FUNCTION_ENABLE_TILT_STATE_CALLBACK =

:nodoc:

2
FUNCTION_DISABLE_TILT_STATE_CALLBACK =

:nodoc:

3
FUNCTION_IS_TILT_STATE_CALLBACK_ENABLED =

:nodoc:

4
FUNCTION_GET_IDENTITY =

:nodoc:

255
TILT_STATE_CLOSED =

:nodoc:

0
TILT_STATE_OPEN =

:nodoc:

1
TILT_STATE_CLOSED_VIBRATING =

:nodoc:

2

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

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tinkerforge/bricklet_tilt.rb', line 38

def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_GET_TILT_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_ENABLE_TILT_STATE_CALLBACK] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_DISABLE_TILT_STATE_CALLBACK] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_IS_TILT_STATE_CALLBACK_ENABLED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_TILT_STATE] = [9, 'C']

  @ipcon.add_device self
end

Instance Method Details

#disable_tilt_state_callbackObject

Disables the CALLBACK_TILT_STATE callback.



79
80
81
82
83
# File 'lib/tinkerforge/bricklet_tilt.rb', line 79

def disable_tilt_state_callback
  check_validity

  send_request FUNCTION_DISABLE_TILT_STATE_CALLBACK, [], '', 8, ''
end

#enable_tilt_state_callbackObject

Enables the CALLBACK_TILT_STATE callback.



72
73
74
75
76
# File 'lib/tinkerforge/bricklet_tilt.rb', line 72

def enable_tilt_state_callback
  check_validity

  send_request FUNCTION_ENABLE_TILT_STATE_CALLBACK, [], '', 8, ''
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|



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

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

#get_tilt_stateObject

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

.. image

/Images/Bricklets/bricklet_tilt_mechanics.jpg

:scale: 100 %
:alt: Tilt states
:align: center
:target: ../../_images/Bricklets/bricklet_tilt_mechanics.jpg


65
66
67
68
69
# File 'lib/tinkerforge/bricklet_tilt.rb', line 65

def get_tilt_state
  check_validity

  send_request FUNCTION_GET_TILT_STATE, [], '', 9, 'C'
end

#is_tilt_state_callback_enabledObject

Returns true if the CALLBACK_TILT_STATE callback is enabled.



86
87
88
89
90
# File 'lib/tinkerforge/bricklet_tilt.rb', line 86

def is_tilt_state_callback_enabled
  check_validity

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

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



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

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