Class: Tinkerforge::BrickletIndustrialDigitalIn4

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

Overview

4 galvanically isolated digital inputs

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

223
DEVICE_DISPLAY_NAME =

:nodoc:

'Industrial Digital In 4 Bricklet'
CALLBACK_INTERRUPT =

This callback is triggered whenever a change of the voltage level is detected on pins where the interrupt was activated with BrickletIndustrialDigitalIn4#set_interrupt.

The values are a bitmask that specifies which interrupts occurred and the current value bitmask.

For example:

  • (1, 1) or (0b0001, 0b0001) means that an interrupt on pin 0 occurred and currently pin 0 is high and pins 1-3 are low.

  • (9, 14) or (0b1001, 0b1110) means that interrupts on pins 0 and 3 occurred and currently pin 0 is low and pins 1-3 are high.

The interrupts use the grouping as set by BrickletIndustrialDigitalIn4#set_group.

9
FUNCTION_GET_VALUE =

:nodoc:

1
FUNCTION_SET_GROUP =

:nodoc:

2
FUNCTION_GET_GROUP =

:nodoc:

3
FUNCTION_GET_AVAILABLE_FOR_GROUP =

:nodoc:

4
FUNCTION_SET_DEBOUNCE_PERIOD =

:nodoc:

5
FUNCTION_GET_DEBOUNCE_PERIOD =

:nodoc:

6
FUNCTION_SET_INTERRUPT =

:nodoc:

7
FUNCTION_GET_INTERRUPT =

:nodoc:

8
FUNCTION_GET_EDGE_COUNT =

:nodoc:

10
FUNCTION_SET_EDGE_COUNT_CONFIG =

:nodoc:

11
FUNCTION_GET_EDGE_COUNT_CONFIG =

:nodoc:

12
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::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) ⇒ BrickletIndustrialDigitalIn4

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 55

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

  @api_version = [2, 0, 1]

  @response_expected[FUNCTION_GET_VALUE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_GROUP] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_GROUP] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_AVAILABLE_FOR_GROUP] = 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_SET_INTERRUPT] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_INTERRUPT] = 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_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_INTERRUPT] = [12, 'S S']

  @ipcon.add_device self
end

Instance Method Details

#get_available_for_groupObject

Returns a bitmask of ports that are available for grouping. For example the value 5 or 0b0101 means: Port A and port C are connected to Bricklets that can be grouped together.



130
131
132
133
134
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 130

def get_available_for_group
  check_validity

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

#get_debounce_periodObject

Returns the debounce period as set by BrickletIndustrialDigitalIn4#set_debounce_period.



148
149
150
151
152
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 148

def get_debounce_period
  check_validity

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

#get_edge_count(pin, reset_counter) ⇒ Object

Returns the current value of the edge counter for the selected pin. You can configure the edges that are counted with BrickletIndustrialDigitalIn4#set_edge_count_config.

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

The edge counters use the grouping as set by BrickletIndustrialDigitalIn4#set_group.

.. versionadded

2.0.1$nbsp;(Plugin)



186
187
188
189
190
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 186

def get_edge_count(pin, reset_counter)
  check_validity

  send_request FUNCTION_GET_EDGE_COUNT, [pin, reset_counter], 'C ?', 12, 'L'
end

#get_edge_count_config(pin) ⇒ Object

Returns the edge type and debounce time for the selected pin as set by BrickletIndustrialDigitalIn4#set_edge_count_config.

.. versionadded

2.0.1$nbsp;(Plugin)



220
221
222
223
224
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 220

def get_edge_count_config(pin)
  check_validity

  send_request FUNCTION_GET_EDGE_COUNT_CONFIG, [pin], 'C', 10, 'C C'
end

#get_groupObject

Returns the group as set by BrickletIndustrialDigitalIn4#set_group



121
122
123
124
125
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 121

def get_group
  check_validity

  send_request FUNCTION_GET_GROUP, [], '', 12, 'k4'
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|



236
237
238
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 236

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

#get_interruptObject

Returns the interrupt bitmask as set by BrickletIndustrialDigitalIn4#set_interrupt.



171
172
173
174
175
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 171

def get_interrupt
  check_validity

  send_request FUNCTION_GET_INTERRUPT, [], '', 10, 'S'
end

#get_valueObject

Returns the input value with a bitmask. The bitmask is 16bit long, true refers to high and false refers to low.

For example: The value 3 or 0b0011 means that pins 0-1 are high and the other pins are low.

If no groups are used (see BrickletIndustrialDigitalIn4#set_group), the pins correspond to the markings on the IndustrialDigital In 4 Bricklet.

If groups are used, the pins correspond to the element in the group. Element 1 in the group will get pins 0-3, element 2 pins 4-7, element 3 pins 8-11 and element 4 pins 12-15.



90
91
92
93
94
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 90

def get_value
  check_validity

  send_request FUNCTION_GET_VALUE, [], '', 10, 'S'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



241
242
243
244
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 241

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

#set_debounce_period(debounce) ⇒ Object

Sets the debounce period of the CALLBACK_INTERRUPT callback.

For example: If you set this value to 100, you will get the interrupt maximal every 100ms. This is necessary if something that bounces is connected to the Digital In 4 Bricklet, such as a button.



141
142
143
144
145
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 141

def set_debounce_period(debounce)
  check_validity

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

#set_edge_count_config(selection_mask, edge_type, debounce) ⇒ Object

Configures the edge counter for the selected pins. A bitmask of 9 or 0b1001 will enable the edge counter for pins 0 and 3.

The edge type parameter configures if rising edges, falling edges or both are counted if the pin is configured for input. Possible edge types are:

  • 0 = rising

  • 1 = falling

  • 2 = both

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.

The edge counters use the grouping as set by BrickletIndustrialDigitalIn4#set_group.

.. versionadded

2.0.1$nbsp;(Plugin)



210
211
212
213
214
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 210

def set_edge_count_config(selection_mask, edge_type, debounce)
  check_validity

  send_request FUNCTION_SET_EDGE_COUNT_CONFIG, [selection_mask, edge_type, debounce], 'S C C', 8, ''
end

#set_group(group) ⇒ Object

Sets a group of Digital In 4 Bricklets that should work together. You can find Bricklets that can be grouped together with BrickletIndustrialDigitalIn4#get_available_for_group.

The group consists of 4 elements. Element 1 in the group will get pins 0-3, element 2 pins 4-7, element 3 pins 8-11 and element 4 pins 12-15.

Each element can either be one of the ports (‘a’ to ‘d’) or ‘n’ if it should not be used.

For example: If you have two Digital In 4 Bricklets connected to port A and port B respectively, you could call with “[‘a’, ‘b’, ‘n’, ‘n’]“.

Now the pins on the Digital In 4 on port A are assigned to 0-3 and the pins on the Digital In 4 on port B are assigned to 4-7. It is now possible to call BrickletIndustrialDigitalIn4#get_value and read out two Bricklets at the same time.

Changing the group configuration resets all edge counter configurations and values.



114
115
116
117
118
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 114

def set_group(group)
  check_validity

  send_request FUNCTION_SET_GROUP, [group], 'k4', 8, ''
end

#set_interrupt(interrupt_mask) ⇒ Object

Sets the pins on which an interrupt is activated with a bitmask. Interrupts are triggered on changes of the voltage level of the pin, i.e. changes from high to low and low to high.

For example: An interrupt bitmask of 9 or 0b1001 will enable the interrupt for pins 0 and 3.

The interrupts use the grouping as set by BrickletIndustrialDigitalIn4#set_group.

The interrupt is delivered with the CALLBACK_INTERRUPT callback.



164
165
166
167
168
# File 'lib/tinkerforge/bricklet_industrial_digital_in_4.rb', line 164

def set_interrupt(interrupt_mask)
  check_validity

  send_request FUNCTION_SET_INTERRUPT, [interrupt_mask], 'S', 8, ''
end