diff --git a/Gemfile b/Gemfile index 465f675..bbcafc8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' -# Specify your gem's dependencies in co2mond.gemspec +# Specify your gem's dependencies in co2mon.gemspec gemspec diff --git a/README.md b/README.md index 323c214..6c0bee6 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,7 @@ -# Co2mond +# Co2mon -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/co2mond`. To experiment with that code, run `bin/console` for an interactive prompt. +Based on https://github.com/dmage/co2mon -TODO: Delete this and the text above, and describe your gem - -## Installation - -Add this line to your application's Gemfile: - -```ruby -gem 'co2mond' -``` - -And then execute: - - $ bundle - -Or install it yourself as: - - $ gem install co2mond - -## Usage - -TODO: Write usage instructions here - -## Development - -After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. - -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). - -## Contributing - -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/co2mond. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. - - -## License - -The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). +## Requirements + $ brew install hidapi diff --git a/Rakefile b/Rakefile index 6335ee5..43022f7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,2 @@ -require 'rake/extensiontask' -spec = Gem::Specification.load('co2mond.gemspec') -Rake::ExtensionTask.new('co2mon', spec) +require "bundler/gem_tasks" +task :default => :spec diff --git a/bin/console b/bin/console index a390041..bab7e9f 100755 --- a/bin/console +++ b/bin/console @@ -1,7 +1,7 @@ #!/usr/bin/env ruby require "bundler/setup" -require "co2mond" +require "co2mon" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. diff --git a/co2mon.gemspec b/co2mon.gemspec new file mode 100644 index 0000000..6c66edd --- /dev/null +++ b/co2mon.gemspec @@ -0,0 +1,25 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'co2mon/version' + +Gem::Specification.new do |spec| + spec.name = "co2mon" + spec.version = Co2mon::VERSION + spec.authors = ["Arkadiy Butermanov"] + spec.email = ["arkadiy.butermanov@flatstack.com"] + + spec.summary = %q{FFI bindings for MasterKit CO2 Monitor} + spec.homepage = "https://github.com/arkadiybutermanov/co2mon" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + spec.bindir = "bin" + spec.extensions << './ext/co2mon/extconf.rb' + spec.require_paths = ["lib"] + + spec.add_dependency "ffi" + + spec.add_development_dependency "bundler", "~> 1.11" + spec.add_development_dependency "rake", "~> 10.0" +end diff --git a/co2mond.gemspec b/co2mond.gemspec deleted file mode 100644 index 057400e..0000000 --- a/co2mond.gemspec +++ /dev/null @@ -1,34 +0,0 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'co2mond/version' - -Gem::Specification.new do |spec| - spec.name = "co2mond" - spec.version = Co2mond::VERSION - spec.authors = ["Arkadiy Butermanov"] - spec.email = ["arkadiy.butermanov@flatstack.com"] - - spec.summary = %q{Write a short summary, because Rubygems requires one.} - spec.description = %q{Write a longer description or delete this line.} - spec.homepage = "https://github.com/dmage/co2mon" - spec.license = "MIT" - - # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or - # delete this section to allow pushing this gem to any host. - if spec.respond_to?(:metadata) - spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'" - else - raise "RubyGems 2.0 or newer is required to protect against public gem pushes." - end - - spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - spec.bindir = "exe" - spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.extensions << './ext/co2mon/extconf.rb' - spec.require_paths = ["lib"] - - spec.add_development_dependency "bundler", "~> 1.11" - spec.add_development_dependency "rake", "~> 10.0" - spec.add_development_dependency "rake-compiler" -end diff --git a/ext/co2mon/co2mon.c b/ext/co2mon/co2mon.c index 594e4a0..fd9ead7 100644 --- a/ext/co2mon/co2mon.c +++ b/ext/co2mon/co2mon.c @@ -35,44 +35,13 @@ typedef hid_device *co2mon_device; typedef unsigned char co2mon_data_t[8]; -int -co2mon_init() -{ - int r = hid_init(); - if (r < 0) - { - fprintf(stderr, "hid_init: error\n"); - } - return r; -} - -void -co2mon_exit() -{ - int r = hid_exit(); - if (r < 0) - { - fprintf(stderr, "hid_exit: error\n"); - } -} - hid_device * co2mon_open_device() { hid_device *dev = hid_open(0x04d9, 0xa052, NULL); - if (!dev) - { - fprintf(stderr, "hid_open: error\n"); - } return dev; } -void -co2mon_close_device(hid_device *dev) -{ - hid_close(dev); -} - int co2mon_device_path(hid_device *dev, char *str, size_t maxlen) { @@ -225,48 +194,29 @@ static int *device_loop(co2mon_device dev) static int *main_loop() { - int error_shown = 0; + static int empty_result[2] = {0}; + co2mon_device dev = co2mon_open_device(); + if (dev == NULL) { - if (!error_shown) - { - fprintf(stderr, "Unable to open CO2 device\n"); - error_shown = 1; - } - } else { - error_shown = 0; + return empty_result; } int *result = device_loop(dev); - co2mon_close_device(dev); + hid_close(dev); return result; } -VALUE get(VALUE self) +int *get() { - int r = co2mon_init(); - if (r < 0) - { - return r; - } + hid_init(); int *result = main_loop(); - co2mon_exit(); + hid_exit(); - VALUE array; - array = rb_ary_new(); - rb_ary_push(array, INT2FIX(result[0])); - rb_ary_push(array, INT2FIX(result[1])); - - return array; -} - -void -Init_co2mon(void) { - VALUE cCo2mon = rb_define_class("Co2mon", rb_cObject); - rb_define_singleton_method(cCo2mon, "get", get, 0); + return result; } diff --git a/lib/co2mon.bundle b/lib/co2mon.bundle deleted file mode 100755 index b4951ce..0000000 Binary files a/lib/co2mon.bundle and /dev/null differ diff --git a/lib/co2mon.rb b/lib/co2mon.rb new file mode 100644 index 0000000..45250bb --- /dev/null +++ b/lib/co2mon.rb @@ -0,0 +1,20 @@ +require "co2mon/version" +require "ffi" + +module Co2mon + extend FFI::Library + + ffi_lib File.expand_path("co2mon.bundle", __dir__) + + attach_function :get, [], :pointer + + def self.get_data + data = get.get_array_of_int32(0, 2) + + { + :co2 => data[0], + :temp => data[1] + } + rescue => e + end +end diff --git a/lib/co2mond/version.rb b/lib/co2mon/version.rb similarity index 61% rename from lib/co2mond/version.rb rename to lib/co2mon/version.rb index 15e0ee3..e14850b 100644 --- a/lib/co2mond/version.rb +++ b/lib/co2mon/version.rb @@ -1,3 +1,3 @@ -module Co2mond +module Co2mon VERSION = "0.1.0" end diff --git a/lib/co2mond.rb b/lib/co2mond.rb deleted file mode 100644 index 5bcaa08..0000000 --- a/lib/co2mond.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "co2mond/version" - -module Co2mond - # Your code goes here... -end - -require "co2mon"