class Tinkerforge::BrickletDualRelay

Two relays to switch AC/DC devices

Constants

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

Public Class Methods

new(uid, ipcon) click to toggle source

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

Calls superclass method
# File lib/tinkerforge/bricklet_dual_relay.rb, line 34
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

Public Instance Methods

get_identity() click to toggle source

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|

# File lib/tinkerforge/bricklet_dual_relay.rb, line 123
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_monoflop(relay) click to toggle source

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.

# File lib/tinkerforge/bricklet_dual_relay.rb, line 96
def get_monoflop(relay)
  check_validity

  send_request FUNCTION_GET_MONOFLOP, [relay], 'C', 17, '? L L'
end
get_state() click to toggle source

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

# File lib/tinkerforge/bricklet_dual_relay.rb, line 66
def get_state
  check_validity

  send_request FUNCTION_GET_STATE, [], '', 10, '? ?'
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_dual_relay.rb, line 128
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
set_monoflop(relay, state, time) click to toggle source

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.

# File lib/tinkerforge/bricklet_dual_relay.rb, line 85
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) click to toggle source

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.

# File lib/tinkerforge/bricklet_dual_relay.rb, line 107
def set_selected_state(relay, state)
  check_validity

  send_request FUNCTION_SET_SELECTED_STATE, [relay, state], 'C ?', 8, ''
end
set_state(relay1, relay2) click to toggle source

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.

# File lib/tinkerforge/bricklet_dual_relay.rb, line 59
def set_state(relay1, relay2)
  check_validity

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