Ruby+Processing

From BenningtonWiki

Jump to: navigation, search

Ditch Java and increase your creativity a thousandfold.

Contents

[edit] Get It

You no longer need JRuby installed to play with Ruby-Processing. All you need is Ruby-Processing itself.
Once downloaded, cd into it and try "script/open samples/jwishy.rb".
Or, cd into it and try:

script/live samples/jwishy.rb

And then you can start live coding with your app.

Processing Language Reference

[edit] Samples

The easiest way to try one of these is to put it in the ruby-processing/samples directory and run it like so:

cd ruby-processing
script/open samples/CircleCollision.rb

[edit] Tree

Image:Trees.jpg

Tree.rb

Translation of Tree sample from the Processing Topics page. I modified it to automatically spin through one unfolding of the tree and print out the time it took.
Compare it to Tree.pde in Java+Processing.

[edit] CircleCollision

CircleCollision.rb

Translation of CircleCollision sample from the Processing Topics page.

CircleCollision2.rb

Modification of CircleCollision that handles more than two circles. Plus, I made it easier on Ruby eyes.

As of Ruby-Processing 0.3, Tree and CircleCollision2 are included with the samples.

[edit] Gotchas

[edit] Using Processing

All of the Processing methods (frameRate, ellipse, and the 158 others) are instance methods in your Sketch because you're inheriting them from Processing::App. This makes it easy as pie to use them within your Sketch.

require 'ruby-processing'
 
class Sketch < Processing::App
  def setup
    color_mode RGB, 1
    frame_rate 30
  end
 
  def draw
    triangle(rand(width), rand(height), rand(width), rand(height), rand(width), rand(height))
  end
end
 
P = Sketch.new(:width => 600, :height => 600, :title => "Triangle Madness")

In order to use the Processing methods outside of of your Sketch class you need to include an explicit reference. I stuff the Sketch instance in the global constant P to make this available everywhere.

As of release 0.3, you can also call Processing::App.current to get a handle to the current app.

class Ball
  def draw
    P.fill 240
    P.ellipse @x, @y, @r, @r
  end
end

[edit] Serial I/O

Seeing this?

$ script/open serial_test.rb
Experimental:  JNI_OnLoad called.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
gnu.io.PortInUseException: Unknown Application
	at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)
	at processing.serial.Serial.<init>(Serial.java:136)
	at processing.serial.Serial.<init>(Serial.java:102)
        ... about a thousand more lines of java backtrace ...

portInUseException?

Better explanations forthcoming.

[edit] Serial Example

Using serial with an Arduino. I was having problems writing (see above), so all this example does is spit out six bytes. On my Arduino it makes an RGB LED fade through HSV colors. Jealous?

Remember to copy the serial libraries from your Processing (in my case: processing-0135/libraries/serial/libraries/*) to ruby-processing/libraries/

# Ruby+Processing 0.6
 
require 'ruby-processing'
 
class SerialTest < Processing::App
  load_java_library "serial"
  include_package "processing.serial"
 
  def setup
    @port = open_arduino_serial
    if @port.nil?
      puts "Error: Serial port not found"
    else
      @port.write(97)
      @port.write(100)
      @port.write(120)
      @port.write(97)
      @port.write(97)
      @port.write(97)
    end
  end
 
  def open_arduino_serial
    Serial.list.each do |name|
        return Serial.new(self, name, 9600) if name =~ /usbserial/
    end
    return nil
  end
end
 
SerialTest.new(:title => "Serial Test")

[edit] Serial Example

Using serial with an Arduino. I was having problems writing (see above), so all this example does is spit out six bytes. On my Arduino it makes an RGB LED fade through HSV colors. Jealous?

# Ruby+Processing 0.5
 
require 'ruby-processing'
 
class SerialTest < Processing::App
  load_library "serial"
  include_package "processing.serial"
 
  def setup
    @port = open_arduino_serial
    @port.write(97)
    @port.write(100)
    @port.write(120)
    @port.write(97)
    @port.write(97)
    @port.write(97)
  end
 
  def open_arduino_serial
    Serial.list.each do |name|
        return Serial.new(self, name, 9600) if name =~ /usbserial/
    end
    return nil
  end
 
end
 
 
SerialTest.new(:title => "Serial Test")

[edit] Applets!

As of Ruby-Processing 0.6, you can export your sketches as applets. For instance: http://fiercefrontiers.com/applets/jwishy/ Do it like this:

./applet_tree my_sketch.rb

That will make a folder at applets/my_sketch, which contains an HTML page that you can post on the web. But there are a couple of caveats about require'ing other files, so check the ReadMe.

[edit] Questions

Can JRuby+Processing apps be embedded in a web page?

  • (working on it --rugnetta)
  • Mike, be sure to check out Ruby for the Web? Check!
    • Ruby in y'browsa, son -- not really what we're after. this is a javascript program that ajaxes back to an irb session on a server. (joe)
  • I've also emailed Charles Nutter about this, and we can hopefully be baking some applet pie sometime real soon. (omygawshkenas)
  • Nevermind, maybe not so easy at all. I've looked at the processing source, which does an insane amount of fudging and regex scrubbing to get their sketches into applets, and I've looked at the JRuby sample applet, which cleanly plops IRB into an applet. How to get both together may be beyond me. (omygawshkenas)
  • Yes, yes you can. Get the latest Ruby-Processing (0.6) and applet away.
Personal tools