Permalink
Browse files

New

  • Loading branch information...
1 parent 951fa1b commit 604747c4dfc014b7c29d9e76cc69d7d8b79eaaf5 @Neurogami committed Sep 13, 2009
Showing with 257 additions and 0 deletions.
  1. +7 −0 jar-and-run.sh
  2. +11 −0 lib/ruby/wii_api_manager.rb
  3. +80 −0 lib/ruby/wiimotable.rb
  4. +159 −0 lib/ruby/wiimote_event_listener.rb
View
@@ -0,0 +1,7 @@
+# Assumes you have a Jimpanzee app with Roir. And this script might only be needed on Linux.
+# It's here pending better resolution of loading binary libs from a JRuby app.
+# The Wii java code needs a .so file.
+
+jruby -S rake roir:jar
+LD_LIBRARY_PATH=./lib/java/ java -Djava.library.path=./lib/java/ -jar package/jar/NAME-OF-YOUR-APP.jar
+
@@ -0,0 +1,11 @@
+module Neurogami
+
+ class WiiUseApiManager < Java::wiiusej.WiiUseApiManager
+
+ def self.wiimotes number_of_remotes, send_rumble=true
+ wiimotes = getWiimotes number_of_remotes, send_rumble
+ wiimotes.to_ary
+ end
+
+ end
+end
View
@@ -0,0 +1,80 @@
+# http://wiki.jruby.org/wiki/Calling_Java_from_JRuby#Use_include_package_within_a_Ruby_Module_to_import_a_Java_Package
+
+# require 'spinner_dialog' # need a way to allow some UI stuff without presuming too much
+
+require 'pp'
+
+module Neurogami
+
+ module Wiimotable
+ def wiimote_me listener, max_attempts=nil
+
+ attempts_so_far = 0
+
+ begin
+ attempts_so_far += 1
+ transfer[:attempt_number] = attempts_so_far
+ signal :set_discovery_mode
+
+ @remote = get_wiimote
+ raise "Nil remote" unless @remote
+ prepare_wiimote @remote , listener
+ rescue Exception => e
+ warn "Error #{e.inspect}"
+ warn "Signal to end dicovery mode. Have @remote #{@remote.inspect}"
+
+ exit if e.is_a?(NameError)
+
+ signal :end_discovery_mode
+ if max_attempts.nil? || attempts_so_far < max_attempts
+ warn "Attempte #{attempts_so_far}. Retry connection ... "
+ retry
+ else
+ raise
+ end
+ end
+ ensure
+ signal :end_discovery_mode
+ end
+
+
+ def get_wiimote
+ begin
+ wiimotes = WiiUseApiManager.wiimotes(1)
+ warn "wiimotes: #{wiimotes.pretty_inspect}"
+ wiimote = wiimotes.first
+ rescue Exception => e
+ (STDERR.puts( " \n ERROR at #{__FILE__}:#{__LINE__}: #{e.inspect} " ); STDERR.flush ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
+ raise e
+ end
+ end
+
+ def prepare_wiimote wiimote, listener
+ wiimote.activateIRTRacking
+ wiimote.activateMotionSensing
+ wiimote.addWiiMoteEventListeners(listener)
+ wiimote
+ end
+ end
+
+ module WiimotableView
+
+ def set_discovery_mode model, transfer
+ begin
+ @spinner = SpinnerDialog.new(["Attempt #{transfer[:attempt_number] }", "Press buttons 1+2 together ..."])
+ @spinner.visible = true
+ rescue Exception
+ warn "Error creating SpinnerDialog: #{$!}"
+ raise
+ end
+ end
+
+ def end_discovery_mode model, transfer
+ return unless @spinner
+ @spinner.visible = false
+ @spinner.hide
+ @spinner = nil
+ end
+ end
+
+end
@@ -0,0 +1,159 @@
+module Neurogami
+
+ class WiimoteEventListener
+=begin
+ /* Buttons MACRO */
+ private static short WIIMOTE_BUTTON_TWO = 0x0001
+ private static short WIIMOTE_BUTTON_ONE = 0x0002
+ private static short WIIMOTE_BUTTON_B = 0x0004
+ private static short WIIMOTE_BUTTON_A = 0x0008
+ private static short WIIMOTE_BUTTON_MINUS = 0x0010
+ private static short WIIMOTE_BUTTON_ZACCEL_BIT6 = 0x0020
+ private static short WIIMOTE_BUTTON_ZACCEL_BIT7 = 0x0040
+ private static short WIIMOTE_BUTTON_HOME = 0x0080
+ private static short WIIMOTE_BUTTON_LEFT = 0x0100
+ private static short WIIMOTE_BUTTON_RIGHT = 0x0200
+ private static short WIIMOTE_BUTTON_DOWN = 0x0400
+ private static short WIIMOTE_BUTTON_UP = 0x0800
+ private static short WIIMOTE_BUTTON_PLUS = 0x1000
+ private static short WIIMOTE_BUTTON_ZACCEL_BIT4 = 0x2000
+ private static short WIIMOTE_BUTTON_ZACCEL_BIT5 = 0x4000
+ private static int WIIMOTE_BUTTON_UNKNOWN = 0x8000
+ private static short WIIMOTE_BUTTON_ALL = 0x1F9F
+=end
+
+ WIIMOTE_BUTTON_TWO = 0x0001
+ WIIMOTE_BUTTON_ONE = 0x0002
+ WIIMOTE_BUTTON_B = 0x0004
+ WIIMOTE_BUTTON_A = 0x0008
+ WIIMOTE_BUTTON_MINUS = 0x0010
+ WIIMOTE_BUTTON_ZACCEL_BIT6 = 0x0020
+ WIIMOTE_BUTTON_ZACCEL_BIT7 = 0x0040
+ WIIMOTE_BUTTON_HOME = 0x0080
+ WIIMOTE_BUTTON_LEFT = 0x0100
+ WIIMOTE_BUTTON_RIGHT = 0x0200
+ WIIMOTE_BUTTON_DOWN = 0x0400
+ WIIMOTE_BUTTON_UP = 0x0800
+ WIIMOTE_BUTTON_PLUS = 0x1000
+ WIIMOTE_BUTTON_ZACCEL_BIT4 = 0x2000
+ WIIMOTE_BUTTON_ZACCEL_BIT5 = 0x4000
+ WIIMOTE_BUTTON_UNKNOWN = 0x8000
+ WIIMOTE_BUTTON_ALL = 0x1F9F
+
+
+ def initialize mapping
+ define_on_ir_event(mapping.delete :ir_event) if mapping[:ir_event ]
+ define_on_motion_sensing_event(mapping.delete :motion_sensing_event) if mapping[:motion_sensing_event ]
+ assign_button_mapping mapping
+ end
+
+
+ def define_on_ir_event prk
+ self.class.send :define_method, :on_ir_event do |event|
+ prk.call(event)
+ end
+ end
+
+ def define_on_motion_sensing_event prk
+ self.class.send :define_method, :on_motion_sensing_event do |event|
+ prk.call(event)
+ end
+ end
+
+
+ # The goal is that when creating an app, there is a default event listener that does nothing.
+ # The developer than provides either a whole new listener, or passes in a set of event mappings
+ # when creatng an instance of this class:
+ # :wiimote_button_two => :some_cool_thing, :wiimote_button_home => other_cool_thing
+ #
+ # We can easily do this for single keys, but when we have, say, a button event number of 6, we need
+ # to find the mapping for WIIMOTE_BUTTON_ONE + WIIMOTE_BUTTON_B
+ # So we allow mappings such as
+ # :wiimote_button_home_and_wiimote_button_two => :really_cool_stuff
+ # then decompose that key into WIIMOTE_BUTTON_HOME + WIIMOTE_BUTTON_TWO to get the hash number
+ #
+ # These key mappings can map to lambdas or method names. A typical scenario is that
+ # some Jimpanzee controller will want to map controller methods to Wiimote events:
+ #
+ #
+ # mappings = {
+ # :wiimote_button_home => lambda {|e| FooController.instance.home_button_action_performed(e) },
+ # :wiimote_button_a_and_wiimote_button_b => lambda {|e| FooController.instance.a_and_b_action_performed(e) }
+ # }
+ #
+ # listener = WiimoteEventListener.new(mappings)
+ #
+ def assign_button_mapping mapping
+
+ @button_handlers ||= { }
+
+ mapping.each do |button_key, response|
+ key_str = button_key.to_s
+ keys = key_str.split( '_and_')
+ key_sum = keys.inject(0){|sum, k| sum + eval(k.upcase) }
+ @button_handlers[key_sum] = response
+ end
+ end
+
+ def assign_ir_mapping prk
+ define_method
+ end
+
+
+ # WiimoteButtonsEvent
+ def onButtonsEvent event
+ button = event.buttonsJustPressed
+
+ return if button == 0
+ return unless handler = @button_handlers[button]
+
+ if handler.respond_to?(:call)
+ begin
+ handler[event]
+ rescue Exception => e
+ STDERR.puts " \n ERROR at #{__FILE__}:#{__LINE__}: #{e.inspect} \n handler = #{handler.pretty_inspect}"
+ raise e
+ end
+ else
+ send handler, event
+ end
+ end
+
+ # IREvent
+ def on_ir_event event
+ warn "Default on_ir_event " if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
+ end
+
+
+
+ def on_motion_sensing_event event
+ STDERR.puts( ":DEBUG #{__FILE__}:#{__LINE__} DEFAULT onMotionSensingEvent" ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
+ end
+
+
+ # ExpansionEvent
+ def onExpansionEvent event
+ # warn event
+ end
+
+ # StatusEvent
+ def onStatusEvent event
+ # warn event
+ end
+
+ # DisconnectionEvent
+ def onDisconnectionEvent event
+ # warn event
+ end
+
+ # NunchukInsertedEvent
+ def onNunchukInsertedEvent event
+ # warn event
+ end
+
+ # NunchukRemovedEvent
+ def onNunchukRemovedEvent event
+ # warn event
+ end
+ end
+end

0 comments on commit 604747c

Please sign in to comment.