Class: Tinkerforge::BrickletHallEffect

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

Overview

Detects presence of magnetic field

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

240
DEVICE_DISPLAY_NAME =

:nodoc:

'Hall Effect Bricklet'
CALLBACK_EDGE_COUNT =

This callback is triggered periodically with the period that is set by BrickletHallEffect#set_edge_count_callback_period. The parameters are the current count and the current value (see BrickletHallEffect#get_value and BrickletHallEffect#get_edge_count).

CALLBACK_EDGE_COUNT is only triggered if the count or value changed since the last triggering.

10
FUNCTION_GET_VALUE =

:nodoc:

1
FUNCTION_GET_EDGE_COUNT =

:nodoc:

2
FUNCTION_SET_EDGE_COUNT_CONFIG =

:nodoc:

3
FUNCTION_GET_EDGE_COUNT_CONFIG =

:nodoc:

4
FUNCTION_SET_EDGE_INTERRUPT =

:nodoc:

5
FUNCTION_GET_EDGE_INTERRUPT =

:nodoc:

6
FUNCTION_SET_EDGE_COUNT_CALLBACK_PERIOD =

:nodoc:

7
FUNCTION_GET_EDGE_COUNT_CALLBACK_PERIOD =

:nodoc:

8
FUNCTION_EDGE_INTERRUPT =

:nodoc:

9
FUNCTION_GET_IDENTITY =

:nodoc:

255
EDGE_TYPE_RISING =

:nodoc:

0
EDGE_TYPE_FALLING =

:nodoc:

1
EDGE_TYPE_BOTH =

:nodoc:

2

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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 43

def initialize(uid, ipcon)
  super uid, ipcon

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_GET_VALUE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_EDGE_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_EDGE_COUNT_CONFIG] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_EDGE_COUNT_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_EDGE_INTERRUPT] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_EDGE_INTERRUPT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_EDGE_COUNT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_EDGE_COUNT_CALLBACK_PERIOD] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_EDGE_INTERRUPT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[CALLBACK_EDGE_COUNT] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_EDGE_COUNT] = 'L ?'
end

Instance Method Details

#edge_interruptObject

This callback is triggered every n-th count, as configured with BrickletHallEffect#set_edge_interrupt. The parameters are the current count and the current value (see BrickletHallEffect#get_value and BrickletHallEffect#get_edge_count).



143
144
145
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 143

def edge_interrupt
  send_request(FUNCTION_EDGE_INTERRUPT, [], '', 5, 'L ?')
end

#get_edge_count(reset_counter) ⇒ Object

Returns the current value of the edge counter. You can configure edge type (rising, falling, both) that is counted with BrickletHallEffect#set_edge_count_config.

If you set the reset counter to true, the count is set back to 0 directly after it is read.



74
75
76
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 74

def get_edge_count(reset_counter)
  send_request(FUNCTION_GET_EDGE_COUNT, [reset_counter], '?', 4, 'L')
end

#get_edge_count_callback_periodObject

Returns the period as set by BrickletHallEffect#set_edge_count_callback_period.



136
137
138
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 136

def get_edge_count_callback_period
  send_request(FUNCTION_GET_EDGE_COUNT_CALLBACK_PERIOD, [], '', 4, 'L')
end

#get_edge_count_configObject

Returns the edge type and debounce time as set by BrickletHallEffect#set_edge_count_config.



104
105
106
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 104

def get_edge_count_config
  send_request(FUNCTION_GET_EDGE_COUNT_CONFIG, [], '', 2, 'C C')
end

#get_edge_interruptObject

Returns the edges as set by BrickletHallEffect#set_edge_interrupt.



120
121
122
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 120

def get_edge_interrupt
  send_request(FUNCTION_GET_EDGE_INTERRUPT, [], '', 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|



155
156
157
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 155

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

#get_valueObject

Returns true if a magnetic field of 35 Gauss (3.5mT) or greater is detected.



64
65
66
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 64

def get_value
  send_request(FUNCTION_GET_VALUE, [], '', 1, '?')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



160
161
162
163
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 160

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

#set_edge_count_callback_period(period) ⇒ Object

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

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

The default value is 0.



131
132
133
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 131

def set_edge_count_callback_period(period)
  send_request(FUNCTION_SET_EDGE_COUNT_CALLBACK_PERIOD, [period], 'L', 0, '')
end

#set_edge_count_config(edge_type, debounce) ⇒ Object

The edge type parameter configures if rising edges, falling edges or both are counted. Possible edge types are:

  • 0 = rising (default)

  • 1 = falling

  • 2 = both

A magnetic field of 35 Gauss (3.5mT) or greater causes a falling edge and a magnetic field of 25 Gauss (2.5mT) or smaller causes a rising edge.

If a magnet comes near the Bricklet the signal goes low (falling edge), if a magnet is removed from the vicinity the signal goes high (rising edge).

The debounce time is given in ms.

Configuring an edge counter resets its value to 0.

If you don’t know what any of this means, just leave it at default. The default configuration is very likely OK for you.

Default values: 0 (edge type) and 100ms (debounce time)



99
100
101
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 99

def set_edge_count_config(edge_type, debounce)
  send_request(FUNCTION_SET_EDGE_COUNT_CONFIG, [edge_type, debounce], 'C C', 0, '')
end

#set_edge_interrupt(edges) ⇒ Object

Sets the number of edges until an interrupt is invoked.

If edges is set to n, an interrupt is invoked for every n-th detected edge.

If edges is set to 0, the interrupt is disabled.

Default value is 0.



115
116
117
# File 'lib/tinkerforge/bricklet_hall_effect.rb', line 115

def set_edge_interrupt(edges)
  send_request(FUNCTION_SET_EDGE_INTERRUPT, [edges], 'L', 0, '')
end