Permalink
Browse files

starting conversion to C

  • Loading branch information...
1 parent 2126a65 commit 7deb969c3b51e8d12c79a45dfde7cc04df6cd3bf @tenderlove committed Aug 8, 2009
Showing with 126 additions and 13 deletions.
  1. +11 −8 Rakefile
  2. +56 −0 ext/nfc/extconf.rb
  3. +10 −0 ext/nfc/nfc.c
  4. +11 −0 ext/nfc/nfc.h
  5. +26 −0 ext/nfc/nfc_device.c
  6. +8 −0 ext/nfc/nfc_device.h
  7. +4 −5 lib/nfc.rb
View
@@ -2,15 +2,18 @@
require 'rubygems'
require 'hoe'
-$: << "lib"
-require 'nfc'
+gem 'rake-compiler', '>= 0.4.1'
+require "rake/extensiontask"
-Hoe.new('nfc', NFC::VERSION) do |p|
- p.developer('Aaron Patterson', 'aaronp@rubyforge.org')
- p.readme_file = 'README.rdoc'
- p.history_file = 'CHANGELOG.rdoc'
- p.extra_rdoc_files = FileList['*.rdoc']
- p.extra_deps = ['ffi']
+HOE = Hoe.spec('nfc') do
+ developer('Aaron Patterson', 'aaronp@rubyforge.org')
+ self.readme_file = 'README.rdoc'
+ self.history_file = 'CHANGELOG.rdoc'
+ self.extra_rdoc_files = FileList['*.rdoc']
+end
+
+RET = Rake::ExtensionTask.new("nfc", HOE.spec) do |ext|
+ ext.lib_dir = File.join('lib', 'nfc')
end
# vim: syntax=Ruby
View
@@ -0,0 +1,56 @@
+ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
+
+# :stopdoc:
+
+require 'mkmf'
+
+LIBDIR = Config::CONFIG['libdir']
+INCLUDEDIR = Config::CONFIG['includedir']
+
+$CFLAGS << " -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline"
+
+HEADER_DIRS = [
+ # First search /opt/local for macports
+ '/opt/local/include',
+
+ # Then search /usr/local for people that installed from source
+ '/usr/local/include',
+
+ # Check the ruby install locations
+ INCLUDEDIR,
+
+ # Finally fall back to /usr
+ '/usr/include',
+]
+
+LIB_DIRS = [
+ # First search /opt/local for macports
+ '/opt/local/lib',
+
+ # Then search /usr/local for people that installed from source
+ '/usr/local/lib',
+
+ # Check the ruby install locations
+ LIBDIR,
+
+ # Finally fall back to /usr
+ '/usr/lib',
+]
+
+nfc_dirs = dir_config('nfc', '/opt/local/include', '/opt/local/lib')
+unless ["", ""] == nfc_dirs
+ HEADER_DIRS.unshift nfc_dirs.first
+ LIB_DIRS.unshift nfc_dirs[1]
+end
+
+unless find_header('libnfc/libnfc.h', *HEADER_DIRS)
+ abort "libnfc is missing. please install libnfc: http://libnfc.org/"
+end
+
+unless find_library('nfc', 'nfc_connect', *LIB_DIRS)
+ abort "libnfc is missing. please install libnfc: http://libnfc.org/"
+end
+
+create_makefile('nfc/nfc')
+
+# :startdoc:
View
@@ -0,0 +1,10 @@
+#include <nfc.h>
+
+VALUE cNfc;
+
+void Init_nfc()
+{
+ cNfc = rb_define_class("NFC", rb_cObject);
+
+ init_device();
+}
View
@@ -0,0 +1,11 @@
+#ifndef NFC_H
+#define NFC_H
+
+#include <ruby.h>
+#include <libnfc/libnfc.h>
+
+extern VALUE cNfc;
+
+#include <nfc_device.h>
+
+#endif
View
@@ -0,0 +1,26 @@
+#include <nfc_device.h>
+
+static VALUE connect(VALUE klass)
+{
+ dev_info * dev = nfc_connect();
+ nfc_initiator_init(dev);
+
+ return Data_Wrap_Struct(klass, 0, 0, dev);
+}
+
+static VALUE disconnect(VALUE self)
+{
+ dev_info * dev;
+ Data_Get_Struct(self, dev_info, dev);
+ nfc_disconnect(dev);
+
+ return self;
+}
+
+void init_device()
+{
+ VALUE cNfcDevice = rb_define_class_under(cNfc, "Device", rb_cObject);
+
+ rb_define_singleton_method(cNfcDevice, "connect", connect, 0);
+ rb_define_method(cNfcDevice, "disconnect", disconnect, 0);
+}
View
@@ -0,0 +1,8 @@
+#ifndef NFC_DEVICE_H
+#define NFC_DEVICE_H
+
+#include <nfc.h>
+
+void init_device();
+
+#endif
View
@@ -1,6 +1,7 @@
require 'singleton'
require 'thread'
-require 'nfc/lib_nfc'
+#require 'nfc/lib_nfc'
+require 'nfc/nfc'
class NFC
VERSION = '1.0.0'
@@ -73,12 +74,10 @@ def find
private
def connect
- device = NFC::LibNFC::Device.new(NFC::LibNFC.nfc_connect)
- LibNFC.nfc_initiator_init(device.pointer)
- device
+ NFC::Device.connect
end
def disconnect
- LibNFC.nfc_disconnect device.pointer
+ device.disconnect
end
end

0 comments on commit 7deb969

Please sign in to comment.