Class: NFC::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/nfc.rb

Overview

}}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tty = "/dev/ttyUSB0") ⇒ Reader

{{{



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nfc.rb', line 78

def initialize(tty = "/dev/ttyUSB0") #{{{
  @reader = NFC.nfc_reader_init(tty)
  @thread = nil
  @serialnr = nil
  command('x')

 #     begin
 #       @serialnr = command('b')
 #     end while @serialnr == nil
 #     @serialnr = @serialnr.unpack("C*").map{|e| "%02x" % e}.join
  Signal::trap("INT") do
    puts 'stopping...'
    stop_poll
    @thread.kill
  end
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



76
77
78
# File 'lib/nfc.rb', line 76

def reader
  @reader
end

#serialnrObject (readonly)

Returns the value of attribute serialnr.



77
78
79
# File 'lib/nfc.rb', line 77

def serialnr
  @serialnr
end

Instance Method Details

#command(cmd) ⇒ Object

}}}



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nfc.rb', line 95

def command( cmd) #{{{
  obj = Struct.new(@reader)
  temp = nil
  while temp.nil? do
    NFC.nfc_set_cmd(@reader,cmd)
    NFC.nfc_reader_do(@reader)
    if (obj[:reddit].read_string(obj[:reddit_len])[3..-3]== nil)
      temp = nil
    else
      temp=Result.new(obj[:reddit].read_string(obj[:reddit_len])[3..-3])
    end
    temp = true if cmd=='x'
  end
  temp
end

#command_print(cmd) ⇒ Object

}}}



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nfc.rb', line 111

def command_print(cmd) #{{{
  obj = Struct.new(@reader)
  NFC.nfc_set_cmd(@reader,cmd)
  NFC.nfc_reader_do(@reader)
  if (obj[:reddit].read_string(obj[:reddit_len])[3..-3]== nil)
    temp = nil
  else
    temp=Result.new(obj[:reddit].read_string(obj[:reddit_len])[3..-3])
  end
  p temp
end

#poll(&bl) ⇒ Object

}}}



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/nfc.rb', line 129

def poll(&bl) #{{{
  pr = Proc.new do |obj|
    obj = Struct.new(@reader)
    bl.call(Result.new(obj[:reddit].read_string(obj[:reddit_len])[3..-3]))
  end
  NFC.nfc_reader_on_tag(@reader,pr)
  command('dp')
  NFC.nfc_reader_poll(@reader)
  @thread = Thread.new { Thread::stop }
  @thread.join
end

#read_onceObject

}}}



123
124
125
# File 'lib/nfc.rb', line 123

def read_once #{{{
  NFC.nfc_reader_1_read(@reader)
end

#stop_pollObject

}}}



126
127
128
# File 'lib/nfc.rb', line 126

def stop_poll #{{{
  NFC.nfc_reader_stop_poll(@reader)
end