This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-01-27 12:08:46

logiblocs
Scratcher
Registered: 2010-05-05
Posts: 100+

Remote Sensor Connections in Ruby

Here is a tutorial about how to use remote sensor connections in ruby. I have made a tiny one-file library to simplify this. You can find it here. The syntax looks like this:

Code:

require "scratchrsc"
class PrintRSC < RSCWatcher
    def on_broadcast(name) # when a broadcast is sent
        puts "broadcast #{name}" # print it to the screen
    end

    def on_sensor_update(name,value) # when a variable or sensor is updated
        puts "#{name} = #{value}" # print the change to the screen
    end

    def broadcast_hello # when the broadcast "hello" is sent, ignore on_broadcast and
        puts "Hello World" # greet the world,
        broadcast "foo" # and send another broadcast
    end
end

watcher = PrintRSC.new # you can provide the host as an argument
watcher.sensor_update "connected", "1"
loop { watcher.handle_command }

All you need to run this is ruby. Download the library file and place it in the same directory. Name the example test.rb. Make sure scratch is running and has remote sensor connections enabled, and then run it usng this command:
$ ruby -I . test.rb

I can upload a gem for this if anyone finds this useful  smile

Last edited by logiblocs (2013-01-27 12:21:18)

Offline

 

#2 2013-01-28 09:58:54

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Remote Sensor Connections in Ruby

That cool! It's nice to see modules coming out for other programming languages!

Offline

 

Board footer