Class: Phidgets::RFID

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/phidgets-ffi/rfid.rb

Overview

This class represents a CPhidgetRFID.

Defined Under Namespace

Classes: RFIDOutputs

Constant Summary collapse

Klass =
Phidgets::FFI::CPhidgetRFID

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#attached?, #attached_to_server?, attributes, #close, #detached?, #detached_to_server?, device_class, #device_class, device_id, #id, #initialize, #label, #label=, #name, #on_attach, #on_detach, #on_error, #on_server_connect, #on_server_disconnect, #on_sleep, #on_wake, #serial_number, #server_address, server_address, #server_id, server_id, server_status, #type, #version, #wait_for_attachment

Instance Attribute Details

#attributesObject (readonly)

The attributes of a PhidgetRFID



16
17
18
# File 'lib/phidgets-ffi/rfid.rb', line 16

def attributes
  @attributes
end

#outputsRFIDOutputs (readonly)

Collection of digital outputs

Returns:



11
12
13
# File 'lib/phidgets-ffi/rfid.rb', line 11

def outputs
  @outputs
end

Instance Method Details

#antennaBoolean

Returns the antenna state of the Phidget.

Returns:

  • (Boolean)

    returns the ratiometric state or raises an error



81
82
83
84
85
# File 'lib/phidgets-ffi/rfid.rb', line 81

def antenna
  ptr = ::FFI::MemoryPointer.new(:int)
  Klass.getAntennaOn(@handle, ptr)
  (ptr.get_int(0) == 0) ? false : true
end

#antenna=(new_state) ⇒ Boolean

Sets the antenna state of the Phidget. True turns the antenna on, false turns it off. The antenna if by default turned off, and needs to be explicitely activated before tags can be read. Control over the antenna allows multiple readers to be used in close proximity, as multiple readers will interfere with each other if their antenna’s are activated simultaneously.

Parameters:

  • new_state (Boolean)

    new antenna state

Returns:

  • (Boolean)

    returns the antenna state, or raises an error



90
91
92
93
94
# File 'lib/phidgets-ffi/rfid.rb', line 90

def antenna=(new_state)
  tmp = new_state ? 1 : 0
  Klass.setAntennaOn(@handle, tmp)
  new_state
end

#last_tagString

Returns the last tag read by the reader. This may or may not still be on the reader - use #tag_present to find out.

Returns:

  • (String)

    returns the last tag or raises an error



117
118
119
120
121
122
123
# File 'lib/phidgets-ffi/rfid.rb', line 117

def last_tag
     tag = ::FFI::MemoryPointer.new(:string)
  proto = ::FFI::MemoryPointer.new(:int)
     Klass.getLastTag2(@handle, tag, proto)
     strPtr = tag.get_pointer(0)
     strPtr.null? ? nil : strPtr.read_string
end

#last_tag_protocolPhidgets::FFI::RFIDTagProtocol

Returns the last tag protocol or raises an error

Returns:



128
129
130
131
132
133
# File 'lib/phidgets-ffi/rfid.rb', line 128

def last_tag_protocol
     tag = ::FFI::MemoryPointer.new(:string)
  proto = ::FFI::MemoryPointer.new(:int)
     Klass.getLastTag2(@handle, tag, proto)
     Phidgets::FFI::RFIDTagProtocol[proto.get_int(0)]
end

#ledBoolean

Returns the LED state.

Returns:

  • (Boolean)

    returns the LED state or raises an error



99
100
101
102
103
# File 'lib/phidgets-ffi/rfid.rb', line 99

def led
  ptr = ::FFI::MemoryPointer.new(:int)
  Klass.getLEDOn(@handle, ptr)
  (ptr.get_int(0) == 0) ? false : true
end

#led=(new_state) ⇒ Boolean

Sets the LED. True turns the LED on, false turns it off. The LED is by default turned off.

Parameters:

  • new_state (Boolean)

    new LED state

Returns:

  • (Boolean)

    returns the LED state or raises an error



108
109
110
111
112
# File 'lib/phidgets-ffi/rfid.rb', line 108

def led=(new_state)
  tmp = new_state ? 1 : 0
  Klass.setLEDOn(@handle, tmp)
  new_state
end

#on_output_change(obj = nil, &block) ⇒ Boolean

Sets an output change handler callback function. This is called when a digital output on the PhidgetRFID board has changed.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

rfid.on_output_change do |device, output, state, obj|
  print "Digital Output  #{output.index}, changed to #{state}\n"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



34
35
36
37
38
39
40
# File 'lib/phidgets-ffi/rfid.rb', line 34

def on_output_change(obj=nil, &block)
  @on_output_change_obj = obj
  @on_output_change = Proc.new { |device, obj_ptr, index, state|
    yield self, @outputs[index], (state == 0 ? false : true), object_for(obj_ptr)
  }
  Klass.set_OnOutputChange_Handler(@handle, @on_output_change, pointer_for(obj))
end

#on_tag(obj = nil, &block) ⇒ Boolean

Sets a tag handler callback function. This is called when a new tag is seen by the reader. The event is only fired one time for a new tag, so the tag has to be removed and then replaced before another tag gained event will fire.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

rfid.on_tag do |device, tag, obj|
  puts "Tag #{tag} detected"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



52
53
54
55
56
57
58
# File 'lib/phidgets-ffi/rfid.rb', line 52

def on_tag(obj=nil, &block)
	  @on_tag_obj = obj
  @on_tag = Proc.new { |device, obj_ptr, tag, proto|
		yield self, tag.read_string, object_for(obj_ptr)
	}
  Klass.set_OnTag2_Handler(@handle, @on_tag, pointer_for(obj))
end

#on_tag_lost(obj = nil, &block) ⇒ Boolean

Sets a tag lost handler callback function. This is called when a tag is removed from the reader

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

rfid.on_tag_lost do |device, tag, obj|
  puts "Tag #{tag} removed"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



70
71
72
73
74
75
76
# File 'lib/phidgets-ffi/rfid.rb', line 70

def on_tag_lost(obj=nil, &block)
	  @on_tag_lost_obj = obj
  @on_tag_lost = Proc.new { |device, obj_ptr, tag, proto|
 yield self, tag.read_string, object_for(obj_ptr)
	}
  Klass.set_OnTagLost2_Handler(@handle, @on_tag_lost, pointer_for(obj))
end

#tag_presentBoolean

Returns a value indicating whether or not a tag is on the reader, or raises an error

Returns:

  • (Boolean)

    returns a value indicating whether or not a tag is on the reader, or raises an error



138
139
140
141
142
# File 'lib/phidgets-ffi/rfid.rb', line 138

def tag_present
  ptr = ::FFI::MemoryPointer.new(:int)
  Klass.getTagStatus(@handle, ptr)
  (ptr.get_int(0) == 0) ? false : true
end

#write(tag, protocol, lock = false) ⇒ Boolean

Writes to a tag.

Parameters:

  • tag (String)

    Tag data to write. See product manual for formatting.

  • protocol (Phidgets::FFI::RFIDTagProtocol)

    Tag Protocol to use.

  • lock (Boolean) (defaults to: false)

    Lock the tag from further writes

Returns:

  • (Boolean)

    returns true or raises an error



150
151
152
153
154
# File 'lib/phidgets-ffi/rfid.rb', line 150

def write(tag, protocol, lock=false)
     tmp = lock ? 1 : 0
  Klass.write(@handle, tag,  Phidgets::FFI::RFIDTagProtocol[protocol], tmp)
  true
end