#!/usr/bin/env ruby require 'rubygems' #Include the FireWatir file. require 'firewatir' #include the FireWatir Module. include FireWatir require 'joystick' require "festivaltts4r" require 'optparse' class CatArguments < Hash def initialize(args) super() self[:joystick] = '/dev/input/js0' self[:site]= 'rubypython.railsplayground.net/' opts = OptionParser.new do |opts| opts.banner = "Usage: #$0 [options]" opts.on('-j', '--joystick [STRING]', 'default is /dev/input/js0') do |s| self[:joystick] = s end opts.on('-l', '--local', 'to use localhost:3000') do self[:site] = 'localhost:3000/' end opts.on('-b','--button [STRING]', 'to limit input to single button') do |s| self[:only_button]=s.to_i end opts.on_tail('-h', '--help', 'display this help and exit') do puts opts exit end end opts.parse!(args) end end def correct_button(button_arg,button_pressed) if button_arg button_pressed == button_arg else true end end arguments = CatArguments.new(ARGV) site=arguments[:site] # p arguments t_edge_detection=1 tShortClick=2 tAutoRefresh=5 junk=[] def go_to (f,st) p st f.goto(st) page_text=f.text if page_text =~ /say:"(.+)"/ speak=$1 speak.to_speech end t=f.table(:id,/content/) r=t.rows.first apps=[] r.each do |cell| m=cell.to_s apps << m unless m.empty? end cap ||= 'modules' aindex=apps.index(cap) || 0 p apps # p "aindex at loading #{aindex}" [Time.now,apps] end open('jlog.txt','w') do |wf| if !Joystick::Device::open(arguments[:joystick]){ |joy| puts "Joystick: #{joy.name}", "Axes / Buttons : #{joy.axes} / #{joy.buttons}", begin t_release=Time.now t_clicking=Time.now long_counter=1 apps=ARGV if apps.empty? # apps=["easypic","abcd","words"] apps=['modules'] end puts "running applications: #{apps.join(";")}" aindex=0 #the index for apps cap=apps[aindex] #currently running app # th=Thread.new{system('script/server')} # sleep(3) p ff=Firefox.new lastupdate,selapps= go_to(ff, "http://#{site}#{cap}/index/stopscroll+nobuttons") pressed=[] selapps=['abcd','easypic','modules'] # loop forever loop { while joy.pending? ev = joy.ev if correct_button(arguments[:only_button], ev.num) lastupdate=Time.now wf.puts "n#{ev.num} val #{ev.val} #{lastupdate}" case ev.class when Joystick::Event::INIT puts 'init' else if (ev.val!=0) #start of event if !pressed.index(ev.num) pressed.push(ev.num) end else #val=0, possible end of event pressed.delete(ev.num) end end end end # wf.puts pressed.join(',') unless pressed.empty? if t_clicking and pressed.empty? #click over t_release=Time.now click_duration=Time.now-t_clicking wf.puts "click duration #{click_duration}" if click_duration < tShortClick lastupdate,selapps= go_to(ff,"http://#{site}#{cap}/select") wf.puts "selected #{cap} from #{selapps}" end t_clicking=nil elsif !t_clicking and !pressed.empty? t_clicking=Time.now long_counter=1 t_release=nil end # wf.puts apps.join "|" if t_clicking and ((Time.now-t_clicking) > (long_counter*tShortClick)) then long_counter+=1 p 'selapps ', selapps apps=selapps cap ||= 'modules' aindex=apps.index(cap) || 0 aindex=(aindex+1) % apps.size cap=apps[aindex] p apps p "ai #{aindex}" lastupdate,apps=go_to(ff, "http://#{site}#{cap}/first_time") end junk=[] if ((Time.now-lastupdate)>tAutoRefresh) and t_release then lastupdate,junk= go_to(ff, "http://#{site}#{cap}/down") end } ensure ff.close unless ff.nil? # th.terminate end } then puts 'cannot find joystick at /dev/input/js0' end end