Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 171ed283d9d5bb27ec7ed8f8ba1804517814ebb3 @tenderlove committed
Showing with 255 additions and 0 deletions.
  1. +8 −0 .autotest
  2. +6 −0 CHANGELOG.rdoc
  3. +8 −0 Manifest.txt
  4. +73 −0 README.markdown
  5. +18 −0 Rakefile
  6. +3 −0 bin/sphero
  7. +131 −0 lib/sphero.rb
  8. +8 −0 test/test_sphero.rb
8 .autotest
@@ -0,0 +1,8 @@
+# -*- ruby -*-
+
+require 'autotest/restart'
+
+Autotest.add_hook :initialize do |at|
+ at.testlib = 'minitest/autorun'
+ at.find_directories = ARGV unless ARGV.empty?
+end
6 CHANGELOG.rdoc
@@ -0,0 +1,6 @@
+=== 1.0.0 / 2012-07-20
+
+* 1 major enhancement
+
+ * Birthday!
+
8 Manifest.txt
@@ -0,0 +1,8 @@
+.autotest
+CHANGELOG.rdoc
+Manifest.txt
+README.markdown
+Rakefile
+bin/sphero
+lib/sphero.rb
+test/test_sphero.rb
73 README.markdown
@@ -0,0 +1,73 @@
+# sphero
+
+* http://github.com/tenderlove/sphero
+
+## DESCRIPTION:
+
+A ruby gem for controlling your Sphero ball. Sends commands over the TTY
+provided by the bluetooth connection.
+
+## FEATURES/PROBLEMS:
+
+* You need a Sphero
+
+## SYNOPSIS:
+
+```ruby
+s = Sphero.new "/dev/tty.Sphero-PRG-RN-SPP"
+p s.ping
+
+# Roll 0 degrees, speed 125
+p s.roll(125, 0)
+
+trap(:INT) {
+ s.stop # Stop the ball
+ exit!
+}
+
+sleep 1
+loop do
+0.step(360, 30) { |h|
+ h = 0 if h == 360
+
+ # Set the heading to h degrees
+ s.heading = h
+ sleep 1
+}
+end
+sleep 1
+s.stop
+```
+
+## REQUIREMENTS:
+
+* A Sphero ball connected to your computer
+
+## INSTALL:
+
+* gem install sphero
+
+## LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2012 Aaron Patterson
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 Rakefile
@@ -0,0 +1,18 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+Hoe.plugins.delete :rubyforge
+Hoe.plugin :minitest
+Hoe.plugin :gemspec # `gem install hoe-gemspec`
+Hoe.plugin :git # `gem install hoe-git`
+
+Hoe.spec 'sphero' do
+ developer('Aaron Patterson', 'aaron@tenderlovemaking.com')
+ self.readme_file = 'README.markdown'
+ self.history_file = 'CHANGELOG.rdoc'
+ self.extra_rdoc_files = FileList['*.{rdoc,markdown}']
+end
+
+# vim: syntax=ruby
3 bin/sphero
@@ -0,0 +1,3 @@
+#!/usr/bin/env ruby
+
+abort "you need to write me"
131 lib/sphero.rb
@@ -0,0 +1,131 @@
+require 'serialport'
+
+class Sphero
+ VERSION = '1.0.0'
+
+ class Response
+ SOP1 = 0
+ SOP2 = 1
+ MRSP = 2
+ SEQ = 3
+ DLEN = 4
+
+ CODE_OK = 0
+
+ attr_reader :body
+
+ def initialize header, body
+ @header = header
+ @body = body
+ end
+
+ def empty?
+ @header[DLEN] == 1
+ end
+
+ def success?
+ @header[MRSP] == CODE_OK
+ end
+
+ def seq
+ @header[SEQ]
+ end
+ end
+
+ def initialize dev
+ @sp = SerialPort.new dev, 115200, 8, 1, SerialPort::NONE
+ @dev = 0x00
+ @seq = 0x00
+ end
+
+ def ping
+ write 0x01
+ end
+
+ def version
+ write 0x02
+ end
+
+ def bluetooth_info
+ resp = write 0x11
+ [resp.body.take(15).pack('C*'), resp.body.drop(15).pack('C*')]
+ end
+
+ def auto_reconnect= time_s
+ write 0x12, [0x01, time_s]
+ end
+
+ def auto_reconnect
+ write(0x13).body[1]
+ end
+
+ def disable_auto_reconnect
+ write 0x12, [0x00, 0x05]
+ end
+
+ def roll speed, heading, delay = 0x01
+ cmd = [speed, heading >> 8, heading & 0xFF, delay]
+ write 0x30, cmd, 0x02
+ end
+
+ def stop
+ write 0x30, [0x01, 0x00, 0x00, 0x00], 0x02
+ end
+
+ def heading= h
+ write 0x01, [h >> 8, h & 0xFF], 0x02
+ end
+
+ private
+
+ def write cmd, data = [], did = @dev
+ data_len = data.length + 1
+
+ packet = [0xFF, 0xFF, did, cmd, @seq, data_len] + data
+ checksum = packet.drop(2).reduce :+
+
+ packet << ~(checksum % 256)
+ @sp.write packet.pack('C*')
+ @seq += 1
+
+ header = @sp.read(5).unpack('C5')
+ body = @sp.read(header.last).unpack 'C*'
+ response = Response.new header, body
+
+ if response.success?
+ response
+ else
+ raise response
+ end
+ end
+end
+
+
+if $0 == __FILE__
+ begin
+ s = Sphero.new "/dev/tty.Sphero-PRG-RN-SPP"
+ rescue Errno::EBUSY
+ p :wtf
+ retry
+ end
+
+ p s.ping
+ p s.roll(125, 0)
+
+ trap(:INT) {
+ s.stop
+ exit!
+ }
+
+ sleep 1
+ loop do
+ 0.step(360, 30) { |h|
+ h = 0 if h == 360
+
+ s.heading = h
+ sleep 1
+ }
+ end
+ sleep 1
+ s.stop
+end
8 test/test_sphero.rb
@@ -0,0 +1,8 @@
+require 'minitest/autorun'
+require 'sphero'
+
+class TestSphero < MiniTest::Unit::TestCase
+ def test_sanity
+ flunk "write tests or I will kneecap you"
+ end
+end

0 comments on commit 171ed28

Please sign in to comment.
Something went wrong with that request. Please try again.