Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
compare: 959239cc10
Checking mergeability… Don't worry, you can still create the pull request.
  • 9 commits
  • 13 files changed
  • 0 commit comments
  • 2 contributors
Commits on Apr 07, 2012
@docwhat Added development dependency on rake
It annoys me to no end that tools don't auto add this when
they add a Rakefile.
11ffcf1
@steveklabnik steveklabnik Merge pull request #1 from docwhat/gemfile-rake
Added development dependency on rake
ca823aa
Commits on Apr 10, 2012
@docwhat Alrighty, we have working code!
This adds the Connection object.  It is very minimal at the moment.
The specs will actually cause your NXT brick to beep, if you have
it plugged in, powered up, and have the environment variable
'HAS_NXT' set to 'true'.

This is fully documented as well.
600648b
@docwhat Added travis support files 6a30949
@docwhat Added references cb8ecd2
@steveklabnik steveklabnik Merge pull request #2 from docwhat/nxtcomm
Nxtcomm
aa72c47
@docwhat Renamed class constant to LegoNXT 1e81f94
@docwhat better handling for when NXT brick isn't there. a31690d
@docwhat Added check for return value on transmit
Renamed send_bits() to transmit(). I discovered
earlier that I couldn't name it .send()...
959239c
View
3  .rspec
@@ -0,0 +1,3 @@
+--colour
+--format doc
+--order rand
View
5 .travis.yml
@@ -0,0 +1,5 @@
+rvm:
+ - 1.8.7
+ - 1.9.2
+ - 1.9.3
+script: "bundle exec rake spec"
View
6 .yardopts
@@ -0,0 +1,6 @@
+--protected
+--no-private
+--markup markdown
+-
+README.md
+
View
6 Guardfile
@@ -0,0 +1,6 @@
+guard 'rspec', :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
+
View
25 README.md
@@ -1,6 +1,8 @@
# LegoNxt
-TODO: Write a gem description
+[![Build Status](https://secure.travis-ci.org/JumpstartLab/lego_nxt.png?branch=master)](http://travis-ci.org/JumpstartLab/lego_nxt)
+
+An object oriented interface for talking to the LEGO NXT brick.
## Installation
@@ -20,6 +22,18 @@ Or install it yourself as:
TODO: Write usage instructions here
+## Testing
+
+Most tests will work without owning a NXT brick
+
+To run all the tests, including the ones that require a NXT brick, then you need to
+plugin your NXT brick, power it on, and set the environment variable `HAS_NXT` to
+the value `true`.
+
+Example:
+
+ env HAS_NXT=true rspec spec
+
## Contributing
1. Fork it
@@ -27,3 +41,12 @@ TODO: Write usage instructions here
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
+
+## Reference Material
+
+Useful if you're going to be hacking on the code:
+
+* [libusb](https://github.com/larskanis/libusb) - The [docs](http://rubydoc.info/gems/libusb/LIBUSB) in particular.
+* [LEGO's Support Files](http://mindstorms.lego.com/en-us/support/files/default.aspx#Advanced) - In particular:
+ * The Bluetooth Developer Kit -- Appendix 1: LEGO MINDSTORMS NXT Communication protocol
+ * Software Development Kit -- The PDF contains a description of the Executable File Specification
View
7 Rakefile
@@ -1,2 +1,9 @@
#!/usr/bin/env rake
+
require "bundler/gem_tasks"
+require "yard"
+require "rspec/core/rake_task"
+
+YARD::Rake::YardocTask.new
+RSpec::Core::RakeTask.new(:spec)
+
View
17 lego_nxt.gemspec
@@ -13,5 +13,20 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "lego_nxt"
gem.require_paths = ["lib"]
- gem.version = LegoNxt::VERSION
+ gem.version = LegoNXT::VERSION
+
+ gem.add_runtime_dependency 'libusb', '~> 0.1.3'
+
+ gem.add_development_dependency 'rake', '~> 0.9.2'
+
+ # Testing framework
+ gem.add_development_dependency 'rspec', '~> 2.9.0'
+
+ # Continous integration testing
+ gem.add_development_dependency 'guard', '~> 1.0.1'
+ gem.add_development_dependency 'guard-rspec', '~> 0.7.0'
+
+ # Documentation
+ gem.add_development_dependency 'yard'
+ gem.add_development_dependency 'redcarpet'
end
View
4 lib/lego_nxt.rb
@@ -1,3 +1,5 @@
require "lego_nxt/version"
-puts "coming soon..."
+# {LegoNXT} is a library for working with the LEGO NXT bricks.
+module LegoNXT
+end
View
73 lib/lego_nxt/connection.rb
@@ -0,0 +1,73 @@
+require 'libusb'
+require 'lego_nxt/errors'
+
+module LegoNXT
+
+ # Low-level connection object for communicating with the NXT brick.
+ #
+ # It's interface is very simple. Higher levels of abstractions provide
+ # access to opcodes, etc.
+ #
+ class Connection
+ # The USB idVendor code
+ LEGO_VENDOR_ID = 0x0694
+ # The USB idProduct code
+ NXT_PRODUCT_ID = 0x0002
+
+ # The USB endpoint (communication channel) to send data out to the NXT brick
+ USB_ENDPOINT_OUT = 0x01
+
+ # The USB endpoint (communication channel) to receive data from the NXT brick
+ USB_ENDPOINT_IN = 0x82
+
+ # Creates a connection to the NXT brick.
+ #
+ def initialize
+ @mutex = Mutex.new
+ open
+ end
+
+ # Sends a packed string of bits.
+ #
+ # Example:
+ #
+ # # Causes the brick to beep
+ # conn.send_bits [0x80, 0x03, 0xf4, 0x01, 0xf4, 0x01].pack('C*')'
+ #
+ # @see The LEGO MINDSTORMS NXT Communications Protocol (Appendex 1 of the Bluetooth Development Kit)
+ #
+ # @param {String} bits This must be a binary string. Use `Array#pack('C*')` to generate the string.
+ # @return [Boolean] Returns true if all the data was sent and received by the NXT.
+ def transmit bits
+ bytes_sent = @handle.bulk_transfer dataOut: bits, endpoint: USB_ENDPOINT_OUT
+ bytes_sent == bits.length
+ end
+
+ # Closes the connection
+ #
+ # @return [nil]
+ def close
+ return if @handle.nil?
+ @handle.close
+ @handle = nil
+ end
+
+
+ private
+
+ # Opens the connection
+ #
+ # This is triggered automatically by intantiating the object.
+ #
+ # @return [nil]
+ def open
+ @mutex.synchronize do
+ context = LIBUSB::Context.new
+ device = context.devices(:idVendor => LEGO_VENDOR_ID, :idProduct => NXT_PRODUCT_ID).first
+ raise NoDeviceError.new("Please make sure the device is plugged in and powered on") if device.nil?
+ @handle = device.open
+ @handle.claim_interface(0)
+ end
+ end
+ end
+end
View
10 lib/lego_nxt/errors.rb
@@ -0,0 +1,10 @@
+module LegoNXT
+ # All LegoNXT errors are subclassed from this
+ # error class.
+ class LegoNXTError < StandardError
+ end
+
+ # No NXT bricks were found.
+ class NoDeviceError < LegoNXTError
+ end
+end
View
3  lib/lego_nxt/version.rb
@@ -1,3 +1,4 @@
-module LegoNxt
+module LegoNXT
+ # The version of the LegoNXT code
VERSION = "0.0.1"
end
View
51 spec/lib/lego_nxt/connection_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+require 'lego_nxt/connection'
+
+describe LegoNXT::Connection do
+ subject { LegoNXT::Connection.new }
+ after(:each) { HAS_NXT and subject.close }
+
+ describe "#new" do
+ it "does something" do
+ needs_nxt do
+ LegoNXT::Connection.new.close
+ end
+ end
+
+ it "works twice" do
+ needs_nxt do
+ LegoNXT::Connection.new.close
+ LegoNXT::Connection.new.close
+ end
+ end
+ end
+
+ describe ".transmit" do
+ it "should beep" do
+ needs_nxt do
+ subject.transmit [0x80, 0x03, 0xf4, 0x01, 0xf4, 0x01].pack('C*')
+ end
+ end
+
+ it "return success" do
+ needs_nxt do
+ subject.transmit([0x80, 0x03, 0xf4, 0x01, 0xf4, 0x01].pack('C*')).should be_true
+ end
+ end
+ end
+
+ describe ".close" do
+ it "does something" do
+ needs_nxt do
+ subject.close
+ end
+ end
+
+ it "can be called twice with no ill effects" do
+ needs_nxt do
+ subject.close
+ subject.close
+ end
+ end
+ end
+end
View
15 spec/spec_helper.rb
@@ -0,0 +1,15 @@
+HAS_NXT = ENV['HAS_NXT'] == 'true'
+
+if not HAS_NXT
+ puts "**NOTE** Some tests are marked pending because you did not indicate you have a NXT brick."
+ puts " To run all tests, plugin a NXN brick via USB, power it on, and set"
+ puts " the environment variable 'HAS_NXT' to 'true'."
+end
+
+def needs_nxt &block
+ if HAS_NXT
+ yield
+ else
+ pending "This requires a NXT brick"
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.