Skip to content

Commit

Permalink
adding crc and parity crap
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jun 2, 2009
1 parent 5bbde11 commit ce1d53c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
22 changes: 19 additions & 3 deletions lib/nfc.rb
@@ -1,15 +1,31 @@
require 'singleton'
require 'nfc/lib_nfc'

class NFC
VERSION = '1.0.0'

attr_reader :device

include Singleton

def initialize
@device = nil
@device = NFC::LibNFC::Device.new(NFC::LibNFC.nfc_connect)
LibNFC.nfc_reader_init(@device.pointer)
end

def connect
@device = NFC::LibNFC::Device.new(NFC::LibNFC.nfc_connect)
def deactivate_field
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_ACTIVATE_FIELD, 0
end

def activate_field
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_ACTIVATE_FIELD, 1
end

def crc= value
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_HANDLE_CRC, value ? 1 : 0
end

def parity= v
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_HANDLE_PARITY, v ? 1 : 0
end
end
6 changes: 6 additions & 0 deletions lib/nfc/lib_nfc.rb
Expand Up @@ -7,6 +7,12 @@ class LibNFC # :nodoc

attach_function :nfc_connect, [], :pointer
attach_function :nfc_disconnect, [:pointer], :void
attach_function :nfc_reader_init, [:pointer], :int
attach_function :nfc_configure, [:pointer, :int, :int], :int

DCO_HANDLE_CRC = 0x00
DCO_HANDLE_PARITY = 0x01
DCO_ACTIVATE_FIELD = 0x10

class Device < FFI::Struct
layout(:name, [:char, 256])
Expand Down
27 changes: 23 additions & 4 deletions test/test_nfc.rb
Expand Up @@ -2,10 +2,29 @@
require "nfc"

class TestNFC < Test::Unit::TestCase
def setup
@nfc = NFC.instance
end

def test_connect
nfc = NFC.new
assert_nil nfc.device
nfc.connect
assert_not_nil nfc.device
assert_not_nil @nfc.device
end

def test_deactivate_field
@nfc.deactivate_field
end

def test_activate_field
@nfc.activate_field
end

def test_crc=
@nfc.crc = true
@nfc.crc = false
end

def test_parity=
@nfc.parity = true
@nfc.parity = false
end
end

0 comments on commit ce1d53c

Please sign in to comment.