Skip to content

Commit

Permalink
Two working examples for interacting with the sphero
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Webber committed Jun 15, 2013
0 parents commit 9d0d137
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gem 'midi'
gem 'hybridgroup-sphero'
gem 'listen'
37 changes: 37 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
GEM
remote: https://rubygems.org/
specs:
ffi (1.9.0)
ffi-coremidi (0.2.0)
ffi (>= 1.0)
hybridgroup-sphero (1.3.0)
listen (1.1.4)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
rb-kqueue (>= 0.2)
midi (0.0.9)
midi-eye (>= 0.1.3)
midi-message (>= 0.2.2)
unimidi (>= 0.2.3)
midi-eye (0.2.2)
midi-message
midi-nibbler
unimidi
midi-message (0.3.2)
midi-nibbler (0.1.1)
midi-message
rb-fsevent (0.9.3)
rb-inotify (0.9.0)
ffi (>= 0.5.0)
rb-kqueue (0.2.0)
ffi (>= 0.5.0)
unimidi (0.3.3)
ffi-coremidi

PLATFORMS
ruby

DEPENDENCIES
hybridgroup-sphero
listen
midi
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Sphero Examples

## Installation

```
$ bundle
```

## Sphero

The sphero command example allows you to write and save commands to a file and
have sphero execute those commands. Sphero will automatically execute the
commands written into the `sphero.rb` file whenver they are saved.

### Run

```
$ ruby sphero_saddle.rb
```

### Write

* Open the file `sphero.rb` with your text editor
* Write commands
* Save the file

## Music Sphero

The music sphero command example allows you to write and save commands to a
file and have sphero execute those commands. Sphero will automatically execute
the commands written into the `midi.rb` file whenver they are saved.

### Run

```
$ ruby midi_saddle.rb
```

### Write

* Open the file `midi.rb` with your text editor
* Write commands
* Save the file
23 changes: 23 additions & 0 deletions midi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# .----------------.
# | .--------------. | I AM SPHERO! I can do a lot of things:
# | | ____ | |
# | | .' `. | | sphero.color("azure")
# | | / .--. \ | | sphero.roll(SPEED,DIRECTION_IN_DEGREES)
# | | | | | | | | sphero.stop
# | | \ `--' / | |
# | | `.____.' | |
# | | | |
# | '--------------' |
# '----------------'

if note.note_name == "C"
puts "I just played a C"
sphero.color("green")
puts "Setting the heading"
end

if note.velocity > 100
puts "I just played a note really hard"
sphero.color("gold")
end
178 changes: 178 additions & 0 deletions midi_saddle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
require 'midi'
require 'erb'
require 'sphero'

puts "Connecting to Sphero..."

# class Sphero
# def initialize(connection) ; end

# def roll(*params)
# puts "Rolling with #{params.inspect}"
# end

# def color(*args)
# puts "Changing Color"
# end

# def stop
# puts "Stopping"
# end
# end


sphero = Sphero.new ENV['sphero'] || ARGV[0] || "/dev/tty.Sphero-GRR-RN-SPP"
puts "Connected to Sphero"

NOTES = { "C" => %{
.----------------.
| .--------------. |
| | ______ | | note.note_name : <%= note.note_name %>
| | .' ___ | | | note.name : <%= note.name %>
| | / .' \\_| | | note.velocity : <%= note.velocity %>
| | | | | |
| | \\ `.___.'\\ | |
| | `._____.' | |
| | | |
| '--------------' |
'----------------'
},
"D" => %{
.----------------.
| .--------------. |
| | ________ | | note.note_name : <%= note.note_name %>
| | |_ ___ `. | | note.name : <%= note.name %>
| | | | `. \\ | | note.velocity : <%= note.velocity %>
| | | | | | | |
| | _| |___.' / | |
| | |________.' | |
| | | |
| '--------------' |
'----------------'
},
"E" => %{
.----------------.
| .--------------. |
| | _________ | | note.note_name : <%= note.note_name %>
| | |_ ___ | | | note.name : <%= note.name %>
| | | |_ \\_| | | note.velocity : <%= note.velocity %>
| | | _| _ | |
| | _| |___/ | | |
| | |_________| | |
| | | |
| '--------------' |
'----------------'
},
"F" => %{
.----------------.
| .--------------. |
| | _________ | | note.note_name : <%= note.note_name %>
| | |_ ___ | | | note.name : <%= note.name %>
| | | |_ \\_| | | note.velocity : <%= note.velocity %>
| | | _| | |
| | _| |_ | |
| | |_____| | |
| | | |
| '--------------' |
'----------------'
},
"G" => %{
.----------------.
| .--------------. |
| | ______ | | note.note_name : <%= note.note_name %>
| | .' ___ | | | note.name : <%= note.name %>
| | / .' \\_| | | note.velocity : <%= note.velocity %>
| | | | ____ | |
| | \\ `.___] _| | |
| | `._____.' | |
| | | |
| '--------------' |
'----------------'
},
"A" => %{
.----------------.
| .--------------. |
| | __ | | note.note_name : <%= note.note_name %>
| | / \\ | | note.name : <%= note.name %>
| | / /\\ \\ | | note.velocity : <%= note.velocity %>
| | / ____ \\ | |
| | _/ / \\ \\_ | |
| ||____| |____|| |
| | | |
| '--------------' |
'----------------'
},
"B" => %{
.----------------.
| .--------------. |
| | ______ | | note.note_name : <%= note.note_name %>
| | |_ _ \\ | | note.name : <%= note.name %>
| | | |_) | | | note.velocity : <%= note.velocity %>
| | | __'. | |
| | _| |__) | | |
| | |_______/ | |
| | | |
| '--------------' |
'----------------'
}
}

NOTES.default = %{
.----------------.
| .--------------. |
| | _ | | note.note_name : <%= note.note_name %>
| | /\\| |/\\ | | note.name : <%= note.name %>
| | \\ / | | note.velocity : <%= note.velocity %>
| | |_ _| | |
| | / \\ | |
| | \\/|_|\\/ | |
| | | |
| '--------------' |
'----------------'
}

puts %{
.----------------. .----------------. .----------------. .----------------.
| .--------------. || .--------------. || .--------------. || .--------------. |
| | ______ | || | _____ | || | __ | || | ____ ____ | |
| | |_ __ \\ | || | |_ _| | || | / \\ | || | |_ _||_ _| | |
| | | |__) | | || | | | | || | / /\\ \\ | || | \\ \\ / / | |
| | | ___/ | || | | | _ | || | / ____ \\ | || | \\ \\/ / | |
| | _| |_ | || | _| |__/ | | || | _/ / \\ \\_ | || | _| |_ | |
| | |_____| | || | |________| | || ||____| |____|| || | |______| | |
| | | || | | || | | || | | |
| '--------------' || '--------------' || '--------------' || '--------------' |
'----------------' '----------------' '----------------' '----------------'
}

input = UniMIDI::Input.list.first

MIDI.using(input) do
thru_except :note

receive :note do |note|
return if note.velocity == 0 || note.status.first == 8

# puts "#{note.inspect}"

begin
current_note_text = NOTES[note.note_name]
current_note_template = ERB.new(current_note_text)
puts current_note_template.result(binding)
puts "\n"

rescue Exception => exception
puts "Exception: #{exception}"
end

begin
eval File.read("music.rb")
rescue Exception => exception
puts "Exception: #{exception}"
end

end

join

end
18 changes: 18 additions & 0 deletions sphero.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# .----------------.
# | .--------------. | I AM SPHERO! I can do a lot of things:
# | | ____ | |
# | | .' `. | | sphero.color("azure")
# | | / .--. \ | | sphero.roll(SPEED,DIRECTION_IN_DEGREES)
# | | | | | | | | sphero.keep_going(TIME_IN_SECONDS)
# | | \ `--' / | | sphero.stop
# | | `.____.' | |
# | | | |
# | '--------------' |
# '----------------'

say "Sphero listens, Sphero obeys!"

sphero.color("green")
sphero.keep_going(3)
sphero.color("gold")
69 changes: 69 additions & 0 deletions sphero_saddle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'sphero'
require 'erb'
require 'listen'

puts "Connecting to Sphero..."

# class Sphero
# def initialize(connection) ; end

# def roll(*params)
# puts "Rolling with #{params.inspect}"
# end

# def color(*args)
# puts "Changing Color"
# end

# def stop
# puts "Stopping"
# end
# end

sphero = Sphero.new ENV['sphero'] || ARGV[0] || "/dev/tty.Sphero-GRR-RN-SPP"
puts "Connected to Sphero"

def sphero_template
@sphero_template ||= ERB.new %{
.----------------.
| .--------------. |
| | ____ | |
| | .' `. | |
| | / .--. \\ | | <%= @message %>
| | | | | | | |
| | \\ `--' / | |
| | `.____.' | |
| | | |
| '--------------' |
'----------------'
}
end

def say(message)
@message = message
puts sphero_template.result
end


Thread.abort_on_exception = true
Thread.new do
listener = Listen.to(".")
listener.change do |modified,added,removed|
if modified.first == "sphero.rb"

begin
eval File.read(modified.first)

say "Sphero is ALL DONE!"
sphero.stop

rescue Exception => exception
say "Uh Oh! Sphero Go BOOM! #{exception}"
end
end

end
listener.start
end

sleep

0 comments on commit 9d0d137

Please sign in to comment.