Class: Tinkerforge::BrickletDualRelay

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

Overview

Two relays to switch AC/DC devices

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

26
DEVICE_DISPLAY_NAME =

:nodoc:

'Dual Relay Bricklet'
CALLBACK_MONOFLOP_DONE =

This callback is triggered whenever a monoflop timer reaches 0. The parameter contain the relay (1 or 2) and the current state of the relay (the state after the monoflop).

5
FUNCTION_SET_STATE =

:nodoc:

1
FUNCTION_GET_STATE =

:nodoc:

2
FUNCTION_SET_MONOFLOP =

:nodoc:

3
FUNCTION_GET_MONOFLOP =

:nodoc:

4
FUNCTION_SET_SELECTED_STATE =

:nodoc:

6
FUNCTION_GET_IDENTITY =

:nodoc:

255

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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tinkerforge/bricklet_dual_relay.rb', line 35

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

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_SET_STATE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_MONOFLOP] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_MONOFLOP] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_SELECTED_STATE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_MONOFLOP_DONE] = [10, 'C ?']

  @ipcon.add_device self
end

Instance Method Details

#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|



124
125
126
# File 'lib/tinkerforge/bricklet_dual_relay.rb', line 124

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

#get_monoflop(relay) ⇒ Object

Returns (for the given relay) the current state and the time as set by BrickletDualRelay#set_monoflop as well as the remaining time until the state flips.

If the timer is not running currently, the remaining time will be returned as 0.



97
98
99
100
101
# File 'lib/tinkerforge/bricklet_dual_relay.rb', line 97

def get_monoflop(relay)
  check_validity

  send_request FUNCTION_GET_MONOFLOP, [relay], 'C', 17, '? L L'
end

#get_stateObject

Returns the state of the relays, true means on and false means off.



67
68
69
70
71
# File 'lib/tinkerforge/bricklet_dual_relay.rb', line 67

def get_state
  check_validity

  send_request FUNCTION_GET_STATE, [], '', 10, '? ?'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



129
130
131
132
# File 'lib/tinkerforge/bricklet_dual_relay.rb', line 129

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

#set_monoflop(relay, state, time) ⇒ Object

The first parameter can be 1 or 2 (relay 1 or relay 2). The second parameter is the desired state of the relay (true means on and false means off). The third parameter indicates the time that the relay should hold the state.

If this function is called with the parameters (1, true, 1500): Relay 1 will turn on and in 1.5s it will turn off again.

A monoflop can be used as a failsafe mechanism. For example: Lets assume you have a RS485 bus and a Dual Relay Bricklet connected to one of the slave stacks. You can now call this function every second, with a time parameter of two seconds. The relay will be on all the time. If now the RS485 connection is lost, the relay will turn off in at most two seconds.



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

def set_monoflop(relay, state, time)
  check_validity

  send_request FUNCTION_SET_MONOFLOP, [relay, state, time], 'C ? L', 8, ''
end

#set_selected_state(relay, state) ⇒ Object

Sets the state of the selected relay (1 or 2), true means on and false means off.

A running monoflop timer for the selected relay will be aborted if this function is called.

The other relay remains untouched.



108
109
110
111
112
# File 'lib/tinkerforge/bricklet_dual_relay.rb', line 108

def set_selected_state(relay, state)
  check_validity

  send_request FUNCTION_SET_SELECTED_STATE, [relay, state], 'C ?', 8, ''
end

#set_state(relay1, relay2) ⇒ Object

Sets the state of the relays, true means on and false means off. For example: (true, false) turns relay 1 on and relay 2 off.

If you just want to set one of the relays and don’t know the current state of the other relay, you can get the state with BrickletDualRelay#get_state or you can use BrickletDualRelay#set_selected_state.

All running monoflop timers will be aborted if this function is called.



60
61
62
63
64
# File 'lib/tinkerforge/bricklet_dual_relay.rb', line 60

def set_state(relay1, relay2)
  check_validity

  send_request FUNCTION_SET_STATE, [relay1, relay2], '? ?', 8, ''
end